I need a double duty image re-size script. I need to upload an image and re-size it to a width of around 600px (keeping picture proportionate) and create a thumbnail image – saving both the re-sized bigger picture and the thumbnail image.
I’ve tried numerous variations of the below script but cannot get it to work. Part of the script I’m using now and it works OK, but the user has to re-size the picture first on their computer before uploading. I’ve posted tutorials on how to re-size an image using xnview and other programs but it’s just too difficult for most people to figure out. Microsoft does not have a “simple” picture re-sizer for Vista like they do for XP. So, I have to re-size their original picture for them from a massive 2800 x size and larger to something more website friendly like – 600px wide max, and then make a thumbnail.
[code=php]<form action=”imageupload.php” enctype=”multipart/form-data” method=”post”>
<input type=”file” size=”55″ name=”newimage”>
[code=php]// This is the temporary file created by PHP
$uploadedfile = $_FILES[‘newimage’][‘tmp_name’];
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);
// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being “stretched”
// or “squashed”. If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
// $filename = “images/”. $_FILES[‘newimage’][‘name’]; Blocked this because I do not want it to put picture into a folder yet.
// imagejpeg($tmp,$filename,100); Not sure what this does, so I blocked it since it’s related to the part that puts it into a folder.
// The script above re-sizes the original massive image to something more Web friendly.
// The script below should make a thumbnail of the re-sized image above and save both to the server and insert image info into database.
// The problem is that the script below isn’t connecting with the above script. I think because it’s setup to look for an uploaded file instead of a picture that has already been manipulated.
// The paths to save the image(s):
$thumb_dir = ‘./../../pgallery/thumbs/’;
$photo_dir = ‘./../../pgallery/’;
// The max width, or height of the thumbnail:
$thumb[‘width’] = 100;
$thumb[‘height’] = 80;
$thumb[‘resolution’] = 80;
// Uploaded file’s Temp Name:
// $tempname = $_FILES[‘newimage’][‘tmp_name’]; this looked for an uploaded file named newimage
// $tempname = $_FILES[‘$tmp’][‘tmp_name’]; Using $tmp didn’t work
$tempname = $tmp; // didn’t work either
// Define the image name:
// $image_name = $_FILES[‘newimage’][‘name’]; this looked for an uploaded file named newimage
// $image_name = $_FILES[‘$tmp’][‘name’]; Using $tmp didn’t work
$image_name = $tmp; // didn’t work either
// Is the file uploaded:
// if(is_uploaded_file($tempname))
if($tempname)
{
// Get the orginal dimensions:
$o_dims = getimagesize($tempname);
if($o_dims === false || $o_dims[2] != 2)
{
exit(‘<h2 style=”color:#f00″>Image Upload Failed!!</h2>’);
}
// Put the original dimensions into something easier to work with:
$width = $o_dims[0];
$height = $o_dims[1];
// Create a new image based on the temp:
$orig_image = imagecreatefromjpeg($tempname);
// Get the proportionate thumbnail dimensions:
if($height >= $width)
{
$t_width = ceil(($thumb[‘height’]/$height)*$width);
$t_height = $thumb[‘height’];
}
elseif($width > $height)
{
$t_width = $thumb[‘width’];
$t_height = ceil(($thumb[‘width’]/$width)*$height);
}
// Create a new image with our thumb dimensions:
$new_image = imagecreatetruecolor($t_width, $t_height);
// Now, copy the old image to the new image magically resizing it:
if(imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $t_width, $t_height, $width, $height) === false)
{
exit(‘<h2 style=”color: #f00;”>Image Resizing Failed!!</h2>’);
}
if(@imagejpeg($new_image, $thumb_dir.$image_name, $thumb[‘resolution’]) === false)
{
exit(‘<h2 style=”color:#f00;”>Creating Thumb Failed!!</h2>’);
}
if(move_uploaded_file($tempname, $photo_dir.$image_name) === false)
{
exit(‘<h2 style=”color:#f00;”>Unable to move uploaded file!!</h2>’);
}
}
// Insert image information into database
As you can see, I’ve tried to bypass the part of the thumbnail script that is looking for an uploaded file rather than the re-sized image. But I can’t get the thumbnail part to connect with the first re-size part.
Any help here would be appreciated.