Resizing images in CakePHP
Developing an app with CakePHP, I needed to resize an image in order to show them in the frontend. It’s a common task in nowadays webs so it shouldn’t be complicated. Doing a short google search i found this solution that it’s fine, but it didn’t work on the version of CakePHP I’m using, so, i’ve modified a bit and now I can use it. It’s a helper that you can use directly on the template, so it’s really handy.
The helper is in charge of resizing the images and save them in the cache directory /webroot/image_cache, you must be sure this directory exists.
/app/views/helpers/resizer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?php class ResizerHelper extends AppHelper { var $helpers = array('Html'); var $cacheDir = 'image_cache'; // relative to IMAGES_URL path /** * Automatically resizes an image and returns formatted IMG tag * * @param string $path Path to the image file, relative to the webroot/img/ directory. * @param integer $width Image of returned image * @param integer $height Height of returned image * @param boolean $aspect Maintain aspect ratio (default: true) * @param array $htmlAttributes Array of HTML attributes. * @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT. * @return mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. * @access public */ function resize($path, $width, $height, $aspect = true, $htmlAttributes = array(), $return = false) { $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type $fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS; $url = $fullpath.$path; if (!($size = getimagesize($url))) return; // image doesn't exist if ($aspect) { // adjust to aspect. if (($size[1]/$height) > ($size[0]/$width)) // $size[0]:width, [1]:height, [2]:type $width = ceil(($size[0]/$size[1]) * $height); else $height = ceil($width / ($size[0]/$size[1])); } $relfile = $this->webroot.$this->cacheDir.'/'.$width.'x'.$height.'_'.basename($path); // relative file $cachefile = $fullpath.$this->cacheDir.DS.$width.'x'.$height.'_'.basename($path); // location on server if (file_exists($cachefile)) { $csize = getimagesize($cachefile); $cached = ($csize[0] == $width && $csize[1] == $height); // image is cached if (@filemtime($cachefile) < @filemtime($url)) // check if up to date $cached = false; } else { $cached = false; } if (!$cached) { $resize = ($size[0] > $width || $size[1] > $height) || ($size[0] < $width || $size[1] < $height); } else { $resize = false; } if ($resize) { $image = call_user_func('imagecreatefrom'.$types[$size[2]], $url); if (function_exists("imagecreatetruecolor") && ($temp = imagecreatetruecolor ($width, $height))) { imagecopyresampled ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } else { $temp = imagecreate ($width, $height); imagecopyresized ($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } call_user_func("image".$types[$size[2]], $temp, $cachefile); imagedestroy ($image); imagedestroy ($temp); } return $this->output(sprintf($this->Html->tags['image'], $relfile, null), $return); } } ?> |
So, in order to use it in the template you must add it to the controller
1 | var $helpers = array('Html', 'Form', 'Session', 'Resizer'); |
Finally, in the template
1 | <?php echo $this->Resizer->resize($image['url'], 150, 150, true) ?> |