PhpThumbHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. */
  9. class PhpThumbHelper extends AppHelper {
  10. protected $PhpThumb;
  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->thumbData = array();
  21. $this->error = 0;
  22. }
  23. protected function set_file_extension() {
  24. $this->fileExtension = 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. $filenameParts = array();
  29. $cacheableProperties = 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, $cacheableProperties)) {
  32. $filenameParts[$key] = $value;
  33. }
  34. }
  35. $this->cacheFilename = '';
  36. foreach ($filenameParts as $key => $value) {
  37. $this->cacheFilename .= $key . $value;
  38. }
  39. $lastModified = ''; //date("F d Y H:i:s.", filectime($this->options['src']));
  40. $this->cacheFilename = $this->options['save_path'] . DS . md5($this->cacheFilename . $lastModified) . $this->fileExtension;
  41. }
  42. protected function image_is_cached() {
  43. if (is_file($this->cacheFilename)) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. protected function create_thumb() {
  49. if (!isset($this->PhpThumb) || !is_object($this->PhpThumb)) {
  50. App::import('Vendor', 'phpThumb', array('file' => 'phpThumb' . DS . 'phpthumb.class.php'));
  51. }
  52. $this->PhpThumb = new PhpThumb();
  53. set_time_limit(30);
  54. //TODO: make it cleaner
  55. # addon
  56. $phpthumbConfig = array();
  57. $phpthumbConfig['allow_src_above_docroot'] = true;
  58. $phpthumbConfig['cache_disable_warning'] = true;
  59. $phpthumbConfig['max_source_pixels'] = 1920000;
  60. $phpthumbConfig['error_message_image_default'] = 'Image not found';
  61. $phpthumbConfig['error_die_on_source_failure'] = true;
  62. if ((int)Configure::read('debug') > 0) {
  63. $phpthumbConfig['cache_disable_warning'] = false;
  64. $phpthumbConfig['error_die_on_source_failure'] = false;
  65. }
  66. if (!empty($phpthumbConfig)) {
  67. foreach ($phpthumbConfig as $key => $value) {
  68. $keyname = 'config_' . $key;
  69. $this->PhpThumb->setParameter($keyname, $value);
  70. }
  71. }
  72. # addon end
  73. foreach ($this->PhpThumb as $var => $value) {
  74. if (isset($this->options[$var])) {
  75. $this->PhpThumb->setParameter($var, $this->options[$var]);
  76. }
  77. }
  78. if ($this->PhpThumb->GenerateThumbnail()) {
  79. $this->PhpThumb->RenderToFile($this->cacheFilename);
  80. } else {
  81. $this->error = 1;
  82. $this->errorDetail = ereg_replace("[^A-Za-z0-9\/: .]", "", $this->PhpThumb->fatalerror);
  83. }
  84. }
  85. protected function get_thumb_data() {
  86. $this->thumbData['error'] = $this->error;
  87. if ($this->error) {
  88. $this->thumbData['error_detail'] = $this->errorDetail;
  89. $this->thumbData['src'] = $this->options['error_image_path'];
  90. } else {
  91. $this->thumbData['src'] = $this->options['display_path'] . '/' . mb_substr($this->cacheFilename, mb_strrpos($this->cacheFilename, DS) + 1, mb_strlen($this->cacheFilename));
  92. }
  93. if (isset($this->options['w'])) {
  94. $this->thumbData['w'] = $this->options['w'];
  95. }
  96. if (isset($this->options['h'])) {
  97. $this->thumbData['h'] = $this->options['h'];
  98. }
  99. return $this->thumbData;
  100. }
  101. protected function validate() {
  102. if (!is_file($this->options['src'])) {
  103. $this->error = 1;
  104. $this->errorDetail = 'File ' . $this->options['src'] . ' does not exist';
  105. return;
  106. }
  107. $validExtensions = array('.gif', '.jpg', '.jpeg', '.png');
  108. if (!in_array($this->fileExtension, $validExtensions)) {
  109. $this->error = 1;
  110. $this->errorDetail = '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. */
  128. public function error() {
  129. return (string)$this->errorDetail;
  130. }
  131. /** NOT IN USE YET **/
  132. /**
  133. * Image tag
  134. */
  135. public function show($options = array(), $tagOptions = array()) {
  136. $this->init($options, $tagOptions);
  137. if ($this->image_is_cached()) {
  138. return $this->show_image_tag();
  139. } else {
  140. $this->create_thumb();
  141. return $this->show_image_tag();
  142. }
  143. }
  144. /**
  145. * Image src only
  146. */
  147. public function show_src($options = array()) {
  148. $this->init($options);
  149. if ($this->image_is_cached()) {
  150. return $this->get_image_src();
  151. } else {
  152. $this->create_thumb();
  153. return $this->get_image_src();
  154. }
  155. }
  156. }