PageRenderTime 60ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/resizer.php

https://github.com/jon1012/smoothgallery
PHP | 113 lines | 63 code | 15 blank | 35 comment | 17 complexity | e48f6470b28f8a641d9085eb89bd5713 MD5 | raw file
  1. <?php
  2. /* ----------------------------------------------------------------
  3. Script coming with JonDesign's SmoothGallery (http://smoothgallery.jondesign.net).
  4. It is an adapted version of
  5. http://sneak.co.nz/2006/10/27/dynamic-image-resizing/
  6. which is itself an improvement of the original script from
  7. Timothy Crowe's 'veryraw' website, with caching additions added by Trent Davies:
  8. http://veryraw.com/history/2005/03/image-resizing-with-php/
  9. Thanks to:
  10. Tim Novinger for finding the image extension problem (fixed by an strtolower())
  11. ---------------------------------------------------------------- */
  12. $max_height = 1000;
  13. $max_width = 2000;
  14. if (isset($_GET["imgfile"]))
  15. {
  16. if (function_exists(get_magic_quotes_gpc) && get_magic_quotes_gpc())
  17. {
  18. $image = stripslashes( $_GET["imgfile"] );
  19. } else $image = $_GET["imgfile"];
  20. if (isset($_GET["max_width"])) { if($_GET["max_width"] < 2000) $max_width = $_GET["max_width"]; }
  21. if (isset($_GET["max_height"])) { if($_GET["max_height"] < 1000) $max_height = $_GET["max_height"]; }
  22. if (strrchr($image, '/')) {
  23. $filename = substr(strrchr($image, '/'), 1); // remove folder references
  24. } else {
  25. $filename = $image;
  26. }
  27. $size = getimagesize($image);
  28. $width = $size[0];
  29. $height = $size[1];
  30. // get the ratio needed
  31. $x_ratio = $max_width / $width;
  32. $y_ratio = $max_height / $height;
  33. // if image already meets criteria, load current values in
  34. // if not, use ratios to load new size info
  35. if (($width <= $max_width) && ($height <= $max_height) ) {
  36. $tn_width = $width;
  37. $tn_height = $height;
  38. } else if (($x_ratio * $height) < $max_height) {
  39. $tn_height = ceil($x_ratio * $height);
  40. $tn_width = $max_width;
  41. } else {
  42. $tn_width = ceil($y_ratio * $width);
  43. $tn_height = $max_height;
  44. }
  45. /* Caching additions by Trent Davies */
  46. // first check cache
  47. // cache must be world-readable
  48. $resized = 'cache/'.$tn_width.'x'.$tn_height.'-'.$filename;
  49. $imageModified = @filemtime($image);
  50. $thumbModified = @filemtime($resized);
  51. header("Content-type: image/jpeg");
  52. // if thumbnail is newer than image then output cached thumbnail and exit
  53. if($imageModified<$thumbModified) {
  54. header("Last-Modified: ".gmdate("D, d M Y H:i:s",$thumbModified)." GMT");
  55. readfile($resized);
  56. exit;
  57. }
  58. // read image
  59. $ext = strtolower(substr(strrchr($image, '.'), 1)); // get the file extension
  60. switch ($ext) {
  61. case 'jpg': // jpg
  62. $src = imagecreatefromjpeg($image) or notfound();
  63. break;
  64. case 'png': // png
  65. $src = imagecreatefrompng($image) or notfound();
  66. break;
  67. case 'gif': // gif
  68. $src = imagecreatefromgif($image) or notfound();
  69. break;
  70. default:
  71. notfound();
  72. }
  73. // set up canvas
  74. $dst = imagecreatetruecolor($tn_width,$tn_height);
  75. imageantialias ($dst, true);
  76. // copy resized image to new canvas
  77. imagecopyresampled ($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
  78. /* Sharpening adddition by Mike Harding */
  79. // sharpen the image (only available in PHP5.1)
  80. /*if (function_exists("imageconvolution")) {
  81. $matrix = array( array( -1, -1, -1 ),
  82. array( -1, 32, -1 ),
  83. array( -1, -1, -1 ) );
  84. $divisor = 24;
  85. $offset = 0;
  86. imageconvolution($dst, $matrix, $divisor, $offset);
  87. }*/
  88. // send the header and new image
  89. imagejpeg($dst, null, 90);
  90. imagejpeg($dst, $resized, 90); // write the thumbnail to cache as well...
  91. // clear out the resources
  92. imagedestroy($src);
  93. imagedestroy($dst);
  94. }
  95. ?>