Created
December 19, 2012 21:24
-
-
Save irazasyed/4340653 to your computer and use it in GitHub Desktop.
PHP: Resize image and return img resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*------------------------------------------------------+ | |
| 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