Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Created December 19, 2012 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irazasyed/4340653 to your computer and use it in GitHub Desktop.
Save irazasyed/4340653 to your computer and use it in GitHub Desktop.
PHP: Resize image and return img resource
/*------------------------------------------------------+
| Resize Image with crop or without crop and
| return resource
+------------------------------------------------------*/
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*($r-$w/$h)));
} else {
$height = ceil($height-($height*($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment