PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/attach_mod/includes/functions_thumbs.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 387 lines | 288 code | 57 blank | 42 comment | 48 complexity | 4b135f2b41d0d9eb8880165f046b5043 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Extra credits for this file
  13. * (c) 2002 Meik Sievertsen (Acyd Burn)
  14. *
  15. */
  16. /**
  17. * All Attachment Functions needed to create Thumbnails
  18. */
  19. if (!defined('IN_ICYPHOENIX'))
  20. {
  21. die('Hacking attempt');
  22. exit;
  23. }
  24. $imagick = '';
  25. /**
  26. * Calculate the needed size for Thumbnail
  27. */
  28. function get_img_size_format($width, $height)
  29. {
  30. // Maximum Width the Image can take
  31. $max_width = 400;
  32. if ($width > $height)
  33. {
  34. return array(
  35. round($width * ($max_width / $width)),
  36. round($height * ($max_width / $width))
  37. );
  38. }
  39. else
  40. {
  41. return array(
  42. round($width * ($max_width / $height)),
  43. round($height * ($max_width / $height))
  44. );
  45. }
  46. }
  47. /**
  48. * Check if imagick is present
  49. */
  50. function is_imagick()
  51. {
  52. global $imagick, $config;
  53. if ($config['img_imagick'] != '')
  54. {
  55. $imagick = $config['img_imagick'];
  56. return true;
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. /**
  64. * Get supported image types
  65. */
  66. function get_supported_image_types($type)
  67. {
  68. if (@extension_loaded('gd'))
  69. {
  70. $format = imagetypes();
  71. $new_type = 0;
  72. switch ($type)
  73. {
  74. case 1:
  75. $new_type = ($format & IMG_GIF) ? IMG_GIF : 0;
  76. break;
  77. case 2:
  78. case 9:
  79. case 10:
  80. case 11:
  81. case 12:
  82. $new_type = ($format & IMG_JPG) ? IMG_JPG : 0;
  83. break;
  84. case 3:
  85. $new_type = ($format & IMG_PNG) ? IMG_PNG : 0;
  86. break;
  87. case 6:
  88. case 15:
  89. $new_type = ($format & IMG_WBMP) ? IMG_WBMP : 0;
  90. break;
  91. }
  92. return array(
  93. 'gd' => ($new_type) ? true : false,
  94. 'format' => $new_type,
  95. 'version' => (function_exists('imagecreatetruecolor')) ? 2 : 1
  96. );
  97. }
  98. return array('gd' => false);
  99. }
  100. /**
  101. * Thumbnail copy
  102. */
  103. function copy_thumbnail($source, $new_file, $mimetype)
  104. {
  105. global $config;
  106. if (intval($config['allow_ftp_upload']))
  107. {
  108. $result = ftp_file($new_file, $source, $mimetype, true); // True for disable error-mode
  109. if (!$result)
  110. {
  111. return false;
  112. }
  113. }
  114. else
  115. {
  116. $result = @copy($source, $new_file);
  117. @chmod($new_file, 0664);
  118. if (!$result)
  119. {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. /**
  126. * Create thumbnail
  127. */
  128. function create_thumbnail($source, $new_file, $mimetype)
  129. {
  130. global $config, $imagick;
  131. $source = amod_realpath($source);
  132. $min_filesize = (int) $config['img_min_thumb_filesize'];
  133. $img_filesize = (@file_exists($source)) ? @filesize($source) : false;
  134. if (!$img_filesize || ($img_filesize <= $min_filesize))
  135. {
  136. $result = false;
  137. if ($img_filesize <= $min_filesize)
  138. {
  139. $result = copy_thumbnail($source, $new_file, $mimetype);
  140. }
  141. return $result;
  142. }
  143. list($width, $height, $type, ) = getimagesize($source);
  144. if (!$width || !$height)
  145. {
  146. return false;
  147. }
  148. if (($width <= $config['img_link_width']) && ($height <= $config['img_link_height']))
  149. {
  150. $result = copy_thumbnail($source, $new_file, $mimetype);
  151. return $result;
  152. }
  153. list($new_width, $new_height) = get_img_size_format($width, $height);
  154. // If new w and h are larger than current image, just copy the image...
  155. if (($width <= $new_width) && ($height <= $new_height))
  156. {
  157. $result = copy_thumbnail($source, $new_file, $mimetype);
  158. return $result;
  159. }
  160. $tmp_path = $old_file = '';
  161. if (intval($config['allow_ftp_upload']))
  162. {
  163. $old_file = $new_file;
  164. $tmp_path = explode('/', $source);
  165. $tmp_path[sizeof($tmp_path) - 1] = '';
  166. $tmp_path = implode('/', $tmp_path);
  167. if ($tmp_path == '')
  168. {
  169. $tmp_path = '/tmp';
  170. }
  171. $value = trim($tmp_path);
  172. if ($value[strlen($value)-1] == '/')
  173. {
  174. $value[strlen($value)-1] = ' ';
  175. }
  176. //
  177. $new_file = tempnam(trim($value), 't00000');
  178. // We remove it now because it gets created again later
  179. @unlink($new_file);
  180. }
  181. $used_imagick = false;
  182. if (is_imagick())
  183. {
  184. passthru($imagick . ' -quality 85 -antialias -sample ' . $new_width . 'x' . $new_height . ' "' . str_replace('\\', '/', $source) . '" +profile "*" "' . str_replace('\\', '/', $new_file) . '"');
  185. if (@file_exists($new_file))
  186. {
  187. $used_imagick = true;
  188. }
  189. }
  190. if (!$used_imagick)
  191. {
  192. $type = get_supported_image_types($type);
  193. if ($type['gd'])
  194. {
  195. switch ($type['format'])
  196. {
  197. case IMG_GIF:
  198. $image = imagecreatefromgif($source);
  199. break;
  200. case IMG_JPG:
  201. $image = imagecreatefromjpeg($source);
  202. break;
  203. case IMG_PNG:
  204. $image = imagecreatefrompng($source);
  205. break;
  206. case IMG_WBMP:
  207. $image = imagecreatefromwbmp($source);
  208. break;
  209. }
  210. if (($type['version'] == 1) || !$config['use_gd2'])
  211. {
  212. $new_image = imagecreate($new_width, $new_height);
  213. imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  214. }
  215. else
  216. {
  217. $new_image = imagecreatetruecolor($new_width, $new_height);
  218. imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  219. }
  220. switch ($type['format'])
  221. {
  222. case IMG_GIF:
  223. imagegif($new_image, $new_file);
  224. break;
  225. case IMG_JPG:
  226. imagejpeg($new_image, $new_file, 90);
  227. break;
  228. case IMG_PNG:
  229. imagepng($new_image, $new_file);
  230. break;
  231. case IMG_WBMP:
  232. imagewbmp($new_image, $new_file);
  233. break;
  234. }
  235. imagedestroy($new_image);
  236. }
  237. }
  238. if (!@file_exists($new_file))
  239. {
  240. return false;
  241. }
  242. if (intval($config['allow_ftp_upload']))
  243. {
  244. $result = ftp_file($new_file, $old_file, $mimetype, true); // True for disable error-mode
  245. @unlink($new_file);
  246. if (!$result)
  247. {
  248. return false;
  249. }
  250. }
  251. else
  252. {
  253. @chmod($new_file, 0664);
  254. }
  255. return true;
  256. }
  257. /**
  258. * Check if Thumbnail exist
  259. */
  260. function thumbnail_exists($filename)
  261. {
  262. global $upload_dir, $config;
  263. $filename = basename($filename);
  264. if (!intval($config['allow_ftp_upload']))
  265. {
  266. if (!@file_exists(@amod_realpath($upload_dir . '/' . THUMB_DIR . '/t_' . $filename)))
  267. {
  268. return false;
  269. }
  270. else
  271. {
  272. return true;
  273. }
  274. }
  275. else
  276. {
  277. $found = false;
  278. $conn_id = attach_init_ftp(MODE_THUMBNAIL);
  279. $file_listing = array();
  280. $filename = 't_' . $filename;
  281. $file_listing = @ftp_rawlist($conn_id, $filename);
  282. for ($i = 0, $size = sizeof($file_listing); $i < $size; $i++)
  283. {
  284. if (preg_match("/([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)/", $file_listing[$i], $regs))
  285. {
  286. if ($regs[1] == 'd')
  287. {
  288. $dirinfo[0] = 1; // Directory == 1
  289. }
  290. $dirinfo[1] = $regs[2]; // Size
  291. $dirinfo[2] = $regs[3]; // Date
  292. $dirinfo[3] = $regs[4]; // Filename
  293. $dirinfo[4] = $regs[5]; // Time
  294. }
  295. if ($dirinfo[0] != 1 && $dirinfo[4] == $filename)
  296. {
  297. $found = true;
  298. }
  299. }
  300. @ftp_quit($conn_id);
  301. return $found;
  302. }
  303. }
  304. /**
  305. * Sync Thumbnail (if a thumbnail is no longer there, delete it)
  306. */
  307. function check_thumbnail($attachment_data, $upload_dir)
  308. {
  309. global $config, $user, $lang;
  310. if (!thumbnail_exists(basename($attachment_data['physical_filename'])))
  311. {
  312. if (!intval($config['allow_ftp_upload']))
  313. {
  314. $source_file = $upload_dir . '/' . basename($attachment_data['physical_filename']);
  315. $dest_file = @amod_realpath($upload_dir);
  316. $dest_file .= '/' . THUMB_DIR . '/t_' . basename($attachment_data['physical_filename']);
  317. }
  318. else
  319. {
  320. $source_file = $attachment_data['physical_filename'];
  321. $dest_file = THUMB_DIR . '/t_' . basename($attachment_data['physical_filename']);
  322. }
  323. if (create_thumbnail($source_file, $dest_file, $attachment_data['mimetype']))
  324. {
  325. return 1;
  326. }
  327. }
  328. return 0;
  329. }
  330. ?>