PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/products/pics5/images.php

https://github.com/EmranAhmed/wp-easycart
PHP | 114 lines | 63 code | 18 blank | 33 comment | 10 complexity | fa0781a3de30e1b48cfae2738d163cdf MD5 | raw file
  1. <?php
  2. /* ----------------------------------------------------------------
  3. DYNAMIC IMAGE RESIZING SCRIPT - V2
  4. The following script will take an existing JPG image, and resize it
  5. using set options defined in your .htaccess file (while also providing
  6. a nice clean URL to use when referencing the images)
  7. Images will be cached, to reduce overhead, and will be updated only if
  8. the image is newer than it's cached version.
  9. The original script is from Timothy Crowe's 'veryraw' website, with
  10. caching additions added by Trent Davies:
  11. http://veryraw.com/history/2005/03/image-resizing-with-php/
  12. Further modifications to include antialiasing, sharpening, gif & png
  13. support, plus folder structues for image paths, added by Mike Harding
  14. http://sneak.co.nz
  15. For instructions on use, head to http://sneak.co.nz
  16. ---------------------------------------------------------------- */
  17. // max_width and image variables are sent by htaccess
  18. $max_height = 1000;
  19. $image = $_GET["imgfile"];
  20. $max_width = $_GET["max_width"];
  21. if (strrchr($image, '/')) {
  22. $filename = substr(strrchr($image, '/'), 1); // remove folder references
  23. } else {
  24. $filename = $image;
  25. }
  26. $size = getimagesize($image);
  27. $width = $size[0];
  28. $height = $size[1];
  29. // get the ratio needed
  30. $x_ratio = $max_width / $width;
  31. $y_ratio = $max_height / $height;
  32. // if image already meets criteria, load current values in
  33. // if not, use ratios to load new size info
  34. if (($width <= $max_width) && ($height <= $max_height) ) {
  35. $tn_width = $width;
  36. $tn_height = $height;
  37. } else if (($x_ratio * $height) < $max_height) {
  38. $tn_height = ceil($x_ratio * $height);
  39. $tn_width = $max_width;
  40. } else {
  41. $tn_width = ceil($y_ratio * $width);
  42. $tn_height = $max_height;
  43. }
  44. /* Caching additions by Trent Davies */
  45. // first check cache
  46. // cache must be world-readable
  47. $resized = 'cache/'.$tn_width.'x'.$tn_height.'-'.$filename;
  48. $imageModified = @filemtime($image);
  49. $thumbModified = @filemtime($resized);
  50. header("Content-type: image/jpeg");
  51. // if thumbnail is newer than image then output cached thumbnail and exit
  52. if($imageModified<$thumbModified) {
  53. header("Last-Modified: ".gmdate("D, d M Y H:i:s",$thumbModified)." GMT");
  54. readfile($resized);
  55. exit;
  56. }
  57. // read image
  58. $ext = substr(strrchr($image, '.'), 1); // get the file extension
  59. switch ($ext) {
  60. case 'jpg': // jpg
  61. $src = imagecreatefromjpeg($image) or notfound();
  62. break;
  63. case 'png': // png
  64. $src = imagecreatefrompng($image) or notfound();
  65. break;
  66. case 'gif': // gif
  67. $src = imagecreatefromgif($image) or notfound();
  68. break;
  69. default:
  70. notfound();
  71. }
  72. // set up canvas
  73. $dst = imagecreatetruecolor($tn_width,$tn_height);
  74. imageantialias ($dst, true);
  75. // copy resized image to new canvas
  76. imagecopyresampled ($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
  77. /* Sharpening adddition by Mike Harding */
  78. // sharpen the image (only available in PHP5.1)
  79. if (function_exists("imageconvolution")) {
  80. $matrix = array( array( -1, -1, -1 ),
  81. array( -1, 32, -1 ),
  82. array( -1, -1, -1 ) );
  83. $divisor = 24;
  84. $offset = 0;
  85. imageconvolution($dst, $matrix, $divisor, $offset);
  86. }
  87. // send the header and new image
  88. imagejpeg($dst, null, -1);
  89. imagejpeg($dst, $resized, -1); // write the thumbnail to cache as well...
  90. // clear out the resources
  91. imagedestroy($src);
  92. imagedestroy($dst);
  93. ?>