mentecky
Posts: 321
|
Posted: 05/24/2008, 2:04 PM |
|
I posted this as an answer to a problem in the PHP forum and it was suggested I post it here also. So here goes:
My goal was to upload an image, resize it to a maximum of 640x480 and create a thumbnail for the gallery. It's probably not the most elegant piece of code, but it works. The included function below is a derivative of some code I stole from the following web site: http://www.weberdev.com/get_example-4629.html
In preparation, I created 2 folders to hold my images and made sure my web server had read/write access to them. In this example they are "/images/gallery/images/" and "/images/gallery/thumb/".
Put this function somewhere. I put it in the bottom of Common.php:
/* -------------------------------------------------------------------------------------------
Function:
resizeImage
Description:
Resizes an image to fit in max_width x max_height
Params
$filename - Path to file to resize
$max_width - Maximum width of new file
$max_height - Maximum height of new file
$newfilename - Path to new file. If empty the original file is replaced
$withSampling - Resample the image. Default True
Returns
True if successful
------------------------------------------------------------------------------------------- */
function resizeImage($filename, $max_width, $max_height, $newfilename="", $withSampling=true)
{
$width = 0;
$height = 0;
$newwidth = 0;
$newheight = 0;
// If no new filename was specified then use the original filename
if($newfilename == "")
{
$newfilename = $filename;
}
// Get original sizes
list($width, $height) = getimagesize($filename);
if ($width > $height)
{
// We're dealing with max_width
if ($width > $max_width)
{
$newwidth = $width * ($max_width / $width);
$newheight = $height * ($max_width / $width);
}
else
{
// No need to resize
$newwidth = $width;
$newheight = $height;
}
}
else
{
// Deal with max_height
if ($height > $max_height)
{
$newwidth = $width * ($max_height / $height);
$newheight = $height * ($max_height / $height);
}
else
{
// No need to resize
$newwidth = $width;
$newheight = $height;
}
}
// Create a new image object
$thumb = imagecreatetruecolor($newwidth, $newheight);
// Load the original based on it's extension
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if($ext=='jpg' || $ext=='jpeg')
{
$source = imagecreatefromjpeg($filename);
}
else if($ext=='gif')
{
$source = imagecreatefromgif($filename);
}
else if($ext=='png')
{
$source = imagecreatefrompng($filename);
}
else
{
// Fail because we only do JPG, JPEG, GIF and PNG
return FALSE;
}
// Resize the image with sampling if specified
if($withSampling)
{
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
else
{
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
// Save the new image
if($ext=='jpg' || $ext=='jpeg')
{
return imagejpeg($thumb, $newfilename);
}
else if($ext=='gif')
{
return imagegif($thumb, $newfilename);
}
else if($ext=='png')
{
return imagepng($thumb, $newfilename);
}
}
The above function will adjust the image to a maximum width or height. It's far from perfect but it did what I needed.
I set my "Temporary Folder" to a temp folder on my server. I set "File Folder" to "/images/gallery/images" which is the final destination for my full size images.
In my FileUpload component AfterProcessFile I put the following:
$filename = dirname(__FILE__)."/images/gallery/images/".$gallery_images->path->GetValue();
$resized = dirname(__FILE__)."/images/gallery/images/".$gallery_images->path->GetValue();
$thumb = dirname(__FILE__)."/images/gallery/thumb/".$gallery_images->path->GetValue();
// First resize it to 640x480 max
if (resizeImage($filename, 640, 480, $resized))
{
// Create a thumbnail image
if (!resizeImage($filename, 150, 113, $thumb))
{
$gallery_images->Errors->addError("There was an error creating a thumbnail image.");
unlink($resized);
}
}
else
{
$gallery_images->Errors->addError("There was an error resizing your image.");
}
$gallery_images is the name of my record control.
$gallery_images->path is the name of my FileUpload control.
Edit the code that sets the following:
$filename = Original filename
$resized = Target file for the resized image
$thumb = Target filename for the thumbnail
NOTE: In my above code $filename and $resized are the same. This will copy over the original file.
Also change the Width and Height parameters in the resizeImage calls to match your needs.
You can see the results of this code on my band site at: http://www.grifterrock.com/gallery.php
I have another site that actually creates 3 sizes of images at: http://www.meetwny.com/gallery.php
You'll notice the gallery grid formats nicely because the height of pictures taken sideways matches the height of standard images. This makes all rows the same height.
Hope that helps
_________________
http://www.ccselite.com |