PageRenderTime 27ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/Upload/inc/functions_image.php

https://gitlab.com/mybbpl/ppm-1.6
PHP | 257 lines | 198 code | 18 blank | 41 comment | 39 complexity | 3d8429ea4ae8b3e3b2650ec7987f227b MD5 | raw file
  1. <?php
  2. /**
  3. * MyBB 1.6
  4. * Copyright 2010 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://mybb.com
  7. * License: http://mybb.com/about/license
  8. *
  9. * $Id$
  10. */
  11. /**
  12. * Generates a thumbnail based on specified dimensions (supports png, jpg, and gif)
  13. *
  14. * @param string the full path to the original image
  15. * @param string the directory path to where to save the new image
  16. * @param string the filename to save the new image as
  17. * @param integer maximum hight dimension
  18. * @param integer maximum width dimension
  19. * @return array thumbnail on success, error code 4 on failure
  20. */
  21. function generate_thumbnail($file, $path, $filename, $maxheight, $maxwidth)
  22. {
  23. if(!function_exists("imagecreate"))
  24. {
  25. $thumb['code'] = 3;
  26. return $thumb;
  27. }
  28. $imgdesc = getimagesize($file);
  29. $imgwidth = $imgdesc[0];
  30. $imgheight = $imgdesc[1];
  31. $imgtype = $imgdesc[2];
  32. $imgattr = $imgdesc[3];
  33. $imgbits = $imgdesc['bits'];
  34. $imgchan = $imgdesc['channels'];
  35. if($imgwidth == 0 || $imgheight == 0)
  36. {
  37. $thumb['code'] = 3;
  38. return $thumb;
  39. }
  40. if(($imgwidth >= $maxwidth) || ($imgheight >= $maxheight))
  41. {
  42. check_thumbnail_memory($imgwidth, $imgheight, $imgtype, $imgbits, $imgchan);
  43. if($imgtype == 3)
  44. {
  45. if(@function_exists("imagecreatefrompng"))
  46. {
  47. $im = @imagecreatefrompng($file);
  48. }
  49. }
  50. elseif($imgtype == 2)
  51. {
  52. if(@function_exists("imagecreatefromjpeg"))
  53. {
  54. $im = @imagecreatefromjpeg($file);
  55. }
  56. }
  57. elseif($imgtype == 1)
  58. {
  59. if(@function_exists("imagecreatefromgif"))
  60. {
  61. $im = @imagecreatefromgif($file);
  62. }
  63. }
  64. else
  65. {
  66. $thumb['code'] = 3;
  67. return $thumb;
  68. }
  69. if(!$im)
  70. {
  71. $thumb['code'] = 3;
  72. return $thumb;
  73. }
  74. $scale = scale_image($imgwidth, $imgheight, $maxwidth, $maxheight);
  75. $thumbwidth = $scale['width'];
  76. $thumbheight = $scale['height'];
  77. $thumbim = @imagecreatetruecolor($thumbwidth, $thumbheight);
  78. if(!$thumbim)
  79. {
  80. $thumbim = @imagecreate($thumbwidth, $thumbheight);
  81. $resized = true;
  82. }
  83. // Attempt to preserve the transparency if there is any
  84. if($imgtype == 3)
  85. {
  86. // A PNG!
  87. imagealphablending($thumbim, false);
  88. imagefill($thumbim, 0, 0, imagecolorallocatealpha($thumbim, 0, 0, 0, 127));
  89. // Save Alpha...
  90. imagesavealpha($thumbim, true);
  91. }
  92. elseif($imgtype == 1)
  93. {
  94. // Transparent GIF?
  95. $trans_color = imagecolortransparent($im);
  96. if($trans_color >= 0 && $trans_color < imagecolorstotal($im))
  97. {
  98. $trans = imagecolorsforindex($im, $trans_color);
  99. $new_trans_color = imagecolorallocate($thumbim, $trans['red'], $trans['blue'], $trans['green']);
  100. imagefill($thumbim, 0, 0, $new_trans_color);
  101. imagecolortransparent($thumbim, $new_trans_color);
  102. }
  103. }
  104. if(!isset($resized))
  105. {
  106. @imagecopyresampled($thumbim, $im, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imgwidth, $imgheight);
  107. }
  108. else
  109. {
  110. @imagecopyresized($thumbim, $im, 0, 0, 0, 0, $thumbwidth, $thumbheight, $imgwidth, $imgheight);
  111. }
  112. @imagedestroy($im);
  113. if(!function_exists("imagegif") && $imgtype == 1)
  114. {
  115. $filename = str_replace(".gif", ".jpg", $filename);
  116. }
  117. switch($imgtype)
  118. {
  119. case 1:
  120. if(function_exists("imagegif"))
  121. {
  122. @imagegif($thumbim, $path."/".$filename);
  123. }
  124. else
  125. {
  126. @imagejpeg($thumbim, $path."/".$filename);
  127. }
  128. break;
  129. case 2:
  130. @imagejpeg($thumbim, $path."/".$filename);
  131. break;
  132. case 3:
  133. @imagepng($thumbim, $path."/".$filename);
  134. break;
  135. }
  136. @my_chmod($path."/".$filename, '0644');
  137. @imagedestroy($thumbim);
  138. $thumb['code'] = 1;
  139. $thumb['filename'] = $filename;
  140. return $thumb;
  141. }
  142. else
  143. {
  144. return array("code" => 4);
  145. }
  146. }
  147. /**
  148. * Attempts to allocate enough memory to generate the thumbnail
  149. *
  150. * @param integer hight dimension
  151. * @param integer width dimension
  152. * @param string one of the IMAGETYPE_XXX constants indicating the type of the image
  153. * @param string the bits area the number of bits for each color
  154. * @param string the channels - 3 for RGB pictures and 4 for CMYK pictures
  155. */
  156. function check_thumbnail_memory($width, $height, $type, $bitdepth, $channels)
  157. {
  158. if(!function_exists("memory_get_usage"))
  159. {
  160. return false;
  161. }
  162. $memory_limit = @ini_get("memory_limit");
  163. if(!$memory_limit || $memory_limit == -1)
  164. {
  165. return false;
  166. }
  167. $limit = preg_match("#^([0-9]+)\s?([kmg])b?$#i", trim(my_strtolower($memory_limit)), $matches);
  168. $memory_limit = 0;
  169. if($matches[1] && $matches[2])
  170. {
  171. switch($matches[2])
  172. {
  173. case "k":
  174. $memory_limit = $matches[1] * 1024;
  175. break;
  176. case "m":
  177. $memory_limit = $matches[1] * 1048576;
  178. break;
  179. case "g":
  180. $memory_limit = $matches[1] * 1073741824;
  181. }
  182. }
  183. $current_usage = memory_get_usage();
  184. $free_memory = $memory_limit - $current_usage;
  185. $thumbnail_memory = round(($width * $height * $bitdepth * $channels / 8) * 5);
  186. $thumbnail_memory += 2097152;
  187. if($thumbnail_memory > $free_memory)
  188. {
  189. if($matches[1] && $matches[2])
  190. {
  191. switch($matches[2])
  192. {
  193. case "k":
  194. $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1024))."K";
  195. break;
  196. case "m":
  197. $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1048576))."M";
  198. break;
  199. case "g":
  200. $memory_limit = ceil((($memory_limit+$thumbnail_memory) / 1073741824))."G";
  201. }
  202. }
  203. @ini_set("memory_limit", $memory_limit);
  204. }
  205. }
  206. /**
  207. * Figures out the correct dimensions to use
  208. *
  209. * @param integer current hight dimension
  210. * @param integer current width dimension
  211. * @param integer max hight dimension
  212. * @param integer max width dimension
  213. * @return array correct height & width
  214. */
  215. function scale_image($width, $height, $maxwidth, $maxheight)
  216. {
  217. $width = intval($width);
  218. $height = intval($height);
  219. if(!$width) $width = $maxwidth;
  220. if(!$height) $height = $maxheight;
  221. $newwidth = $width;
  222. $newheight = $height;
  223. if($width > $maxwidth)
  224. {
  225. $newwidth = $maxwidth;
  226. $newheight = ceil(($height*(($maxwidth*100)/$width))/100);
  227. $height = $newheight;
  228. $width = $newwidth;
  229. }
  230. if($height > $maxheight)
  231. {
  232. $newheight = $maxheight;
  233. $newwidth = ceil(($width*(($maxheight*100)/$height))/100);
  234. }
  235. $ret['width'] = $newwidth;
  236. $ret['height'] = $newheight;
  237. return $ret;
  238. }
  239. ?>