PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/e107_handlers/resize_handler.php

https://github.com/CasperGemini/e107
PHP | 275 lines | 211 code | 23 blank | 41 comment | 68 complexity | 4518e5e80874f3e543d295521f8e034e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2009 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. *
  10. *
  11. * $Source: /cvs_backup/e107_0.8/e107_handlers/resize_handler.php,v $
  12. * $Revision$
  13. * $Date$
  14. * $Author$
  15. */
  16. if (!defined('e107_INIT')) { exit; }
  17. // Given an image file name, return the mime type string. Returns FALSE if invalid
  18. function mimeFromFilename($fileName)
  19. {
  20. $fileExt = strtolower(substr(strrchr($fileName, "."), 1));
  21. $mimeTypes = array(
  22. 'jpg' => 'jpeg',
  23. 'gif' => 'gif',
  24. 'png' => 'png',
  25. 'jpeg' => 'jpeg',
  26. 'pjpeg' => 'jpeg',
  27. 'bmp' => 'bmp'
  28. );
  29. if (!isset($mimeTypes[$fileExt])) { return FALSE; } // only allow image files }
  30. return "Content-type: image/".$mimeTypes[$fileExt];
  31. }
  32. function resize_image($source_file, $destination_file, $type = "upload", $model = "")
  33. {
  34. // $destination_file - 'stdout' sends direct to browser. Otherwise treated as file name
  35. // - if its a file, given '644' permissions
  36. // $type - numeric - sets new width of image
  37. // - "upload" - uses preference 'im_width', or 400px if not defined
  38. // - anything else - default preference for image width & heightused, or failing that, 120 px x 100 px
  39. // 'avatar' may be used to invoke the default (see usersettings.php)
  40. // $model - "copy" - creates a new file for the destination, by prefixing $destination_file with 'thumb_'. Return error for small images.
  41. // - 'upsize' - small images are enlarged
  42. // - 'noscale' - small images are transferred at their original size
  43. // - 'nocopy' - used in content manager plugin
  44. // Otherwise overwrites any existing $destination_file
  45. // Returns: TRUE - essentially, if $destination_file (or a file with a modified name) is valid:
  46. // - if resizing done
  47. // - source and (ultimate) destination files are the same, and the image was smaller than the limits
  48. // - destination was 'stdout', and file output successfully
  49. // FALSE - essentially, if there is not a valid output file available - usually, that resizing failed, or some other error.
  50. global $pref;
  51. $new_height = 0;
  52. $mode = ($pref['resize_method'] ? $pref['resize_method'] : "gd2");
  53. if ($type == "upload")
  54. {
  55. $new_size = varset($pref['im_width'],400);
  56. }
  57. elseif(is_numeric($type))
  58. {
  59. $new_size = $type;
  60. }
  61. else
  62. { // Use preferences or failing that hard-coded defaults for new size
  63. $new_size = varset($pref['im_width'], 120);
  64. $new_height = varset($pref['im_height'], 100);
  65. }
  66. $im_quality = varset($pref['im_quality'], 99);
  67. $image_stats = getimagesize($source_file);
  68. if ($image_stats == null)
  69. {
  70. // echo "<b>DEBUG</b> image_stats are null<br />";
  71. return false;
  72. }
  73. if (($image_stats[0] == 0) || ($image_stats[1] == 0))
  74. {
  75. return FALSE; // Zero sized image - shouldn't happen
  76. }
  77. // Check the image type. '1'=GIF, '2'=jpeg, '3' = PNG
  78. if ($image_stats[2] != 1 && $image_stats[2] != 2 && $image_stats[2] != 3 && ($mode == 'gd1' || $mode == 'gd2'))
  79. {
  80. echo "<b>DEBUG</b> Wrong image type<br />";
  81. return FALSE;
  82. }
  83. $imagewidth = $image_stats[0]; // Width of existing image
  84. $imageheight = $image_stats[1]; // Height of existing image
  85. if ($imagewidth <= $new_size && ($imageheight <= $new_height || $new_height == 0))
  86. { // Nothing to do if image width already smaller than the maximum
  87. // If we were basically ensuring an existing file was within limits, return TRUE
  88. // If we had to create a new file, return FALSE since it wasn't done.
  89. switch ($model)
  90. {
  91. case 'copy' : // Not sure what to do here!
  92. return FALSE; // This is what it used to do
  93. break;
  94. case 'upsize' : // Scale source up to required size
  95. break; // Just fall through to do that.
  96. case 'noscale' : // No scaling of small images- just want destination to be the same as source
  97. if ($destination_file == 'stdout')
  98. {
  99. if (($result = mimeFromFilename($source_file)) === FALSE) { return FALSE; }
  100. header($result);
  101. if (@readfile($source_file) === FALSE) { return FALSE; }
  102. }
  103. else
  104. {
  105. return copy($source_file,$destination_file);
  106. }
  107. return TRUE;
  108. break;
  109. default :
  110. return ($source_file == $destination_file);
  111. }
  112. // return (($source_file == $destination_file) && ($model != 'copy'));
  113. }
  114. $ratio = ($imagewidth / $new_size);
  115. $new_imageheight = round($imageheight / $ratio);
  116. if (($new_height <= $new_imageheight) && $new_height > 0)
  117. {
  118. $ratio = $new_imageheight / $new_height;
  119. $new_imageheight = $new_height;
  120. $new_size = round($new_size / $ratio);
  121. }
  122. if (($destination_file != 'stdout') && ($model == 'copy'))
  123. {
  124. $destination_file = dirname($destination_file).'/thumb_'.basename($destination_file);
  125. }
  126. $returnError = 0; // Return value from some of the commands
  127. switch ($mode)
  128. {
  129. case "ImageMagick" :
  130. if ($destination_file == "stdout")
  131. { // if destination is stdout, output directly to the browser
  132. // $destination_file = "jpg:-";
  133. header("Content-type: image/jpeg");
  134. // Use double quotes instead of single to keep Bill happy
  135. passthru ($pref['im_path']."convert -quality ".$im_quality." -antialias -geometry ".$new_size."x".$new_imageheight." ".escapeshellarg($source_file)." \"jpg:-\"", $returnError);
  136. }
  137. else
  138. { // otherwise output to file
  139. // Use double quotes instead of single to keep Bill happy
  140. exec ($pref['im_path']."convert -quality ".$im_quality." -antialias -geometry ".$new_size."x".$new_imageheight." ".escapeshellarg($source_file)." \"".$destination_file."\"", $dummy, $returnError);
  141. }
  142. if ($returnError) echo "ImageMagick resize/output error: {$returnError}<br />";
  143. break;
  144. case 'gd1' :
  145. case 'gd2' :
  146. switch ($image_stats[2])
  147. {
  148. case IMAGETYPE_PNG : // 3 - PNG
  149. $src_img = @imagecreatefrompng($source_file);
  150. $fileExt = 'png';
  151. break;
  152. case IMAGETYPE_GIF : // 1 - GIF
  153. if (!function_exists('imagecreatefromgif')) return FALSE; // Some versions of GD library don't support GIF
  154. $src_img = @imagecreatefromgif($source_file);
  155. $fileExt = 'gif';
  156. break;
  157. case IMAGETYPE_JPEG : // 2 - Jpeg
  158. $src_img = @imagecreatefromjpeg($source_file);
  159. $fileExt = 'jpg';
  160. break;
  161. default :
  162. return FALSE; // Unsupported image type
  163. }
  164. if (!$src_img)
  165. {
  166. return FALSE;
  167. }
  168. // Only next line is different between gd1 and gd2
  169. if ($mode == 'gd1')
  170. {
  171. $dst_img = imagecreate($new_size, $new_imageheight); // Create blank image of correct size as target
  172. if (!imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_size, $new_imageheight, $imagewidth, $imageheight)) { $returnError = -4; }
  173. }
  174. else
  175. {
  176. $dst_img = imagecreatetruecolor($new_size, $new_imageheight);
  177. if (!imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_size, $new_imageheight, $imagewidth, $imageheight)) { $returnError = -5; }
  178. }
  179. if ($returnError)
  180. {
  181. echo "Resizing error (1): {$returnError}<br />";
  182. return FALSE;
  183. }
  184. // Now output or save the resized file
  185. $destName = $destination_file;
  186. if ($destination_file == "stdout")
  187. {
  188. $destName = '';
  189. if (($result = mimeFromFilename($source_file)) === FALSE)
  190. {
  191. $returnError = -6;
  192. }
  193. else
  194. {
  195. header($result);
  196. }
  197. }
  198. else
  199. {
  200. $fileExt = strtolower(substr(strrchr($destination_file, "."), 1));
  201. }
  202. if ($returnError == 0)
  203. { // We can output the image, or save it to a file
  204. switch ($fileExt)
  205. {
  206. case 'png' :
  207. if (!imagepng($dst_img, $destName, 6)) { $returnError = -1; } // Fix the quality for now
  208. $outputFunc = 'imagepng';
  209. break;
  210. case 'gif' :
  211. if (!imagegif($dst_img, $destName)) { $returnError = -1; }
  212. $outputFunc = 'imagegif';
  213. break;
  214. case 'jpg' :
  215. case 'jpeg' :
  216. if (!imagejpeg($dst_img, $destName, $im_quality)) { $returnError = -1; }
  217. $outputFunc = 'imagejpeg';
  218. break;
  219. default :
  220. $returnError = -7; // Invalid output extension
  221. $outputFunc = 'none';
  222. }
  223. }
  224. if (!imagedestroy($src_img)) { $returnError = -2; }
  225. if (!imagedestroy($dst_img)) { $returnError = -3; }
  226. if ($returnError)
  227. {
  228. echo "Resizing error (2): {$returnError} - {$outputFunc} -> {$destName}<br />";
  229. return FALSE;
  230. }
  231. break;
  232. default :
  233. echo "Invalid resize function: {$mode}<br />";
  234. return FALSE;
  235. } // End switch($mode)
  236. if ($destination_file == "stdout") return TRUE; // Can't do anything more if file sent to stdout - assume success
  237. @chmod($destination_file, 0644);
  238. if ($pref['image_owner'])
  239. {
  240. @chown($destination_file, $pref['image_owner']);
  241. }
  242. $image_stats = getimagesize($destination_file);
  243. if ($image_stats == null)
  244. {
  245. return FALSE;
  246. }
  247. else
  248. {
  249. return TRUE;
  250. }
  251. }
  252. ?>