PhpThumbHelper.php 4.9 KB

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