Image thumbnail generator for PHP


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



Copy this code and paste it in your HTML
  1. $upfile = $_REQUEST['p'];
  2.  
  3. $max_width = $_REQUEST["w"];
  4. $max_height = $_REQUEST["h"];
  5.  
  6. $def_width = $_REQUEST["wh"];
  7. $def_height = $_REQUEST["hw"];
  8.  
  9. if(empty($def_width) && empty($def_height)) {
  10. $def_width = '245';
  11. $def_height = '245';
  12. }
  13. if ($def_height=='') $def_height = $def_width;
  14.  
  15. //IMAGE PATH
  16. $exportPng = false;
  17. if(!file_exists($_SERVER['DOCUMENT_ROOT'] . $upfile)) $upfile = $_SERVER['DOCUMENT_ROOT'] . $upfile;
  18. else {
  19. $upfile = "img/nophoto.jpg";
  20. $exportPng = true;
  21. }
  22.  
  23. //since we already have predefined values of extension
  24. $ext = strtolower(substr($upfile, -3));
  25.  
  26. //INCREASE MEMORY LIMIT WHEN WORKING WITH
  27. ini_set('memory_limit', '64M');
  28.  
  29. //MAKE NEW IMAGE OUT OF EXTENSION
  30. if ($ext=="gif") { $src = @ImageCreateFromGif($upfile); header("Content-Type: image/gif"); }
  31. if ($ext=="jpg") { $src = @ImageCreateFromJpeg($upfile); header("Content-Type: image/jpeg"); }
  32. if ($ext=="png") { $src = @ImageCreateFromPng($upfile); header("Content-Type: image/png"); }
  33.  
  34. $size = GetImageSize($upfile);
  35. $width = $size[0];
  36. $height = $size[1];
  37.  
  38. if ($def_width == '') {
  39. // Proportionally resize the image to the max sizes
  40. if (($max_width == '')&&($max_height == '')) {
  41. $max_width = $width;
  42. $max_height = $height;
  43. }
  44. if ($max_width == '') $max_width = ($max_height*$width) / $height;
  45. if ($max_height == '') $max_height = ($max_width*$height) / $width;
  46. if ($max_height > $max_width) $highter_dimension=$max_height;
  47. else $highter_dimension = $max_width;
  48.  
  49. if ($width == 0) $x_ratio = $max_width;
  50. else $x_ratio = $max_width / $width;
  51. if ($height == 0) $y_ratio = $max_height;
  52. else $y_ratio = $max_height / $height;
  53.  
  54. if( ($width <= $max_width) && ($height <= $max_height) ) {
  55. $tn_width = $width;
  56. $tn_height = $height;
  57. } elseif (($x_ratio * $height) < $max_height) {
  58. $tn_height = ceil($x_ratio * $height);
  59. $tn_width = $max_width;
  60. } else {
  61. $tn_width = ceil($y_ratio * $width);
  62. $tn_height = $max_height;
  63. }
  64. $dst = ImageCreateTrueColor($tn_width, $tn_height);
  65. ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
  66. }
  67.  
  68. if($def_width != '') {
  69. $highter_dimension = $def_width;
  70. if ($def_width < $def_height) $highter_dimension = $def_height;
  71.  
  72. $factor_w = $width / $def_width;
  73. $factor_h = $height / $def_height;
  74.  
  75. if ($factor_w > $factor_h) {
  76. $new_height = floor($def_height * $factor_h);
  77. $new_width = floor($def_width * $factor_h);
  78. } else {
  79. $new_height = floor($def_height * $factor_w);
  80. $new_width = floor($def_width * $factor_w);
  81. }
  82.  
  83. $src_x = ceil(($width - $new_width) / 2);
  84. $src_y = ceil(($height - $new_height) / 2);
  85.  
  86. $dst = ImageCreateTrueColor($def_width, $def_height);
  87. @ImageCopyResized($dst, $src, 0, 0, $src_x, $src_y, $def_width, $def_height, $new_width, $new_height);
  88. }
  89.  
  90. if(!$exportPng) Header("Content-type: image/jpeg");
  91. else Header("Content-type: image/png");
  92.  
  93.  
  94. //OUTPUT INTO PNG TO GET HIGHER QUALITY IMAGE
  95. if(!$exportPng) ImageJpeg($dst);
  96. else ImagePng($dst);
  97.  
  98. @ImageDestroy($src);
  99. @ImageDestroy($dst);

URL: http://stuntsnippets.com/php-thumbnail-generator/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.