Skip to content

Instantly share code, notes, and snippets.

@arnold-almeida
Created April 16, 2009 08:23
Show Gist options
  • Save arnold-almeida/96315 to your computer and use it in GitHub Desktop.
Save arnold-almeida/96315 to your computer and use it in GitHub Desktop.
PHP GD Resize
/**
* @param $file Path to source file
* @param $dest Path to save the image
* @param $type Method of resizing ('resize', 'resizemin', 'resizecrop', 'crop')
* @param $size Dimensions, array of width and height or maxScale
* @param $output Type of file to save it as ('jpg', 'png', 'gif')
* @param $quality 0 < x < 100
*/
function image ($file, $dest, $type, $size, $output = NULL, $quality = NULL, &$error = '') {
if (is_null($type)) $type = 'resize';
if (is_null($size)) $size = 100;
if (is_null($output)) $output = 'jpg';
if (is_null($quality)) $quality = 75;
// -- format variables
$type = strtolower($type);
$output = strtolower($output);
if (is_array($size)) {
$maxW = intval($size[0]);
$maxH = intval($size[1]);
} else {
$maxScale = intval($size);
}
// -- check sizes
if (isset($maxScale)) {
if (!$maxScale) {
$error = "Max scale must be set";
return false;
}
$maxW = $maxH = $maxScale;
} else {
if (!$maxW || !$maxH) {
$error = "Size width and height must be set";
return false;
}
if ($type == 'resize') {
$error = "Provide only one number for size";
return false;
}
}
// -- check output
if ($output != 'jpg' && $output != 'png' && $output != 'gif') {
$error = "Cannot output file as " . strtoupper($output);
return false;
}
if (is_numeric($quality)) {
$quality = intval($quality);
if ($quality > 100 || $quality < 1) {
$quality = 75;
}
} else {
$quality = 75;
}
// -- get some information about the file
$uploadSize = getimagesize($file);
$uploadWidth = $uploadSize[0];
$uploadHeight = $uploadSize[1];
$uploadType = $uploadSize[2];
if ($uploadType != 1 && $uploadType != 2 && $uploadType != 3) {
$error = "File type must be GIF, PNG, or JPG to resize";
return false;
}
switch ($uploadType) {
case 1: $srcImg = imagecreatefromgif($file); break;
case 2: $srcImg = imagecreatefromjpeg($file); break;
case 3: $srcImg = imagecreatefrompng($file); break;
default: $error = "File type must be GIF, PNG, or JPG to resize"; return false;
}
switch ($type) {
case 'resize':
# Maintains the aspect ration of the image and makes sure that it fits
# within the maxW and maxH (thus some side will be smaller)
// -- determine new size
if ($uploadWidth > $maxScale || $uploadHeight > $maxScale) {
if ($uploadWidth > $uploadHeight) {
$newX = $maxScale;
$newY = ($uploadHeight*$newX)/$uploadWidth;
} else if ($uploadWidth < $uploadHeight) {
$newY = $maxScale;
$newX = ($newY*$uploadWidth)/$uploadHeight;
} else if ($uploadWidth == $uploadHeight) {
$newX = $newY = $maxScale;
}
} else {
$newX = $uploadWidth;
$newY = $uploadHeight;
}
$dstImg = imagecreatetruecolor($newX, $newY);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newX, $newY, $uploadWidth, $uploadHeight);
break;
case 'resizemin':
# Maintains aspect ratio but resizes the image so that once
# one side meets its maxW or maxH condition, it stays at that size
# (thus one side will be larger)
#get ratios
$ratioX = $maxW / $uploadWidth;
$ratioY = $maxH / $uploadHeight;
#figure out new dimensions
if (($uploadWidth == $maxW) && ($uploadHeight == $maxH)) {
$newX = $uploadWidth;
$newY = $uploadHeight;
} else if (($ratioX * $uploadHeight) > $maxH) {
$newX = $maxW;
$newY = ceil($ratioX * $uploadHeight);
} else {
$newX = ceil($ratioY * $uploadWidth);
$newY = $maxH;
}
$dstImg = imagecreatetruecolor($newX,$newY);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newX, $newY, $uploadWidth, $uploadHeight);
break;
case 'resizemax':
# Maintains aspect ratio but resizes the image so that once
# one side meets its maxW or maxH condition, it stays at that size
# (thus one side will be small)
#get ratios
$ratioX = $maxW / $uploadWidth;
$ratioY = $maxH / $uploadHeight;
#figure out new dimensions
if (($uploadWidth == $maxW) && ($uploadHeight == $maxH)) {
$newX = $uploadWidth;
$newY = $uploadHeight;
} else if (($ratioX * $uploadHeight) < $maxH) {
$newX = $maxW;
$newY = ceil($ratioX * $uploadHeight);
} else {
$newX = ceil($ratioY * $uploadWidth);
$newY = $maxH;
}
$dstImg = imagecreatetruecolor($newX,$newY);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newX, $newY, $uploadWidth, $uploadHeight);
break;
case 'resizecrop':
// -- resize to max, then crop to center
$ratioX = $maxW / $uploadWidth;
$ratioY = $maxH / $uploadHeight;
if ($ratioX < $ratioY) {
$newX = round(($uploadWidth - ($maxW / $ratioY))/2);
$newY = 0;
$uploadWidth = round($maxW / $ratioY);
$uploadHeight = $uploadHeight;
} else {
$newX = 0;
$newY = 0; //round(($uploadHeight - ($maxH / $ratioX))/2);
$uploadWidth = $uploadWidth;
$uploadHeight = round($maxH / $ratioX);
}
$dstImg = imagecreatetruecolor($maxW, $maxH);
imagecopyresampled($dstImg, $srcImg, 0, 0, $newX, $newY, $maxW, $maxH, $uploadWidth, $uploadHeight);
break;
case 'crop':
// -- a straight centered crop
$startY = ($uploadHeight - $maxH)/2;
$startX = ($uploadWidth - $maxW)/2;
$dstImg = imageCreateTrueColor($maxW, $maxH);
ImageCopyResampled($dstImg, $srcImg, 0, 0, $startX, $startY, $maxW, $maxH, $maxW, $maxH);
break;
default: $error = "Resize function \"$type\" does not exist"; return false;
}
// -- try to write
switch ($output) {
case 'jpg':
$write = imagejpeg($dstImg, $dest, $quality);
break;
case 'png':
$write = imagepng($dstImg, $dest . ".png", $quality);
break;
case 'gif':
$write = imagegif($dstImg, $dest . ".gif", $quality);
break;
}
// -- clean up
imagedestroy($dstImg);
if ($write) {
return true;
} else {
$error = "Could not write " . $file . " to " . $dest;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment