PhpThumbHelper.php 4.7 KB

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