PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/php4/thumbnail_v1/class.Thumbnail.inc.php

http://flaimo-php.googlecode.com/
PHP | 464 lines | 179 code | 39 blank | 246 comment | 20 complexity | f3a9b936535daf93fd129735897aafb7 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //+----------------------------------------------------------------------+
  4. //| WAMP (XP-SP1/1.3.27/4.0.12/4.3.3) |
  5. //+----------------------------------------------------------------------+
  6. //| Copyright (c) 1992-2003 Michael Wimmer |
  7. //+----------------------------------------------------------------------+
  8. //| I don't have the time to read through all the licences to find out |
  9. //| what the exactly say. But it's simple. It's free for non commercial |
  10. //| projects, but as soon as you make money with it, i want my share :-) |
  11. //| (License : Free for non-commercial use) |
  12. //+----------------------------------------------------------------------+
  13. //| Authors: Michael Wimmer <flaimo 'at' gmx 'dot' net> |
  14. //+----------------------------------------------------------------------+
  15. //
  16. // $Id$
  17. /**
  18. * @package Thumbnail
  19. */
  20. /**
  21. * Creates a thumbnail from a source image with GD2 functions
  22. *
  23. * Tested with Apache 1.3.27 and PHP 4.3.3
  24. * Last change: 2003-09-25
  25. *
  26. * @access public
  27. * @author Michael Wimmer <flaimo 'at' gmx 'dot' net>
  28. * @copyright Michael Wimmer
  29. * @link http://www.flaimo.com/
  30. * @package Thumbnail
  31. * @example sample_thumb.php Sample script
  32. * @version 1.001
  33. */
  34. class Thumbnail {
  35. /*-------------------*/
  36. /* V A R I A B L E S */
  37. /*-------------------*/
  38. /**#@+
  39. * @access protected
  40. */
  41. /**
  42. * possible image formats
  43. *
  44. * @var array
  45. */
  46. var $formats = array('gif' => 1, 'jpg' => 2, 'png' => 3, 'wbmp' => 15,
  47. 'string' => 999);
  48. /**
  49. * maximal height of the generated thumbnail
  50. *
  51. * @var int
  52. */
  53. var $thumb_max_height = 100;
  54. /**
  55. * maximal width of the generated thumbnail
  56. *
  57. * @var int
  58. */
  59. var $thumb_max_width = 100;
  60. /**
  61. * quality or speed when generating the thumbnail
  62. *
  63. * @var boolean
  64. */
  65. var $quality_thumb = TRUE;
  66. /**
  67. * path/filename of imagefile
  68. *
  69. * @var string
  70. */
  71. var $image_path;
  72. /**
  73. * @var int
  74. */
  75. var $image_width;
  76. /**
  77. * @var int
  78. */
  79. var $image_height;
  80. /**
  81. * image format of source
  82. *
  83. * @var int
  84. */
  85. var $image_type;
  86. /**
  87. * @var int
  88. */
  89. var $thumbnail_height;
  90. /**
  91. * @var int
  92. */
  93. var $thumbnail_width;
  94. /**
  95. * image format of the thumbnail
  96. *
  97. * @var int
  98. */
  99. var $thumbnail_type = 3;
  100. /**
  101. * @var resource
  102. */
  103. var $image;
  104. /**
  105. * @var resource
  106. */
  107. var $thumbnail;
  108. /**
  109. * @var string
  110. */
  111. var $version = '1.001';
  112. /**#@-*/
  113. /*-----------------------*/
  114. /* C O N S T R U C T O R */
  115. /*-----------------------*/
  116. /**
  117. * Constructor
  118. *
  119. * @param string $file path/filename of picture or stream from a DB field
  120. * @return void
  121. * @access protected
  122. * @uses $image_path
  123. */
  124. function Thumbnail($file = '') {
  125. $this->image_path = (string) $file;
  126. } // end constructor
  127. /*-------------------*/
  128. /* F U N C T I O N S */
  129. /*-------------------*/
  130. /**
  131. * sets the output type of the thumbnail
  132. *
  133. * @param string $format gif, jpg, png, wbmp
  134. * @return void
  135. * @access public
  136. * @uses $thumbnail_type
  137. * @uses $formats
  138. */
  139. function setOutputFormat($format = 'png') {
  140. if (array_key_exists(trim($format), $this->formats)) {
  141. $this->thumbnail_type = $this->formats[trim($format)];
  142. } // end if
  143. } // end function
  144. /**
  145. * sets the max. height of the thumbnail
  146. *
  147. * @param int $height
  148. * @return boolean
  149. * @access public
  150. * @uses readSourceImageData()
  151. * @uses $image_height
  152. * @uses $thumb_max_height
  153. */
  154. function setMaxHeight($height = 0) {
  155. $this->readSourceImageData();
  156. $height = (int) $height;
  157. if ($height < $this->image_height && $height > 0) {
  158. $this->thumb_max_height = $height;
  159. return (boolean) TRUE;
  160. } // end if
  161. return (boolean) FALSE;
  162. } // end function
  163. /**
  164. * sets the max. width of the thumbnail
  165. *
  166. * @param int $width
  167. * @return boolean
  168. * @access public
  169. * @uses readSourceImageData()
  170. * @uses $image_width
  171. * @uses $thumb_max_width
  172. */
  173. function setMaxWidth($width = 0) {
  174. $this->readSourceImageData();
  175. $width = (int) $width;
  176. if ($width < $this->image_width && $width > 0) {
  177. $this->thumb_max_width = $width;
  178. return (boolean) TRUE;
  179. } // end if
  180. return (boolean) FALSE;
  181. } // end function
  182. /**
  183. * sets the max. width and height of the thumbnail
  184. *
  185. * passes values to the functions setMaxHeight() and setMaxWidth()
  186. *
  187. * @param int $width
  188. * @param int $height
  189. * @return boolean
  190. * @access public
  191. * @uses setMaxHeight()
  192. * @uses setMaxWidth()
  193. */
  194. function setMaxSize($width = 0, $height = 0) {
  195. if ($this->setMaxWidth($width) === TRUE
  196. && $this->setMaxHeight($height) === TRUE) {
  197. return (boolean) TRUE;
  198. } else {
  199. return (boolean) FALSE;
  200. } // end if
  201. } // end function
  202. /**
  203. * whether to create thumbs fast or with good quality
  204. *
  205. * @param boolean $boolean
  206. * @return void
  207. * @access public
  208. * @uses $quality_thumb
  209. */
  210. function setQualityOutput($boolean = TRUE) {
  211. $this->quality_thumb = (boolean) $boolean;
  212. } // end function
  213. /**
  214. * reads metadata of the source image
  215. *
  216. * @return void
  217. * @access protected
  218. * @uses $image_width
  219. * @uses $image_height
  220. * @uses $image_type
  221. * @uses $formats
  222. */
  223. function readSourceImageData() {
  224. if (!file_exists($this->image_path)) { // if source pic wasnt found
  225. $this->image_path = 'error_pic';
  226. $this->image_width =& $this->thumb_max_width;
  227. $this->image_height =& $this->thumb_max_height;
  228. $this->image = @imagecreatetruecolor($this->image_width, $this->image_height)
  229. or die ('Cannot Initialize new GD image stream');
  230. $text_color = imagecolorallocate($this->image, 255, 255, 255);
  231. imagestring($this->image, 1, 2,
  232. ($this->image_height / 2 - 10),
  233. "Could't find", $text_color);
  234. imagestring($this->image, 1, 2,
  235. ($this->image_height / 2 - 4),
  236. 'source image', $text_color);
  237. imagestring($this->image, 1, 2,
  238. ($this->image_height / 2 + 4),
  239. '(Thumbnail V' . $this->version . ')', $text_color);
  240. } else {
  241. if (!isset($this->image_width)) {
  242. list($this->image_width, $this->image_height, $this->image_type, $attr) = getimagesize($this->image_path);
  243. unset($attr);
  244. if (!in_array($this->image_type, $this->formats)) {
  245. die("Can't create thumbnail from '" . $this->image_type . "' source: " . $this->image_path);
  246. } // end if
  247. } // end if
  248. } // end if
  249. } // end function
  250. /**
  251. * reads the source image into a variable
  252. *
  253. * @return void
  254. * @access protected
  255. * @uses $image
  256. * @uses readSourceImageData()
  257. * @uses $image_type
  258. * @uses $image_path
  259. */
  260. function readSourceImage() {
  261. if (!isset($this->image)) {
  262. $this->readSourceImageData();
  263. switch ($this->image_type) {
  264. case 1:
  265. $this->image = imagecreatefromgif($this->image_path);
  266. break;
  267. case 2:
  268. $this->image = imagecreatefromjpeg($this->image_path);
  269. break;
  270. case 3:
  271. $this->image = imagecreatefrompng($this->image_path);
  272. break;
  273. case 15:
  274. $this->image = imagecreatefromwbmp($this->image_path);
  275. break;
  276. case 999:
  277. default:
  278. $this->image = imagecreatefromstring($this->image_path);
  279. break;
  280. } // end switch
  281. } // end if
  282. } // end function
  283. /**
  284. * sets the actual width and height of the thumbnail based on the source image size and the max limits for the thumbnail
  285. *
  286. * @return void
  287. * @access protected
  288. * @uses readSourceImageData()
  289. * @uses $image_height
  290. * @uses $thumb_max_height
  291. * @uses $image_width
  292. * @uses $thumb_max_width
  293. */
  294. function setThumbnailSize() {
  295. $this->readSourceImageData();
  296. if (($this->image_height > $this->thumb_max_height)
  297. || ($this->image_width < $this->thumb_max_width)) {
  298. $sizefactor = (double) (($this->image_height > $this->image_width) ? ($this->thumb_max_height / $this->image_height) : ($this->thumb_max_width / $this->image_width));
  299. } else {
  300. $sizefactor = (int) 1;
  301. } // end if
  302. $this->thumbnail_width = (int) ($this->image_width * $sizefactor);
  303. $this->thumbnail_height = (int) ($this->image_height * $sizefactor);
  304. unset($sizefactor);
  305. } // end function
  306. /**
  307. * creates the thumbnail and saves it to a variable
  308. *
  309. * @return void
  310. * @access protected
  311. * @uses setThumbnailSize()
  312. * @uses readSourceImage()
  313. * @uses $thumbnail
  314. * @uses $thumbnail_width
  315. * @uses $thumbnail_height
  316. * @uses $quality_thumb
  317. * @uses $image
  318. * @uses $image_width
  319. * @uses $image_height
  320. */
  321. function createThumbnail() {
  322. $this->setThumbnailSize();
  323. $this->readSourceImage();
  324. if (!isset($this->thumbnail)) {
  325. $this->thumbnail = imagecreatetruecolor($this->thumbnail_width,
  326. $this->thumbnail_height);
  327. if ($this->quality_thumb === TRUE) {
  328. imagecopyresampled($this->thumbnail, $this->image, 0, 0, 0, 0,
  329. $this->thumbnail_width, $this->thumbnail_height,
  330. $this->image_width, $this->image_height);
  331. } else {
  332. imagecopyresized($this->thumbnail, $this->image, 0, 0, 0, 0,
  333. $this->thumbnail_width, $this->thumbnail_height,
  334. $this->image_width, $this->image_height);
  335. } // end if
  336. } // end if
  337. } // end function
  338. /**
  339. * outputs the thumbnail to the browser
  340. *
  341. * @param string $format
  342. * @param int $quality
  343. * @return void
  344. * @access public
  345. * @uses setOutputFormat()
  346. * @uses createThumbnail()
  347. * @uses $thumbnail_type
  348. * @uses $thumbnail
  349. */
  350. function outputThumbnail($format = 'png', $quality = 75) {
  351. $this->setOutputFormat($format);
  352. $this->createThumbnail();
  353. switch ($this->thumbnail_type) {
  354. case 1:
  355. header('Content-type: image/gif');
  356. imagegif($this->thumbnail);
  357. break;
  358. case 2:
  359. $quality = (int) $quality;
  360. if ($quality < 0 || $quality > 100) {
  361. $quality = 75;
  362. } // end if
  363. header('Content-type: image/jpeg');
  364. imagejpeg($this->thumbnail, '', $quality);
  365. break;
  366. case 3:
  367. header('Content-type: image/png');
  368. imagepng($this->thumbnail);
  369. break;
  370. case 15:
  371. header('Content-type: image/vnd.wap.wbmp');
  372. imagewbmp($this->thumbnail);
  373. break;
  374. } // end switch
  375. imagedestroy($this->thumbnail);
  376. imagedestroy($this->image);
  377. } // end function
  378. /**
  379. * returns the variable with the thumbnail image
  380. *
  381. * @return mixed
  382. * @access public
  383. * @uses setOutputFormat()
  384. * @uses createThumbnail()
  385. * @uses $thumbnail
  386. */
  387. function returnThumbnail() {
  388. $this->setOutputFormat();
  389. $this->createThumbnail();
  390. return $this->thumbnail;
  391. } // end function
  392. /**
  393. * returns the height of the thumbnail
  394. *
  395. * @return int
  396. * @access public
  397. * @uses $thumbnail_height
  398. */
  399. function getThumbHeight() {
  400. $this->createThumbnail();
  401. return (int) $this->thumbnail_height;
  402. } // end function
  403. /**
  404. * returns the width of the thumbnail
  405. *
  406. * @return int
  407. * @access public
  408. * @uses $thumbnail_width
  409. */
  410. function getThumbWidth() {
  411. $this->createThumbnail();
  412. return (int) $this->thumbnail_width;
  413. } // end function
  414. /**
  415. * returns the width of the thumbnail
  416. *
  417. * @return int
  418. * @access public
  419. * @uses $thumbnail_width
  420. */
  421. function getPictureName() {
  422. return (string) $this->image_path;
  423. } // end function
  424. } // end class Thumbnail
  425. ?>