Skip to content

Instantly share code, notes, and snippets.

@kadavre
Forked from bchapuis/image.php
Created September 4, 2013 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadavre/6434947 to your computer and use it in GitHub Desktop.
Save kadavre/6434947 to your computer and use it in GitHub Desktop.
<?php
class ImageHelper extends Helper {
var $helpers = array('Html');
var $cacheDir = 'cache'; // relative to IMAGES_URL path
function resize($path, $dst_w, $dst_h, $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.IMAGES_URL;
$url = $fullpath.$path;
list($w, $h, $type) = getimagesize($url);
$r = $w / $h;
$dst_r = $dst_w / $dst_h;
if ($r > $dst_r) {
$src_w = $h * $dst_r;
$src_h = $h;
$src_x = ($w - $src_w) / 2;
$src_y = 0;
} else {
$src_w = $w;
$src_h = $w / $dst_r;
$src_x = 0;
$src_y = ($h - $src_h) / 2;
}
$relfile = $this->cacheDir.'/'.$dst_w.'x'.$dst_h.'_'.basename($path); // relative file
$cachefile = $fullpath.$relfile;
if (file_exists($cachefile)) {
if (@filemtime($cachefile) >= @filemtime($url)) {
$cached = true;
} else {
$cached = false;
}
} else {
$cached = false;
}
if (!$cached) {
$image = call_user_func('imagecreatefrom'.$types[$type], $url);
if (function_exists("imagecreatetruecolor")) {
$temp = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($temp, $image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
} else {
$temp = imagecreate ($dst_w, $dst_h);
imagecopyresized($temp, $image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}
call_user_func("image".$types[$type], $temp, $cachefile);
imagedestroy($image);
imagedestroy($temp);
}
return $this->Html->image($relfile);
}
}
?>
@devworx
Copy link

devworx commented Sep 4, 2013

Keep it up :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment