PhpThumbHelper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @see http://code621.com/content/1/phpthumb-helper-for-cakephp
  4. * some mods?
  5. * //TODO: make to lib!?
  6. * //TODO: integrate ThumbnailHelper !!!
  7. * 2009-09-13 ms
  8. */
  9. class PhpThumbHelper extends AppHelper {
  10. protected $php_thumb;
  11. protected $options;
  12. protected $file_extension;
  13. protected $cache_filename;
  14. protected $thumb_data;
  15. protected $error;
  16. protected $error_detail;
  17. protected function init($options = array()) {
  18. $this->options = $options;
  19. $this->set_file_extension();
  20. $this->thumb_data = array();
  21. $this->error = 0;
  22. }
  23. protected function set_file_extension() {
  24. $this->file_extension = mb_substr($this->options['src'], mb_strrpos($this->options['src'], '.'), mb_strlen($this->options['src']));
  25. }
  26. protected function set_cache_filename() {
  27. ksort($this->options);
  28. $filename_parts = array();
  29. $cacheable_properties = array('src', 'new', 'w', 'h', 'wp', 'hp', 'wl', 'hl', 'ws', 'hs', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'zc', 'bc', 'bg', 'fltr');
  30. foreach ($this->options as $key => $value) {
  31. if (in_array($key, $cacheable_properties)) {
  32. $filename_parts[$key] = $value;
  33. }
  34. }
  35. $this->cache_filename = '';
  36. foreach ($filename_parts as $key => $value) {
  37. $this->cache_filename .= $key . $value;
  38. }
  39. $last_modified = date("F d Y H:i:s.", filectime($this->options['src']));
  40. $this->cache_filename = $this->options['save_path'] . DS . md5($this->cache_filename . $last_modified) . $this->file_extension;
  41. }
  42. protected function image_is_cached() {
  43. if (is_file($this->cache_filename)) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. protected function create_thumb() {
  49. if (!isset($this->php_thumb) || !is_object($this->php_thumb)) {
  50. App::import('Vendor', 'phpThumb', array('file' => 'phpThumb'.DS.'phpthumb.class.php'));
  51. }
  52. $this->php_thumb = new phpThumb();
  53. set_time_limit(30);
  54. //TODO: make it cleaner
  55. # addon
  56. $PHPTHUMB_CONFIG = array();
  57. $PHPTHUMB_CONFIG['allow_src_above_docroot'] = true;
  58. $PHPTHUMB_CONFIG['cache_disable_warning'] = true;
  59. $PHPTHUMB_CONFIG['max_source_pixels'] = 1920000;
  60. $PHPTHUMB_CONFIG['error_message_image_default'] = 'Image not found';
  61. $PHPTHUMB_CONFIG['error_die_on_source_failure'] = true;
  62. if ((int)Configure::read('debug') > 0) {
  63. $PHPTHUMB_CONFIG['cache_disable_warning'] = false;
  64. $PHPTHUMB_CONFIG['error_die_on_source_failure'] = false;
  65. }
  66. if (!empty($PHPTHUMB_CONFIG)) {
  67. foreach ($PHPTHUMB_CONFIG as $key => $value) {
  68. $keyname = 'config_'.$key;
  69. $this->php_thumb->setParameter($keyname, $value);
  70. }
  71. }
  72. # addon end
  73. foreach ($this->php_thumb as $var => $value) {
  74. if (isset($this->options[$var])) {
  75. $this->php_thumb->setParameter($var, $this->options[$var]);
  76. }
  77. }
  78. if ($this->php_thumb->GenerateThumbnail()) {
  79. $this->php_thumb->RenderToFile($this->cache_filename);
  80. } else {
  81. $this->error = 1;
  82. $this->error_detail = ereg_replace("[^A-Za-z0-9\/: .]", "", $this->php_thumb->fatalerror);
  83. }
  84. }
  85. protected function get_thumb_data() {
  86. $this->thumb_data['error'] = $this->error;
  87. if ($this->error) {
  88. $this->thumb_data['error_detail'] = $this->error_detail;
  89. $this->thumb_data['src'] = $this->options['error_image_path'];
  90. } else {
  91. $this->thumb_data['src'] = $this->options['display_path'] . '/' . mb_substr($this->cache_filename, mb_strrpos($this->cache_filename, DS) + 1, mb_strlen($this->cache_filename));
  92. }
  93. if (isset($this->options['w'])) {
  94. $this->thumb_data['w'] = $this->options['w'];
  95. }
  96. if (isset($this->options['h'])) {
  97. $this->thumb_data['h'] = $this->options['h'];
  98. }
  99. return $this->thumb_data;
  100. }
  101. protected function validate() {
  102. if (!is_file($this->options['src'])) {
  103. $this->error = 1;
  104. $this->error_detail = 'File ' . $this->options['src'] . ' does not exist';
  105. return;
  106. }
  107. $valid_extensions = array('.gif', '.jpg', '.jpeg', '.png');
  108. if (!in_array($this->file_extension, $valid_extensions)) {
  109. $this->error = 1;
  110. $this->error_detail = 'File ' . $this->options['src'] . ' is not a supported image type';
  111. return;
  112. }
  113. }
  114. public function generate($options = array()) {
  115. $this->init($options);
  116. $this->validate();
  117. if (!$this->error) {
  118. $this->set_cache_filename();
  119. if (!$this->image_is_cached()) {
  120. $this->create_thumb();
  121. }
  122. }
  123. return $this->get_thumb_data();
  124. }
  125. /**
  126. * @return string $error
  127. * 2010-05-15 ms
  128. */
  129. public function error() {
  130. return (String)$this->error_detail;
  131. }
  132. /** NOT IN USE YET **/
  133. /**
  134. * image tag
  135. */
  136. public function show($options = array(), $tag_options = array()) {
  137. $this->init($options, $tag_options);
  138. if ($this->image_is_cached()) {
  139. return $this->show_image_tag();
  140. } else {
  141. $this->create_thumb();
  142. return $this->show_image_tag();
  143. }
  144. }
  145. /**
  146. * image scr only
  147. */
  148. public function show_src($options = array()) {
  149. $this->init($options);
  150. if ($this->image_is_cached()) {
  151. return $this->get_image_src();
  152. } else {
  153. $this->create_thumb();
  154. return $this->get_image_src();
  155. }
  156. }
  157. }