PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/php4/thumbnail_v1/class.CachedThumbnail.inc.php

http://flaimo-php.googlecode.com/
PHP | 237 lines | 87 code | 15 blank | 135 comment | 15 complexity | cefc458aa9710a0ab1afd6bb34cecc31 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. * include used classes
  19. */
  20. require_once 'class.Thumbnail.inc.php';
  21. require_once 'class.PictureCache.inc.php';
  22. /**
  23. * @package Thumbnail
  24. */
  25. /**
  26. * Creates a thumbnail from a source image and caches it for a given time
  27. *
  28. * Tested with Apache 1.3.27 and PHP 4.3.3
  29. * Last change: 2003-09-25
  30. *
  31. * @access public
  32. * @author Michael Wimmer <flaimo 'at' gmx 'dot' net>
  33. * @copyright Michael Wimmer
  34. * @link http://www.flaimo.com/
  35. * @package Thumbnail
  36. * @example sample_thumb.php Sample script
  37. * @version 1.001
  38. */
  39. class CachedThumbnail extends Thumbnail {
  40. /*-------------------*/
  41. /* V A R I A B L E S */
  42. /*-------------------*/
  43. /**#@+
  44. * @access protected
  45. */
  46. /**
  47. * amount of seconds thumbs should be cached. 0 = no cache
  48. *
  49. * @var int
  50. */
  51. var $cache_time = 0;
  52. /**
  53. * flipped formats array
  54. *
  55. * @var array
  56. */
  57. var $types;
  58. /**
  59. * holds PictureCache object
  60. *
  61. * @var object
  62. */
  63. var $cache;
  64. /**#@-*/
  65. /*-----------------------*/
  66. /* C O N S T R U C T O R */
  67. /*-----------------------*/
  68. /**
  69. * Constructor
  70. *
  71. * @param string $file path/filename of picture
  72. * @param int $seconds amount of seconds thumbs should be cached. 0 = no cache
  73. * @return void
  74. * @access public
  75. * @uses Thumbnail::Thumbnail()
  76. * @uses $cache_time
  77. * @uses $types
  78. */
  79. function CachedThumbnail($file = '', $seconds = 0) {
  80. parent::Thumbnail($file);
  81. $this->cache_time = (int) $seconds;
  82. $this->types = array_flip($this->formats);
  83. } // end constructor
  84. /*-------------------*/
  85. /* F U N C T I O N S */
  86. /*-------------------*/
  87. /**
  88. * fills the cache variable with the cache object
  89. *
  90. * @return void
  91. * @access protected
  92. * @uses PictureCache
  93. * @uses $cache_time
  94. * @uses $cache
  95. * @uses $image_path
  96. * @uses $thumbnail_type
  97. */
  98. function setCache() {
  99. if (!isset($this->cache)) {
  100. $this->cache = new PictureCache($this->image_path,
  101. $this->thumbnail_type,
  102. $this->cache_time,
  103. parent::getThumbHeight() . parent::getThumbWidth());
  104. } // end if
  105. } // end function
  106. /**
  107. * outputs the thumbnail to the browser
  108. *
  109. * overrides method of base class
  110. *
  111. * @param string $format gif, jpg, png, wbmp
  112. * @param int $quality jpg-quality: 0-100
  113. * @return mixed
  114. * @access public
  115. * @uses setOutputFormat()
  116. * @uses setCache()
  117. * @uses $cache_time
  118. * @uses Thumbnail::isPictureCached()
  119. * @uses Thumbnail::createThumbnail()
  120. * @uses PictureCache::writePictureCache()
  121. * @uses Thumbnail::outputThumbnail()
  122. * @uses $thumbnail
  123. * @uses PictureCache::returnPictureCache()
  124. */
  125. function outputThumbnail($format = 'png', $quality = 75) {
  126. parent::setOutputFormat($format);
  127. $this->setCache();
  128. if ($this->cache_time === 0 || $this->cache->isPictureCached() === FALSE) {
  129. parent::createThumbnail();
  130. if ($this->cache_time > 0) {
  131. $this->cache->writePictureCache($this->thumbnail, $quality);
  132. } // end if
  133. parent::outputThumbnail($format, $quality);
  134. } else {
  135. $ct = array(1 => 'gif', 2 => 'jpeg', 3 => 'png', 15 => 'vnd.wap.wbmp');
  136. if (array_key_exists($this->thumbnail_type, $ct)) {
  137. header('Content-type: image/' . $ct[$this->thumbnail_type]);
  138. } // end if
  139. header('Expires: ' . date("D, d M Y H:i:s", time() + $this->cache_time) . ' GMT');
  140. header('Cache-Control: public');
  141. header('Cache-Control: max-age=' . $this->cache_time);
  142. echo $this->cache->returnPictureCache();
  143. } // end if
  144. } // end function
  145. /**
  146. * returns the variable with the thumbnail image
  147. *
  148. * @param string $format gif, jpg, png, wbmp
  149. * @return mixed
  150. * @access public
  151. * @uses setOutputFormat()
  152. * @uses Thumbnail::setCache()
  153. * @uses $cache_time
  154. * @uses Thumbnail::isPictureCached()
  155. * @uses Thumbnail::createThumbnail()
  156. * @uses Thumbnail::writePictureCache()
  157. * @uses $thumbnail_type
  158. * @uses $thumbnail
  159. * @uses Thumbnail::returnCachePicturename()
  160. */
  161. function returnThumbnail($format = 'png') {
  162. $this->setOutputFormat($format);
  163. $this->setCache();
  164. if ($this->cache_time === 0 || $this->cache->isPictureCached() === FALSE) {
  165. parent::createThumbnail();
  166. if ($this->cache_time > 0) {
  167. $this->cache->writePictureCache($this->thumbnail, 100);
  168. } // end if
  169. } else {
  170. switch ($this->thumbnail_type) {
  171. case 1:
  172. $this->thumbnail = imagecreatefromgif($this->cache->returnCachePicturename());
  173. break;
  174. case 2:
  175. $this->thumbnail = imagecreatefromjpeg($this->cache->returnCachePicturename());
  176. break;
  177. case 3:
  178. $this->thumbnail = imagecreatefrompng($this->cache->returnCachePicturename());
  179. break;
  180. case 15:
  181. $this->thumbnail = imagecreatefromwbmp($this->cache->returnCachePicturename());
  182. break;
  183. case 999:
  184. default:
  185. $this->thumbnail = imagecreatefromstring($this->cache->returnCachePicturename());
  186. break;
  187. } // end switch
  188. } // end if
  189. return $this->thumbnail;
  190. } // end function
  191. /**
  192. * returns the path/filename of the cached thumbnail
  193. *
  194. * if cached pic is not available, tries to create it with the given parameters
  195. *
  196. * @param string $format gif, jpg, png, wbmp
  197. * @param int $quality jpg-quality: 0-100
  198. * @return mixed string or FALSE if no cached pic is available
  199. * @access public
  200. * @uses $cache_time
  201. * @uses PictureCache::isPictureCached()
  202. * @uses setOutputFormat()
  203. * @uses PictureCache::writePictureCache()
  204. * @uses Thumbnail::createThumbnail()
  205. */
  206. function getCacheFilepath($format = 'png', $quality = 75) {
  207. if ($this->cache_time === 0) {
  208. return (boolean) FALSE; // no cached thumb available
  209. } // end if
  210. $this->setOutputFormat($format);
  211. $this->setCache();
  212. $path = $this->cache->getCacheFilepath($format, $quality);
  213. if ($path != FALSE) {
  214. return (string) $path;
  215. } else { // trys to create cache and return filename
  216. parent::createThumbnail();
  217. $this->cache->writePictureCache($this->thumbnail, $quality);
  218. return $this->cache->getCacheFilepath($format, $quality);
  219. } // end if
  220. } // end function
  221. } // end class CachedThumbnail
  222. ?>