Posted By

damarev on 07/03/11


Tagged


Versions (?)

scaling PNGs with transparancy channel


 / Published in: PHP
 

  1. function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false )
  2. {
  3. if ( $height <= 0 && $width <= 0 ) {
  4. return false;
  5. }
  6. $info = getimagesize($file);
  7. $image = '';
  8.  
  9. $final_width = 0;
  10. $final_height = 0;
  11. list($width_old, $height_old) = $info;
  12.  
  13. if ($proportional) {
  14. if ($width == 0) $factor = $height/$height_old;
  15. elseif ($height == 0) $factor = $width/$width_old;
  16. else $factor = min ( $width / $width_old, $height / $height_old);
  17. $final_width = round ($width_old * $factor);
  18. $final_height = round ($height_old * $factor);
  19.  
  20. }
  21. else {
  22. $final_width = ( $width <= 0 ) ? $width_old : $width;
  23. $final_height = ( $height <= 0 ) ? $height_old : $height;
  24. }
  25.  
  26. switch ($info[2] ) {
  27. case IMAGETYPE_GIF:
  28. $image = imagecreatefromgif($file);
  29. break;
  30. case IMAGETYPE_JPEG:
  31. $image = imagecreatefromjpeg($file);
  32. break;
  33. case IMAGETYPE_PNG:
  34. $image = imagecreatefrompng($file);
  35. break;
  36. default:
  37. return false;
  38. }
  39.  
  40. $image_resized = imagecreatetruecolor( $final_width, $final_height );
  41.  
  42. if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  43. $trnprt_indx = imagecolortransparent($image);
  44. // If we have a specific transparent color
  45. if ($trnprt_indx >= 0) {
  46. // Get the original image's transparent color's RGB values
  47. $trnprt_color = imagecolorsforindex($image, $trnprt_indx);
  48. // Allocate the same color in the new image resource
  49. $trnprt_indx = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  50. // Completely fill the background of the new image with allocated color.
  51. imagefill($image_resized, 0, 0, $trnprt_indx);
  52. // Set the background color for new image to transparent
  53. imagecolortransparent($image_resized, $trnprt_indx);
  54. }
  55. // Always make a transparent background color for PNGs that don't have one allocated already
  56. elseif ($info[2] == IMAGETYPE_PNG) {
  57. // Turn off transparency blending (temporarily)
  58. imagealphablending($image_resized, false);
  59. // Create a new transparent color for image
  60. $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  61.  
  62. // Completely fill the background of the new image with allocated color.
  63. imagefill($image_resized, 0, 0, $color);
  64.  
  65. // Restore transparency blending
  66. imagesavealpha($image_resized, true);
  67. }
  68. }
  69.  
  70. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
  71.  
  72. if ( $delete_original ) {
  73. if ( $use_linux_commands )
  74. exec('rm '.$file);
  75. else
  76. @unlink($file);
  77. }
  78.  
  79. switch ( strtolower($output) ) {
  80. case 'browser':
  81. $mime = image_type_to_mime_type($info[2]);
  82. header("Content-type: $mime");
  83. $output = NULL;
  84. break;
  85. case 'file':
  86. $output = $file;
  87. break;
  88. case 'return':
  89. return $image_resized;
  90. break;
  91. default:
  92. break;
  93. }
  94.  
  95. switch ($info[2] ) {
  96. case IMAGETYPE_GIF:
  97. imagegif($image_resized, $output);
  98. break;
  99. case IMAGETYPE_JPEG:
  100. imagejpeg($image_resized, $output);
  101. break;
  102. case IMAGETYPE_PNG:
  103. imagepng($image_resized, $output);
  104. break;
  105. default:
  106. return false;
  107. }
  108.  
  109. return true;
  110. }

Report this snippet  

You need to login to post a comment.