/addons/DynamicMTML.pack/php/thumbnail_lib_alt.php

https://github.com/GunioRobot/DynamicMTML · PHP · 354 lines · 280 code · 43 blank · 31 comment · 61 complexity · eadc1a0bcd3725209bfa916da1f1cae4 MD5 · raw file

  1. <?php
  2. require_once( 'class.exception.php' );
  3. class Thumbnail_alt {
  4. private $src_file;
  5. private $src_w;
  6. private $src_h;
  7. private $src_type;
  8. private $src_img;
  9. private $src_attr;
  10. private $dest_img;
  11. private $dest_w;
  12. private $dest_h;
  13. private $dest_scale;
  14. private $dest_square;
  15. private $dest_id;
  16. private $dest_format;
  17. private $dest_type;
  18. private $dest_file;
  19. # construct
  20. public function __construct ($src = null) {
  21. if (!$this->is_available())
  22. throw new MTExtensionNotFoundException('GD');
  23. $this->src_file = null;
  24. $this->src_w = 0;
  25. $this->src_h = 0;
  26. $this->src_type = null;
  27. $this->src_img = null;
  28. $this->src_attr = null;
  29. $this->dest_w = 0;
  30. $this->dest_h = 0;
  31. $this->dest_scale = 0;
  32. $this->dest_square = false;
  33. $this->dest_id = null;
  34. $this->dest_format = '%f-thumb-%wx%h-%i%x';
  35. $this->dest_type = 'auto';
  36. $this->dest_img = null;
  37. $this->dest_file = '';
  38. if (!is_null($src))
  39. $this->src_file = $src;
  40. }
  41. # destruct
  42. public function __destruct () {
  43. if (is_resource($this->src_img))
  44. @imagedestroy($this->src_img);
  45. if (is_resource($this->dest_img))
  46. @imagedestroy($this->dest_img);
  47. }
  48. # property accessor
  49. public function src_file($src = null) {
  50. if (!is_null($src))
  51. $this->src_file = $src;
  52. $src_file = $this->src_file;
  53. if (strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring')) {
  54. // Changes character-set of filename to SJIS on Windows.
  55. $src_file = mb_convert_encoding($src_file, "SJIS", "auto");
  56. }
  57. return $src_file;
  58. }
  59. public function dest_file($dest = null) {
  60. if (!is_null($dest))
  61. $this->dest_file = $dest;
  62. $dest_file = $this->dest_file;
  63. if (strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring')) {
  64. // Changes character-set of filename to SJIS on Windows.
  65. $src_file = mb_convert_encoding($dest_file, "SJIS", "auto");
  66. }
  67. return $dest_file;
  68. }
  69. public function width($w = null) {
  70. if (!is_null($w))
  71. $this->dest_w = $w;
  72. return $this->dest_w;
  73. }
  74. public function height($h = null) {
  75. if (!is_null($h))
  76. $this->dest_h = $h;
  77. return $this->dest_h;
  78. }
  79. public function scale($scale = null) {
  80. if (!is_null($scale))
  81. $this->dest_scale = $scale;
  82. return $this->dest_scale;
  83. }
  84. public function square($square = null) {
  85. if (!is_null($square))
  86. $this->dest_square = $square;
  87. return $this->dest_square;
  88. }
  89. public function id($id = null) {
  90. if (!is_null($id))
  91. $this->dest_id = $id;
  92. return $this->dest_id;
  93. }
  94. public function format($fmt = null) {
  95. if (!is_null($fmt))
  96. $this->dest_format = $fmt;
  97. return $this->dest_format;
  98. }
  99. public function type($type = null) {
  100. if (!is_null($type))
  101. $this->dest_type = $type;
  102. return $this->dest_type;
  103. }
  104. public function dest() {
  105. $dest_file = $this->dest_file();
  106. if ( strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring') ) {
  107. // Changes character-set of filename to 'UTF-8' on Windows.
  108. $dest_file = mb_convert_encoding($dest_file, mb_internal_encoding(), "auto");
  109. }
  110. return $dest_file;
  111. }
  112. private function load_image () {
  113. $src_file = $this->src_file();
  114. if (empty($src_file)) return false;
  115. if (!file_exists($src_file)) return false;
  116. # Get source image information
  117. list($this->src_w, $this->src_h, $this->src_type, $this->src_attr) = getimagesize($src_file);
  118. switch($this->src_type) {
  119. case 1: #GIF
  120. $this->src_img = @imagecreatefromgif($src_file);
  121. break;
  122. case 2: #JPEG
  123. $this->src_img = @imagecreatefromjpeg($src_file);
  124. break;
  125. case 3: #PNG
  126. $this->src_img = @imagecreatefrompng($src_file);
  127. break;
  128. default: #Unsupported format
  129. throw new MTUnsupportedImageTypeException($src_file);
  130. }
  131. if (empty($this->src_img)) return false;
  132. return true;
  133. }
  134. # Can we use function of GD?
  135. public function is_available () {
  136. return extension_loaded('gd');
  137. }
  138. # Calculate image size
  139. # This function returns array object.
  140. # [0] thumbnail width
  141. # [1] thumbnail height
  142. # [2] thumbnail width for file name
  143. # [3] thumbnail width for file name
  144. private function _calculate_size () {
  145. # Calculate thumbnail size
  146. $thumb_w = $this->src_w;
  147. $thumb_h = $this->src_h;
  148. $thumb_w_name = $this->src_w;
  149. $thumb_h_name = $this->src_h;
  150. if ($this->dest_scale > 0) {
  151. $thumb_w = intval($this->src_w * $this->dest_scale / 100);
  152. $thumb_h = intval($this->src_h * $this->dest_scale / 100);
  153. $thumb_w_name = $thumb_w;
  154. $thumb_h_name = $thumb_h;
  155. } elseif ($this->dest_square) {
  156. if ($this->dest_w > 0) {
  157. $thumb_w = $this->dest_w;
  158. $thumb_h = $this->dest_w;
  159. $thumb_w_name = $this->dest_w;
  160. $thumb_h_name = $this->dest_w;
  161. } else {
  162. $thumb_w = $this->dest_h;
  163. $thumb_h = $this->dest_h;
  164. $thumb_w_name = $this->dest_h;
  165. $thumb_h_name = $this->dest_h;
  166. }
  167. } elseif ($this->dest_w > 0 || $this->dest_h > 0) {
  168. $thumb_w_name = 'auto';
  169. $thumb_h_name = 'auto';
  170. $x = $this->dest_w; if ($this->dest_w > 0) $thumb_w;
  171. $y = $this->dest_h; if ($this->dest_h > 0) $thumb_h;
  172. $pct = $this->dest_w > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
  173. $thumb_w = (int)($thumb_w * $pct);
  174. $thumb_h = (int)($thumb_h * $pct);
  175. if ($this->dest_w > 0) $thumb_w_name = $this->dest_w;
  176. if ($this->dest_h > 0) $thumb_h_name = $this->dest_h;
  177. }
  178. return array($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name);
  179. }
  180. private function _make_dest_name ($w, $h) {
  181. if ($this->dest_type == 'auto') {
  182. $output = $this->src_type;
  183. } elseif (strtolower($this->dest_type) == 'gif') {
  184. $output = 1;
  185. } elseif (strtolower($this->dest_type) == 'jpeg') {
  186. $output = 2;
  187. } elseif (strtolower($this->dest_type) == 'png') {
  188. $output = 3;
  189. } else {
  190. $output = $this->src_type;
  191. }
  192. switch($output) {
  193. case 1:
  194. $ext = '.gif';
  195. break;
  196. case 2:
  197. $ext = '.jpg';
  198. break;
  199. case 3:
  200. $ext = '.png';
  201. break;
  202. default:
  203. $ext = image_type_to_extension($output);
  204. }
  205. $pathinfo = pathinfo($this->src_file());
  206. $basename = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
  207. $patterns[0] = '/%w/';
  208. $patterns[1] = '/%h/';
  209. $patterns[2] = '/%f/';
  210. $patterns[3] = '/%i/';
  211. $patterns[4] = '/%x/';
  212. $replacement[0] = $w;
  213. $replacement[1] = $h;
  214. $replacement[2] = $basename;
  215. $replacement[3] = $this->dest_id;
  216. $replacement[4] = $ext;
  217. return preg_replace($patterns, $replacement, $this->dest_format);
  218. }
  219. # Load or generate a thumbnail.
  220. public function get_thumbnail ($args = null) {
  221. # Parse args
  222. if (!empty($args)) {
  223. if (!empty($args['dest']))
  224. $this->dest_file = $args['dest'];
  225. if (!empty($args['width']))
  226. $this->dest_w = $args['width'];
  227. if (!empty($args['height']))
  228. $this->dest_h = $args['height'];
  229. if (!empty($args['id']))
  230. $this->dest_id = $args['id'];
  231. if (!empty($args['scale']))
  232. $this->dest_scale = $args['scale'];
  233. if (!empty($args['format']))
  234. $this->dest_format = $args['format'];
  235. if (!empty($args['dest_type']))
  236. $this->dest_type = $args['dest_type'];
  237. if (!empty($args['square']))
  238. $this->dest_square = $args['square'];
  239. }
  240. # Load source image
  241. if (!$this->load_image()) return false;
  242. # Calculate thumbnail size
  243. list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size();
  244. $this->dest_w = $thumb_w;
  245. $this->dest_h = $thumb_h;
  246. # Decide a destination file name
  247. if (empty($this->dest_file)) {
  248. $this->dest_file($this->_make_dest_name($thumb_w_name, $thumb_h_name));
  249. }
  250. # Generate
  251. $dest_file = $this->dest_file();
  252. if(!file_exists($dest_file)) {
  253. $dir_name = dirname($dest_file);
  254. if (!file_exists($dir_name))
  255. mkpath($dir_name, 0777);
  256. if (!is_writable($dir_name)) {
  257. return false;
  258. }
  259. # if square modifier is enable, crop & resize
  260. $src_x = 0;
  261. $src_y = 0;
  262. $target_w = $this->src_w;
  263. $target_h = $this->src_h;
  264. if ($this->dest_square) {
  265. if ($this->src_w > $this->src_h) {
  266. $src_x = (int)($this->src_w - $this->src_h) / 2;
  267. $src_y = 0;
  268. $target_w = $this->src_h;
  269. } else {
  270. $src_x = 0;
  271. $src_y = (int)($this->src_h - $this->src_w) / 2;
  272. $target_h = $this->src_w;
  273. }
  274. }
  275. # Create thumbnail
  276. $this->dest_img = imagecreatetruecolor ( $thumb_w, $thumb_h );
  277. $result = imagecopyresampled ( $this->dest_img, $this->src_img, 0, 0, $src_x, $src_y,
  278. $thumb_w, $thumb_h, $target_w, $target_h);
  279. $output = $this->src_type;
  280. // begin patch
  281. if ($this->dest_type != 'auto') {
  282. $output = strtolower($this->dest_type) == 'gif' ? 1
  283. : (strtolower($this->dest_type) == 'jpeg' ? 2
  284. : (strtolower($this->dest_type) == 'png' ? 3
  285. : $this->src_type));
  286. }
  287. // end patch
  288. // if ($this->dest_type != 'auto') {
  289. // $output = strtolower($this->dest_type) == 'gif' ? 1
  290. // : strtolower($this->dest_type) == 'jpeg' ? 2
  291. // : strtolower($this->dest_type) == 'png' ? 3
  292. // : $this->src_type;
  293. // }
  294. switch($output) {
  295. case 1: #GIF
  296. imagegif($this->dest_img, $dest_file);
  297. break;
  298. case 2: #JPEG
  299. imagejpeg($this->dest_img, $dest_file);
  300. break;
  301. case 3: #PNG
  302. imagepng($this->dest_img, $dest_file);
  303. break;
  304. }
  305. @imagedestroy($this->dest_img);
  306. }
  307. @imagedestroy($this->src_img);
  308. return true;
  309. }
  310. }
  311. ?>