Posted By

jmiller on 11/01/09


Tagged

resize image


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

luman
vali29
fackz
ignace
aleksanderek
marcus784


Resize Images


 / Published in: PHP
 

URL: http://programanddesign.com/php/resize-images-using-this-php-script/

  1. <?php
  2. // @fill
  3. // center - Centers the image without any scaling. May be smaller than dimensions, or cropped.
  4. // stretch - Stretches the image to fill dimensions. May change image proportions.
  5. // squeeze - Scales the image, maintaining original proportions. May not fill dimensions.
  6. // trim - Scales the image, maintaining original proportions, to fill dimensions. Image may be centered and trimmed.
  7. // trim_rand - Scales the image, maintaining original proportions, to fill dimensions. Trimmed randomly.
  8.  
  9. function resize($src_filename, $dst_filename, $dst_width, $dst_height, $fill='squeeze', $quality=80, $png_filters=PNG_NO_FILTER)
  10. {
  11. if(!file_exists($src_filename)) {
  12. //throw new Exception("File does not exist: $src_filename");
  13. return false;
  14. }
  15. if(empty($dst_filename)) {
  16. $dst_filename = $src_filename;
  17. }
  18. if($dst_width <= 0) {
  19. //throw new Exception("Width must be positive: $dst_width");
  20. return false;
  21. }
  22. if($dst_height <= 0) {
  23. //throw new Exception("Height must be positive: $dst_height");
  24. return false;
  25. }
  26. $src_ext = substr($src_filename,strrpos($src_filename,'.')+1);
  27. switch(strtolower($src_ext)) {
  28. case 'gif':
  29. $src_image = imagecreatefromgif($src_filename);
  30. break;
  31. case 'jpe':
  32. case 'jpeg':
  33. case 'jpg':
  34. $src_image = imagecreatefromjpeg($src_filename);
  35. break;
  36. case 'png':
  37. $src_image = imagecreatefrompng($src_filename);
  38. break;
  39. default:
  40. //throw new Exception("Invalid source file extension: $src_ext");
  41. return false;
  42. }
  43. $src_width = imagesx($src_image);
  44. $src_height = imagesy($src_image);
  45. switch(strtolower(trim($fill))) {
  46. case 'center':
  47. $src_x = round($src_width/2-$dst_width/2);
  48. $src_y = round($src_height/2-$dst_height/2);
  49. if($src_x < 0) {
  50. $dst_width = $src_width;
  51. $src_x = 0;
  52. }
  53. if($src_y < 0) {
  54. $dst_height = $src_height;
  55. $src_y = 0;
  56. }
  57. $dst_image = imagecreatetruecolor($dst_width, $dst_height);
  58. imagecopyresampled($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $dst_width, $dst_height);
  59. break;
  60. case 'stretch':
  61. $dst_image = imagecreatetruecolor($dst_width, $dst_height);
  62. imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
  63. break;
  64. case 'crop':
  65. case 'crop_center':
  66. case 'trim_center':
  67. case 'trim':
  68. $src_ratio = $src_width/$src_height;
  69. $dst_ratio = $dst_width/$dst_height;
  70. if($src_ratio < $dst_ratio) // trim top and bottom
  71. {
  72. $ratio = $src_width/$dst_width;
  73. $crop_height = $dst_height*$ratio;
  74. $src_y = round(($src_height-$crop_height)/2);
  75. $crop_width = $src_width;
  76. $src_x = 0;
  77. }
  78. else // trim left and right
  79. {
  80. $ratio = $src_height/$dst_height;
  81. $crop_width = $dst_width*$ratio;
  82. $src_x = round(($src_width-$crop_width)/2);
  83. $crop_height = $src_height;
  84. $src_y = 0;
  85. }
  86. $dst_image = imagecreatetruecolor($dst_width, $dst_height);
  87. imagecopyresampled($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $crop_width, $crop_height);
  88. break;
  89. case 'crop_rand':
  90. case 'trim_rand':
  91. $src_ratio = $src_width/$src_height;
  92. $dst_ratio = $dst_width/$dst_height;
  93. if($src_ratio < $dst_ratio) // trim top and bottom
  94. {
  95. $ratio = $src_width/$dst_width;
  96. $crop_height = $dst_height*$ratio;
  97. $src_y = rand(0,$src_height-$crop_height);
  98. $crop_width = $src_width;
  99. $src_x = 0;
  100. }
  101. else // trim left and right
  102. {
  103. $ratio = $src_height/$dst_height;
  104. $crop_width = $dst_width*$ratio;
  105. $src_x = rand(0,$src_width-$crop_width);
  106. $crop_height = $src_height;
  107. $src_y = 0;
  108. }
  109. $dst_image = imagecreatetruecolor($dst_width, $dst_height);
  110. imagecopyresampled($dst_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $crop_width, $crop_height);
  111. break;
  112. case 'squeeze':
  113. case 'stretch_prop':
  114. case 'fit':
  115. $ratio = max($src_width/$dst_width, $src_height/$dst_height);
  116. if($ratio < 1) $ratio = 1; // do not enlarge
  117. $dst_width = round($src_width/$ratio);
  118. $dst_height = round($src_height/$ratio);
  119. $dst_image = imagecreatetruecolor($dst_width, $dst_height);
  120. imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
  121. break;
  122. default:
  123. //throw new Exception("Unrecognized fill type: $fill");
  124. return false;
  125. }
  126. $dst_ext = substr($dst_filename,strrpos($dst_filename,'.')+1);
  127. if(empty($dst_ext)) {
  128. $dst_ext = $src_ext;
  129. $dst_filename .= ".$src_ext";
  130. }
  131. switch(strtolower($dst_ext)) {
  132. case 'gif':
  133. return imagegif($dst_image, $dst_filename);
  134. case 'jpe':
  135. case 'jpeg':
  136. case 'jpg':
  137. return imagejpeg($dst_image, $dst_filename, $quality);
  138. case 'png':
  139. return imagepng($dst_image, $dst_filename, $quality, $png_filters);
  140. default:
  141. //throw new Exception('Invalid destination file extension: $dst_ext');
  142. return false;
  143. }
  144. }
  145.  
  146. ?>

Report this snippet  

You need to login to post a comment.