Posted By

eme_dlr on 09/14/10


Tagged

php resize image thumbnail


Versions (?)

Resize image


 / Published in: PHP
 

  1. function crear_miniatura($name,$filename,$new_w,$new_h,$carpeta){
  2. $system=explode(".",$_FILES['Filedata']['name']);
  3. if (preg_match("/jpg|jpeg|JPG|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
  4. if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
  5. if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}
  6.  
  7. $old_x=imageSX($src_img);
  8. $old_y=imageSY($src_img);
  9. $proporcion = $old_x/$old_y;
  10. $proporcion_correcta = $new_w/$new_h;
  11. if ($proporcion >= $proporcion_correcta) {
  12. $recorte_h=$old_y;
  13. $recorte_w=$old_y*$proporcion_correcta;
  14. }else{
  15. // si es vertical
  16. $recorte_h=$old_y;
  17. $recorte_w=$old_y*$proporcion_correcta;
  18. //$recorte_w=$old_x;
  19. //$recorte_h=$old_x/$proporcion_correcta;
  20. }
  21. $x_recorte = $old_x/2 - $recorte_w/2;
  22. $y_recorte = $old_y/2 - $recorte_h/2;
  23. $dst_img=ImageCreateTrueColor($new_w,$new_h);
  24. imagecopyresampled($dst_img,$src_img,0,0,$x_recorte,$y_recorte,$new_w,$new_h,$recorte_w,$recorte_h);
  25. imagejpeg($dst_img,$carpeta.$filename,85);
  26. imagedestroy($dst_img);
  27. imagedestroy($src_img);
  28. }
  29.  
  30.  
  31. crear_miniatura($_FILES['Filedata']['tmp_name'],$nuevo_nom,400,300,"../media/");
  32. crear_miniatura($_FILES['Filedata']['tmp_name'],"pq_".$nuevo_nom,40,40,"../media/");

Report this snippet  

You need to login to post a comment.