It's just a function! ImageCopyResized()
Thank you. I'd like code that offers an option between proportional and
non-proportional resizing. I'm sure they have something like that over
at PHP_CLASSES. But some code is good and some code sucks. So I'm
looking for suggestions.
Just finished writing and testing one!
Example uses of my function below:
# Resize foo.jpg to a jpg with width 400
resizeImage ('foo.jpg', 'jpg', 400);
resizeImage ('foo.jpg', 'jpeg', 400); // will also work
# Resize foo.jpg to a png with height 500
resizeImage ('foo.jpg', 'png', '', 500);
# Resize foo.jpg to a jpg with width 50 x height 500
resizeImage ('foo.jpg', 'png', 50, 500);
# "Resize" foo.jpg to a jpg the same size as the original
resizeImage ('foo.jpg', 'png');
# Licence: GPL
# Author: Martin Lucas-Smith, University of Cambridge
# Function to resize an image; supported input and output formats are: gif, jpg, png
function resizeImage ($sourceFile, $outputFormat = 'jpg', $newWidth = '', $newHeight = '')
{
# Check that the file exists for security reasons
if (!file_exists ($sourceFile)) {echo '<p>Error: the selected file could not be found.</p>'; return false;}
# Obtain the input format by taking the file extension, allowing for .jpeg and .jpg for JPG format
$inputFileExtension = substr ($sourceFile, -4);
if (substr ($sourceFile, -5) == '.jpeg') {$inputFileExtension = '.jpg';}
# Obtain the source image
switch ($inputFileExtension) {
/* # GIF format
case '.gif':
$sourceFile = ImageCreateFromGIF ($sourceFile);
break; */
# JPG format
case '.jpg':
$sourceFile = ImageCreateFromJPEG ($sourceFile);
break;
# PNG format
case '.png':
$sourceFile = ImageCreateFromPNG ($sourceFile);
break;
# If an invalid format has been requested, return false
default:
echo '<p>Error: an unsupported input format was requested.</p>';
return false;
}
# Obtain the height and width
$originalWidth = ImageSx ($sourceFile);
$originalHeight = ImageSy ($sourceFile);
# Ensure that a valid width and height have been entered
if (!is_numeric ($newWidth) && !is_numeric ($newHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
}
# Assign the width and height, proportionally if necessary
$newWidth = (is_numeric ($newWidth) ? $newWidth : (($newHeight / $originalHeight) * $originalWidth));
$newHeight = (is_numeric ($newHeight) ? $newHeight : (($newWidth / $originalWidth) * $originalHeight));
# Create the resized image
$handle = ImageCreateTrueColor ($newWidth, $newHeight);
ImageCopyResampled ($handle, $sourceFile, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
# Send the image
switch ($outputFormat) {
/* # GIF format
case 'gif':
header ("Content-Type: image/gif");
ImageGIF ($handle);
break; */
# JPG format
case 'jpg':
case 'jpeg':
header ("Content-Type: image/jpg");
ImageJPEG ($handle);
break;
# PNG format
case 'png':
header ("Content-Type: image/png");
ImagePNG ($handle);
break;
# If an invalid format has been requested, return false
default:
echo '<p>Error: an unsupported output format was requested.</p>';
return false;
}
}
Martin Lucas-Smith
www.geog.cam.ac.uk/~mvl22 www.lucas-smith.co.uk
Senior Computing Technician (Web Technician)
we*******@geog.cam.ac.uk
Department of Geography, University of Cambridge (01223 3)33390
& Webmaster, SPRI
we*******@spri.cam.ac.uk
Scott Polar Research Institute, University of Cambridge