KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thumb.php 6.6KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. require_once "common.php";
  3. if (ini_get("memory_limit") < 48)
  4. ini_set("memory_limit", "48M");
  5. if (!function_exists("gd_info"))
  6. exit("GD not installed; image cannot be resized.");
  7. $gd_info = gd_info();
  8. $gd_version = (substr_count(strtolower($gd_info["GD Version"]), "2.")) ? 2 : 1 ;
  9. $quality = fallback($_GET["quality"], 80);
  10. $filename = rtrim(fallback($_GET['file']));
  11. $extension = pathinfo($filename, PATHINFO_EXTENSION);
  12. if (!file_exists($filename))
  13. display_error("Image Not Found");
  14. function display_error($string) {
  15. $thumbnail = imagecreatetruecolor(oneof(@$_GET['max_width'], 100), 18);
  16. imagestring($thumbnail, 1, 5, 5, $string, imagecolorallocate($thumbnail, 255, 255, 255));
  17. header("Content-type: image/png");
  18. header("Content-Disposition: inline; filename=error.png");
  19. imagepng($thumbnail);
  20. exit;
  21. }
  22. list($original_width, $original_height, $type, $attr) = getimagesize($filename);
  23. $new_width = (int) fallback($_GET["max_width"], 0);
  24. $new_height = (int) fallback($_GET["max_height"], 0);
  25. $crop_x = 0;
  26. $crop_y = 0;
  27. function resize(&$crop_x, &$crop_y, &$new_width, &$new_height, $original_width, $original_height) {
  28. $xscale = ($new_width / $original_width);
  29. $yscale = $new_height / $original_height;
  30. if ($new_width <= $original_width and $new_height <= $original_height and $xscale == $yscale)
  31. return;
  32. if ( isset($_GET['square']) ) {
  33. if ( $new_width === 0 )
  34. $new_width = $new_height;
  35. if ( $new_height === 0 )
  36. $new_height = $new_width;
  37. if($original_width > $original_height) {
  38. # portrait
  39. $crop_x = ceil( ($original_width - $original_height) / 2 );
  40. } else if ($original_height > $original_width) {
  41. # landscape
  42. $crop_y = ceil( ($original_height - $original_width) / 2 );
  43. }
  44. return;
  45. } else {
  46. if ($new_width and !$new_height)
  47. return $new_height = ($new_width / $original_width) * $original_height;
  48. elseif (!$new_width and $new_height)
  49. return $new_width = ($new_height / $original_height) * $original_width;
  50. if ($xscale != $yscale) {
  51. if ($original_width * $yscale <= $new_width)
  52. $new_width = $original_width * $yscale;
  53. if ($original_height * $xscale <= $new_height)
  54. $new_height = $original_height * $xscale;
  55. }
  56. $xscale = ($new_width / $original_width);
  57. $yscale = $new_height / $original_height;
  58. if (round($xscale, 3) == round($yscale, 3))
  59. return;
  60. resize($crop_x, $crop_y, $new_width, $new_height, $original_width, $original_height);
  61. }
  62. }
  63. # Determine the final scale of the thumbnail.
  64. resize($crop_x, $crop_y, $new_width, $new_height, $original_width, $original_height);
  65. # If it's already below the maximum, just redirect to it.
  66. if ($original_width <= $new_width and $original_height <= $new_height)
  67. header("Location: ".$filename);
  68. $cache_filename = md5($filename.$new_width.$new_height.$quality).".".$extension;
  69. $cache_file = INCLUDES_DIR."/caches/thumb_".$cache_filename;
  70. if (isset($_GET['no_cache']) and $_GET['no_cache'] == "true" and file_exists($cache_file))
  71. unlink($cache_file);
  72. # Serve a cache if it exists and the original image has not changed.
  73. if (file_exists($cache_file) and filemtime($cache_file) > filemtime($filename)) {
  74. header("Last-Modified: ".gmdate('D, d M Y H:i:s', filemtime($cache_file)).' GMT');
  75. header("Content-type: image/".($extension == "jpg" ? "jpeg" : $extension));
  76. header("Cache-Control: public");
  77. header("Expires: ".date("r", strtotime("+30 days")));
  78. header("Content-Disposition: inline; filename=".$cache_filename);
  79. readfile($cache_file);
  80. exit;
  81. }
  82. # Verify that the image is able to be thumbnailed, and prepare variables used later in the script.
  83. switch ($type) {
  84. case IMAGETYPE_GIF:
  85. $image = imagecreatefromgif($filename);
  86. $done = (function_exists("imagegif")) ? "imagegif" : "imagejpeg" ;
  87. $mime = (function_exists("imagegif")) ? "image/gif" : "image/jpeg" ;
  88. break;
  89. case IMAGETYPE_JPEG:
  90. $image = imagecreatefromjpeg($filename);
  91. $done = "imagejpeg";
  92. $mime = "image/jpeg";
  93. break;
  94. case IMAGETYPE_PNG:
  95. $image = imagecreatefrompng($filename);
  96. $done = "imagepng";
  97. $mime = "image/png";
  98. break;
  99. case IMAGETYPE_BMP:
  100. $image = imagecreatefromwbmp($filename);
  101. $done = "imagewbmp";
  102. $mime = "image/bmp";
  103. break;
  104. default:
  105. display_error("Unsupported image type.");
  106. break;
  107. }
  108. if (!$image)
  109. display_error("Image could not be created.");
  110. # Decide what functions to use.
  111. $create = ($gd_version == 2) ? "imagecreatetruecolor" : "imagecreate" ;
  112. $copy = ($gd_version == 2 and $original_width < 4096) ? "imagecopyresampled" : "imagecopyresized" ;
  113. # Create the final resized image.
  114. $thumbnail = $create($new_width, $new_height);
  115. if ($done == "imagepng")
  116. imagealphablending($thumbnail, false);
  117. # if square crop is desired, original dimensions need to be set to square ratio
  118. if ( isset($_GET['square']) ) {
  119. if ($original_width > $original_height) {
  120. $original_width = $original_height;
  121. } else if ($original_height > $original_width) {
  122. $original_height = $original_width;
  123. }
  124. }
  125. $copy($thumbnail, $image, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $original_width, $original_height);
  126. header("Last-Modified: ".gmdate("D, d M Y H:i:s", filemtime($filename))." GMT");
  127. header("Content-type: ".$mime);
  128. header("Content-Disposition: inline; filename=".$cache_filename.".".$extension);
  129. if ($done == "imagepng")
  130. imagesavealpha($thumbnail, true);
  131. # Generate the cache image.
  132. if (!isset($_GET['no_cache']) or $_GET['no_cache'] == "false")
  133. if ($done == "imagejpeg")
  134. $done($thumbnail, $cache_file, $quality);
  135. else
  136. $done($thumbnail, $cache_file);
  137. # Serve the image.
  138. if ($done == "imagejpeg")
  139. $done($thumbnail, "", $quality);
  140. else
  141. $done($thumbnail);
  142. # Clear memory.
  143. imagedestroy($image);
  144. imagedestroy($thumbnail);