Просмотр файла attach_mod/includes/functions_thumbs.php

Размер файла: 4.3Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. if ( !defined('IN_PHPBB') )
  7. {
  8. die("ERROR!!! THIS FILE PROTECTED. IF YOU SAW THIS REPORT, MEANS HACKERS HERE IS NOTHING TO DO ");
  9. }
  10.  
  11.  
  12. $imagick = '';
  13.  
  14. function get_img_size_format($width, $height)
  15. {
  16. $max_width = 400;
  17.  
  18. if ($width > $height)
  19. {
  20. return array(
  21. round($width * ($max_width / $width)),
  22. round($height * ($max_width / $width))
  23. );
  24. }
  25. else
  26. {
  27. return array(
  28. round($width * ($max_width / $height)),
  29. round($height * ($max_width / $height))
  30. );
  31. }
  32. }
  33.  
  34. function is_imagick()
  35. {
  36. global $imagick, $attach_config;
  37.  
  38. if ($attach_config['img_imagick'] != '')
  39. {
  40. $imagick = $attach_config['img_imagick'];
  41. return true;
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. }
  48.  
  49. function get_supported_image_types($type)
  50. {
  51. if (@extension_loaded('gd'))
  52. {
  53. $format = imagetypes();
  54. $new_type = 0;
  55.  
  56. switch ($type)
  57. {
  58. case 1:
  59. $new_type = ($format & IMG_GIF) ? IMG_GIF : 0;
  60. break;
  61. case 2:
  62. case 9:
  63. case 10:
  64. case 11:
  65. case 12:
  66. $new_type = ($format & IMG_JPG) ? IMG_JPG : 0;
  67. break;
  68. case 3:
  69. $new_type = ($format & IMG_PNG) ? IMG_PNG : 0;
  70. break;
  71. case 6:
  72. case 15:
  73. $new_type = ($format & IMG_WBMP) ? IMG_WBMP : 0;
  74. break;
  75. }
  76. return array(
  77. 'gd' => ($new_type) ? true : false,
  78. 'format' => $new_type,
  79. 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
  80. );
  81. }
  82.  
  83. return array('gd' => false);
  84. }
  85.  
  86. function create_thumbnail($source, $new_file, $mimetype)
  87. {
  88. global $attach_config, $imagick;
  89.  
  90. $source = amod_realpath($source);
  91. $min_filesize = (int) $attach_config['img_min_thumb_filesize'];
  92. $img_filesize = (@file_exists($source)) ? @filesize($source) : false;
  93.  
  94. if (!$img_filesize || $img_filesize <= $min_filesize)
  95. {
  96. return false;
  97. }
  98.  
  99. list($width, $height, $type, ) = getimagesize($source);
  100.  
  101. if (!$width || !$height)
  102. {
  103. return false;
  104. }
  105.  
  106. list($new_width, $new_height) = get_img_size_format($width, $height);
  107.  
  108. $tmp_path = $old_file = '';
  109.  
  110. if (intval($attach_config['allow_ftp_upload']))
  111. {
  112. $old_file = $new_file;
  113.  
  114. $tmp_path = explode('/', $source);
  115. $tmp_path[count($tmp_path)-1] = '';
  116. $tmp_path = implode('/', $tmp_path);
  117.  
  118. if ($tmp_path == '')
  119. {
  120. $tmp_path = '/tmp';
  121. }
  122.  
  123. $value = trim($tmp_path);
  124.  
  125. if ($value[strlen($value)-1] == '/')
  126. {
  127. $value[strlen($value)-1] = ' ';
  128. }
  129.  
  130. $new_file = tempnam(trim($value), 't00000');
  131.  
  132. @unlink($new_file);
  133. }
  134.  
  135. $used_imagick = false;
  136.  
  137. if (is_imagick())
  138. {
  139. passthru($imagick . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $new_file) . '"');
  140. if (@file_exists($new_file))
  141. {
  142. $used_imagick = true;
  143. }
  144. }
  145.  
  146. if (!$used_imagick)
  147. {
  148. $type = get_supported_image_types($type);
  149. if ($type['gd'])
  150. {
  151. switch ($type['format'])
  152. {
  153. case IMG_GIF:
  154. $image = imagecreatefromgif($source);
  155. break;
  156. case IMG_JPG:
  157. $image = imagecreatefromjpeg($source);
  158. break;
  159. case IMG_PNG:
  160. $image = imagecreatefrompng($source);
  161. break;
  162. case IMG_WBMP:
  163. $image = imagecreatefromwbmp($source);
  164. break;
  165. }
  166.  
  167. if ($type['version'] == 1 || !$attach_config['use_gd2'])
  168. {
  169. $new_image = imagecreate($new_width, $new_height);
  170. imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  171. }
  172. else
  173. {
  174. $new_image = imagecreatetruecolor($new_width, $new_height);
  175. imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  176. }
  177. switch ($type['format'])
  178. {
  179. case IMG_GIF:
  180. imagegif($new_image, $new_file);
  181. break;
  182. case IMG_JPG:
  183. imagejpeg($new_image, $new_file, 90);
  184. break;
  185. case IMG_PNG:
  186. imagepng($new_image, $new_file);
  187. break;
  188. case IMG_WBMP:
  189. imagewbmp($new_image, $new_file);
  190. break;
  191. }
  192.  
  193. imagedestroy($new_image);
  194. }
  195. }
  196.  
  197. if (!@file_exists($new_file))
  198. {
  199. return false;
  200. }
  201.  
  202. if (intval($attach_config['allow_ftp_upload']))
  203. {
  204. $result = ftp_file($new_file, $old_file, $mimetype, true);
  205. @unlink($new_file);
  206.  
  207. if (!$result)
  208. {
  209. return false;
  210. }
  211. }
  212. else
  213. {
  214. @chmod($new_file, 0664);
  215. }
  216. return true;
  217. }
  218.  
  219. ?>