Page 1 of 1

Resize an image when uploaded

Posted: Mon Aug 17, 2009 1:47 pm
by tomsace
I have an image uploading script on my website which uploads any image as long as it is under 300kb. Now I wish for my script to resize the image when the upload is successful.

Here is my script so far:

Code: Select all

if($_POST["action"] == "Upload Image")
{
unset($imagename);
 
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
 
if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";
 
 
$imagename = basename($_FILES['image_file']['name']);
 
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
 
if(empty($error))
{
$newimage = "user/images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
 
Does anyone have any idea as to what code needs adding to my script? I have tried surfing google for some help but I cant manage to get anything working myself.

Thanks alot.

Re: Resize an image when uploaded

Posted: Mon Aug 17, 2009 2:06 pm
by icesolid
The code you just submitted has errors in it to begin with. Can you actually run that code above? I see syntax errors in it!

Re: Resize an image when uploaded

Posted: Mon Aug 17, 2009 2:10 pm
by tomsace
Yes my image upload script works fine, just copied and pasted the php part of the code!

Why? What errors can you see?

Re: Resize an image when uploaded

Posted: Mon Aug 17, 2009 2:12 pm
by icesolid
Try this and let me know.

Code: Select all

<?php
if($_POST["action"] == "Upload Image") {
    unset($imagename);
     
    if(!isset($_FILES) && isset($HTTP_POST_FILES))
    $_FILES = $HTTP_POST_FILES;
     
    if(!isset($_FILES['image_file']))
    $error["image_file"] = "An image was not found.";
     
    $imagename = basename($_FILES['image_file']['name']);
     
    if(empty($imagename))
    $error["imagename"] = "The name of the image was not found.";
     
    if(empty($error)) {
    $newimage = "user/images/" . $imagename;
     
    $src = imagecreatefromjpeg($_FILES['image_file']['tmp_name']);
 
    list($width, $height) = getimagesize($_FILES['image_file']['tmp_name']);
    
    // NEW WIDTH AND HEIGHT BELOW AND SET THE QUALITY
    
    $new_width = '800';
    $new_height = '600';
    $quality = '100';
 
    $tmp = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($tmp, '/your/root/directory/' . $imagename, $quality);
 
    imagedestroy($src);
    imagedestroy($tmp);                
    }
}
?>

Re: Resize an image when uploaded

Posted: Mon Aug 17, 2009 2:13 pm
by pickle

Re: Resize an image when uploaded

Posted: Mon Aug 17, 2009 4:25 pm
by tomsace
icesolid, I tried the script, the upload still works fine but the image doesn't actually resize.

Thanks pickle I will have a look at the codes.

Re: Resize an image when uploaded

Posted: Tue Aug 18, 2009 10:24 am
by McInfo
Here is an example: Resize images without stretching or squeezing

The script has some limitations, but it works as an example.

Edit: This post was recovered from search engine cache.