PHP Upload imagen con thumbs resize and transparent png


/ Published in: PHP
Save to your folder(s)

AL subir imagen png, se deben pasar al directorio con transparencia


Copy this code and paste it in your HTML
  1. $myfile=$_FILES["youimage"];
  2. function ismyimage($myfile)
  3. {
  4. if((($myfile["type"] == "image/gif") || ($myfile["type"] == "image/jpg") || ($myfile["type"] == "image/jpeg") || ($myfile["type"] == "image/png")) && ($myfile["size"] <= 2097152 /*2mb*/) ) return true;
  5. else return false;
  6. }
  7.  
  8. function upload_file($myfile)
  9. {
  10.  
  11. if(ismyimage($myfile)){
  12. $information=getimagesize($myfile["tmp_name"]);
  13. $mywidth=$information[0];
  14. $myheight=$information[1];
  15.  
  16. $newwidth=$mywidth;
  17. $newheight=$myheight;
  18. while(($newwidth > 600) || ($newheight > 400 )){
  19. $newwidth = $newwidth-ceil($newwidth/100);
  20. $newheight = $newheight-ceil($newheight/100);
  21. }
  22.  
  23. $files=$myfile["name"];
  24.  
  25. if($myfile["type"] == "image/gif"){
  26. $tmp=imagecreatetruecolor($newwidth,$newheight);
  27. $src=imagecreatefromgif($myfile["tmp_name"]);
  28. imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
  29. $con=imagegif($tmp, $files);
  30. imagedestroy($tmp);
  31. imagedestroy($src);
  32. if($con){return true;} else {return false;}
  33. }
  34. else if(($myfile["type"] == "image/jpg") || ($myfile["type"] == "image/jpeg") ) {
  35. $tmp=imagecreatetruecolor($newwidth,$newheight);
  36. $src=imagecreatefromjpeg($myfile["tmp_name"]);
  37. imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
  38. $con=imagejpeg($tmp, $files);
  39. imagedestroy($tmp);
  40. imagedestroy($src);
  41. if($con){ return true;} else {return false;}
  42. }
  43. else if($myfile["type"] == "image/png") {
  44. $tmp=imagecreatetruecolor($newwidth,$newheight);
  45. $src=imagecreatefrompng($myfile["tmp_name"]);
  46. imagealphablending($tmp, false);
  47. imagesavealpha($tmp,true);
  48. $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
  49. imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
  50. imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
  51. $con=imagepng($tmp, $files);
  52. imagedestroy($tmp);
  53. imagedestroy($src);
  54. if($con){ return true;} else {return false;}
  55. }
  56.  
  57. }
  58. else return false;
  59. }

URL: http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresample

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.