PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/thumbnail_lib.php

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