PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/views/helpers/thumbnail.php

http://github.com/Datawalke/Coordino
PHP | 115 lines | 89 code | 12 blank | 14 comment | 13 complexity | 1a0d5a09d813e7fb8c059d14807aa375 MD5 | raw file
  1. <?php
  2. App::import('Vendor', 'phpThumb', array('file' => 'phpThumb'.DS.'phpthumb.class.php'));
  3. class ThumbnailHelper extends Helper {
  4. private $php_thumb;
  5. private $options;
  6. private $tag_options;
  7. private $file_extension;
  8. private $cache_filename;
  9. private $error;
  10. private function init($options = array(), $tag_options = array()) {
  11. $this->options = $options;
  12. $this->tag_options = $tag_options;
  13. $this->set_file_extension();
  14. $this->set_cache_filename();
  15. $this->error = '';
  16. }
  17. private function set_file_extension() {
  18. $this->file_extension = substr($this->options['src'], strrpos($this->options['src'], '.'), strlen($this->options['src']));
  19. }
  20. private function set_cache_filename() {
  21. ksort($this->options);
  22. $filename_parts = array();
  23. $cacheable_properties = array('src', 'new', 'w', 'h', 'wp', 'hp', 'wl', 'hl', 'ws', 'hs', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'zc', 'bc', 'bg', 'fltr');
  24. foreach($this->options as $key => $value) {
  25. if(in_array($key, $cacheable_properties)) {
  26. $filename_parts[$key] = $value;
  27. }
  28. }
  29. $this->cache_filename = '';
  30. foreach($filename_parts as $key => $value) {
  31. $this->cache_filename .= $key . $value;
  32. }
  33. $this->cache_filename = $this->options['save_path'] . DS . md5($this->cache_filename) . $this->file_extension;
  34. }
  35. private function image_is_cached() {
  36. if(is_file($this->cache_filename)) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. private function create_thumb() {
  43. $this->php_thumb = new phpThumb();
  44. foreach($this->php_thumb as $var => $value) {
  45. if(isset($this->options[$var])) {
  46. $this->php_thumb->setParameter($var, $this->options[$var]);
  47. }
  48. }
  49. if($this->php_thumb->GenerateThumbnail()) {
  50. $this->php_thumb->RenderToFile($this->cache_filename);
  51. } else {
  52. $this->error = ereg_replace("[^A-Za-z0-9\/: .]", "", $this->php_thumb->fatalerror);
  53. $this->error = str_replace('phpThumb v1.7.8200709161750', '', $this->error);
  54. }
  55. }
  56. /**
  57. * Print image tag for thumbnail. If the thumbnail doesn't exist, it will be created.
  58. * @param array $options
  59. * @param array $tag_options
  60. */
  61. public function show(array $options = array(),array $tag_options = array()) {
  62. echo $this->get($options, $tag_options);
  63. }
  64. /**
  65. * Return image tag for thumbnail. If the thumbnail doesn't exist, il will be created
  66. * @param array $options
  67. * @param array $tag_options
  68. */
  69. public function get(array $options = array(), array $tag_options = array()) {
  70. $this->init($options, $tag_options);
  71. if(!$this->image_is_cached()) {
  72. $this->create_thumb();
  73. }
  74. return $this->get_image_tag();
  75. }
  76. /**
  77. * Create image tag based on the current conf.
  78. */
  79. private function get_image_tag() {
  80. if($this->error != '') {
  81. $src = $this->options['error_image_path'];
  82. //$this->tag_options['alt'] = $this->error;
  83. } else {
  84. $src = $this->options['display_path'] . '/' . substr($this->cache_filename, strrpos($this->cache_filename, DS) + 1, strlen($this->cache_filename));
  85. }
  86. $img_tag = '<img src="' . $src . '"';
  87. if(isset($this->options['w'])) {
  88. $img_tag .= ' width="' . $this->options['w'] . '"';
  89. }
  90. if(isset($this->options['h'])) {
  91. $img_tag .= ' height="' . $this->options['h'] . '"';
  92. }
  93. if(isset($this->options['alt'])) {
  94. $img_tag .= ' alt="' . $this->options['alt'] . '"';
  95. }
  96. foreach($this->tag_options as $key => $value) {
  97. $img_tag .= ' ' . $key . '="' . $value . '"';
  98. }
  99. $img_tag .= ' />';
  100. return $img_tag;
  101. }
  102. }
  103. ?>