PageRenderTime 75ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/View/Helper/ImageHelper.php

http://github.com/croogo/croogo
PHP | 187 lines | 118 code | 23 blank | 46 comment | 29 complexity | f6209baa61c155d991d991e0ce185bde MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. App::uses('Helper', 'View/Helper');
  3. /**
  4. * @package Croogo.Croogo.View.Helper
  5. * @version 1.1
  6. * @author Josh Hundley
  7. * @author Jorge Orpinel <jop@levogiro.net> (changes)
  8. */
  9. class ImageHelper extends Helper {
  10. public $helpers = array(
  11. 'Html',
  12. 'Theme' => array(
  13. 'className' => 'Croogo.Theme',
  14. ),
  15. );
  16. /**
  17. * Automatically resizes an image and returns formatted IMG tag
  18. *
  19. * Options:
  20. * - aspect Maintain aspect ratio. Default: true
  21. * - uploadsDir Upload directory name. Default: 'uploads'
  22. * - cachedir Cache directory name. Default: 'resized'
  23. * - resizeInd: String to check in filename indicating that it was resized
  24. *
  25. * @param string $path Path to the image file, relative to the webroot/img/ directory.
  26. * @param integer $width Image of returned image
  27. * @param integer $height Height of returned image
  28. * @param array $options Options
  29. * @param array $htmlAttributes Array of HTML attributes.
  30. * @param boolean $return Whether this method should return a value or output it. This overrides AUTO_OUTPUT.
  31. * @return mixed Either string or echoes the value, depends on AUTO_OUTPUT and $return.
  32. * @access public
  33. */
  34. public function resize($path, $width, $height, $options = array(), $htmlAttributes = array(), $return = false) {
  35. if (is_bool($options)) {
  36. $options = array('aspect' => $options);
  37. }
  38. $options = Hash::merge(array(
  39. 'aspect' => true,
  40. 'uploadsDir' => 'uploads',
  41. 'cacheDir' => 'resized',
  42. 'resizedInd' => '.resized-',
  43. ), $options);
  44. $aspect = $options['aspect'];
  45. $uploadsDir = $options['uploadsDir'];
  46. $cacheDir = $options['cacheDir'];
  47. $resizedInd = $options['resizedInd'];
  48. $imgClass = $this->Theme->getCssClass('thumbnailClass');
  49. if (empty($htmlAttributes['alt'])) {
  50. $htmlAttributes['alt'] = 'thumb';
  51. }
  52. if (!array_key_exists('class', $htmlAttributes)) {
  53. $htmlAttributes['class'] = $imgClass;
  54. } elseif (isset($htmlAttributes['class']) && $htmlAttributes['class'] !== false) {
  55. $htmlAttributes['class'] .= ' ' . $imgClass;
  56. }
  57. $sourcefile = WWW_ROOT . DS . $path;
  58. if (!file_exists($sourcefile)) {
  59. return;
  60. }
  61. $size = getimagesize($sourcefile);
  62. if ($aspect) {
  63. if (($size[1]/$height) > ($size[0]/$width)) {
  64. $width = ceil(($size[0]/$size[1]) * $height);
  65. } else {
  66. $height = ceil($width / ($size[0]/$size[1]));
  67. }
  68. }
  69. $dimension = $resizedInd . $width . 'x' . $height;
  70. $parts = pathinfo(WWW_ROOT . $path);
  71. if ($resizedInd === '') {
  72. // legacy format
  73. $filename = $parts['filename'];
  74. $filename = preg_replace('/^[0-9]*x[0-9]*_/', '', $filename);
  75. $resized = $width . 'x' . $height . '_' . $filename . '.' . $parts['extension'];
  76. } else {
  77. $filename = $parts['filename'];
  78. $filename = preg_replace('/' . preg_quote($resizedInd) . '[0-9]*x[0-9]*/', '', $filename);
  79. $resized = $filename . $dimension . '.' . $parts['extension'];
  80. }
  81. $relfile = '/';
  82. if ($uploadsDir) {
  83. $relfile .= ltrim($uploadsDir, '/') . '/';
  84. }
  85. if ($cacheDir) {
  86. $relfile .= ltrim($cacheDir, '/') . '/';
  87. }
  88. $relfile .= $resized;
  89. $cachefile = WWW_ROOT . ltrim($relfile, '/');
  90. $targetDir = dirname($cachefile);
  91. if (!is_dir($targetDir)) {
  92. mkdir($targetDir);
  93. }
  94. $cached = false;
  95. if (file_exists($cachefile)) {
  96. $csize = getimagesize($cachefile);
  97. // image is cached
  98. $cached = ($csize[0] == $width && $csize[1] == $height);
  99. // check if up to date
  100. if (filemtime($cachefile) < filemtime($sourcefile)) {
  101. $cached = false;
  102. }
  103. }
  104. if (!$cached) {
  105. $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height);
  106. } else {
  107. $resize = false;
  108. }
  109. if ($resize) {
  110. $this->_resize($sourcefile, $size, $cachefile, $width, $height);
  111. } else {
  112. //copy($url, $cachefile);
  113. }
  114. return $this->output(sprintf($this->Html->_tags['image'], $this->webroot($relfile), $this->Html->_parseAttributes($htmlAttributes, null, '', ' ')), $return);
  115. }
  116. /**
  117. * Convenience method to resize image
  118. *
  119. * @param string $source File name of the source image
  120. * @param array $sourceSize Result of getimagesize() against $source
  121. * @param string $target File name of the target image
  122. * @param int $w Target Image width
  123. * @param int $h Target image height
  124. * @return void
  125. */
  126. protected function _resize($source, $sourceSize, $target, $w, $h) {
  127. $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp");
  128. $transparency = array("gif", "png");
  129. $format = $types[$sourceSize[2]];
  130. $sw = $sourceSize[0];
  131. $sh = $sourceSize[1];
  132. $image = call_user_func('imagecreatefrom' . $format, $source);
  133. if (function_exists('imagecreatetruecolor')) {
  134. $temp = imagecreatetruecolor($w, $h);
  135. if (in_array($format, $transparency)) {
  136. $this->_setupTransparency($temp, $w, $h);
  137. }
  138. imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $sw, $sh);
  139. } else {
  140. $temp = imagecreate($w, $h);
  141. if (in_array($format, $transparency)) {
  142. $this->_setupTransparency($temp, $w, $h);
  143. }
  144. imagecopyresized($temp, $image, 0, 0, 0, 0, $w, $h, $sw, $sh);
  145. }
  146. call_user_func('image' . $format, $temp, $target);
  147. imagedestroy ($image);
  148. imagedestroy ($temp);
  149. }
  150. /**
  151. * Convenience method to setup image transparency
  152. *
  153. * @param resource $image Image resource
  154. * @param int $w Width
  155. * @param int $h Height
  156. * @return void
  157. */
  158. protected function _setupTransparency($image, $w, $h) {
  159. imagealphablending($image, false);
  160. imagesavealpha($image, true);
  161. $transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
  162. imagefilledrectangle($image, 0, 0, $w, $h, $transparent);
  163. }
  164. }