Resize Image Function
5
jdenton
The resize_image function allows you to resize a GIF, JPEG or PNG file to any dimension you wish and put an optional black border around the image. Here's an explanation of the arguements:
$image_path: The complete path to the image to be resized
$max_width: Maximum width of the resized image. Leave 0 if you want to specify only the height and have the width auto-scale.
$max_height: Maximum height of the resized image. Leave 0 if you want to specify only the width and have the height auto-scale.
$file_prefix: This will prepend a string onto the resized image filename. If you want to create a thumbnail image and keep the original, set $file_prefix = 'thumb'.
$dir: Allows you to save the resized image to a sub directory under the $image_path directory. If you want to save thumbnail images to images/thumbs, set $dir = 'thumbs'.
$border: boolean, 1 = border, 0 = no border
$mime: Image mime type. (image/jpeg, image/gif, image/png)
$image_path: The complete path to the image to be resized
$max_width: Maximum width of the resized image. Leave 0 if you want to specify only the height and have the width auto-scale.
$max_height: Maximum height of the resized image. Leave 0 if you want to specify only the width and have the height auto-scale.
$file_prefix: This will prepend a string onto the resized image filename. If you want to create a thumbnail image and keep the original, set $file_prefix = 'thumb'.
$dir: Allows you to save the resized image to a sub directory under the $image_path directory. If you want to save thumbnail images to images/thumbs, set $dir = 'thumbs'.
$border: boolean, 1 = border, 0 = no border
$mime: Image mime type. (image/jpeg, image/gif, image/png)
function resize_image($image_path,$max_width,$max_height,$file_prefix,$dir,$border,$mime) {
// Determine image filename from image_path
$fileArray = explode("/", $image_path);
$file_elements = count($fileArray);
$last_element = $file_elements - 1;
if (!empty($file_ext)) {
$out_file = $file_prefix.$fileArray[$last_element];
} else {
$out_file = $fileArray[$last_element];
}
// Rebuild directory string so we know where to save thumbnail file
$directory = "";
for ($a=0;$a<=($last_element - 1);$a++) {
if (empty($directory)) {
$directory = $fileArray[$a];
} else {
$directory = $directory."/".$fileArray[$a];
}
}
if (!empty($dir)) { $directory = $directory."/".$dir; }
$out_file = $directory."/".$out_file;
$size = getimagesize($image_path);
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
$im_in = imagecreatefromjpeg($image_path);
} elseif ($mime == "image/gif") {
$im_in = imagecreatefromgif($image_path);
} elseif ($mime == "image/png") {
$im_in = imagecreatefrompng($image_path);
}
// Get original image dimensions
$origW = $size[0];
$origH = $size[1];
// Determine scale percentage - we have 3 cases here
if (empty($max_width) && empty($max_height)) {
// This is gonna be tricky 'cause we want to avoid proportion problems
// Do short side first, then figure out other dimention
if ($origW > $origH) {
// Horizontal orientation
// Determine ideal scale
$scale = floor($max_height / $origH);
$new_height = $max_height;
$new_width = $max_width;
$optW = floor($origW*$scale);
// Now we need to first crop image to the right proportions before scaling
if ($max_width == $max_height) {
$cropW = $origH;
$cropH = $origH;
$origW = $cropW;
$origH = $cropH;
} elseif ($max_width < $max_height) {
$ratio = $max_width/$max_height;
$cropW = ($origW*$ratio);
$cropH = $origH;
$origW = $cropW;
$origH = $cropH;
} elseif ($max_width > $max_height) {
$ratio = $max_height/$max_width;
$cropW = $origW;
$cropH = ($origH*$ratio);
$origW = $cropW;
$origH = $cropH;
}
$im_out = imagecreatetruecolor($cropW, $cropH);
imagecopy($im_out, $im_in, 0, 0, 0, 0, $cropW, $cropH);
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
imagejpeg($im_out, $out_file, 100);
$im_in = imagecreatefromjpeg($out_file);
} elseif ($mime == "image/gif") {
imagegif($im_out, $out_file, 100);
$im_in = imagecreatefromgif($out_file);
} elseif ($mime == "image/png") {
imagepng($im_out, $out_file, 100);
$im_in = imagecreatefrompng($image_path);
}
} elseif ($origW < $origH) {
// Vertical orientation
// Determine ideal scale
$scale = floor($max_width / $origW);
$new_width = $max_width;
$new_height = $max_height;
$optH = floor($origH*$scale);
// Now we need to first crop image to the right proportions before scaling
if ($max_width == $max_height) {
$cropW = $origW;
$cropH = $origW;
$origW = $cropW;
$origH = $cropH;
} elseif ($max_width < $max_height) {
$ratio = $max_width/$max_height;
$cropW = ($origW*$ratio);
$cropH = $origH;
$origW = $cropW;
$origH = $cropH;
} elseif ($max_width > $max_height) {
$ratio = $max_height/$max_width;
$cropW = $origW;
$cropH = ($origH*$ratio);
$origW = $cropW;
$origH = $cropH;
}
$im_out = imagecreatetruecolor($cropW, $cropH);
imagecopy($im_out, $im_in, 0, 0, 0, 0, $cropW, $cropH);
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
imagejpeg($im_out, $out_file, 100);
$im_in = imagecreatefromjpeg($out_file);
} elseif ($mime == "image/gif") {
imagegif($im_out, $out_file, 100);
$im_in = imagecreatefromgif($out_file);
} elseif ($mime == "image/png") {
imagepng($im_out, $out_file, 100);
$im_in = imagecreatefrompng($out_file);
}
} elseif ($origW == $origH) {
// Square - easy
$new_width = $max_width;
$new_height = $max_height;
}
} elseif (!empty($max_width) && empty($max_height)) {
$scale = ($max_width / $origW);
$new_width = $max_width;
$new_height = floor($origH * $scale);
} elseif (!empty($max_height) && empty($max_width)) {
$scale = ($max_height / $origH);
$new_height = $max_height;
$new_width = floor($origW * $scale);
}
$im_out = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($im_out, $im_in, 0, 0, 0, 0, $new_width, $new_height, $origW, $origH);
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
imagejpeg($im_out, $out_file, 100);
} elseif ($mime == "image/gif") {
imagegif($im_out, $out_file, 100);
} elseif ($mime == "image/png") {
imagepng($im_out, $out_file, 100);
}
// Draw border
if ($border == 1) {
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
$im_in = imagecreatefromjpeg($out_file);
} elseif ($mime == "image/gif") {
$im_in = imagecreatefromgif($out_file);
} elseif ($mime == "image/png") {
$im_in = imagecreatefrompng($out_file);
}
$color = ImageColorAllocate($im_in,0,0,0);
ImageLine($im_in,0,0,($max_width-1),0,$color); // Top border
ImageLine($im_in,($max_width-1),1,($max_width-1),($max_height-1),$color); // Right border
ImageLine($im_in,($max_width-1),($max_height-1),1,($max_height-1),$color); // Bottom border
ImageLine($im_in,0,($max_height-1),0,0,$color); // Left border
if (($mime == "image/jpeg") || ($mime == "image/pjpeg")) {
imagejpeg($im_in, $out_file, 100);
} elseif ($mime == "image/gif") {
imagegif($im_in, $out_file, 100);
} elseif ($mime == "image/png") {
imagepng($im_in, $out_file, 100);
}
}
imagedestroy($im_out);
imagedestroy($im_in);
return $origW;
return $origH;
}






First off you have to change $file_ext to $file_prefix, but after that PHP still complains about being unable to open a stream path. I have used an absolute URL and have chmodded the directory so I don't see what the problem would be?