www.webdeveloper.com
Results 1 to 2 of 2

Thread: "Double" Re-Size Image Script

  1. #1
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    104

    "Double" Re-Size Image Script

    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.

    PHP Code:
    <form action="imageupload.php" enctype="multipart/form-data" method="post">
    <
    input type="file" size="55" name="newimage"
    PHP Code:
    // 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_image0000$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.

  2. #2
    Join Date
    Nov 2007
    Posts
    140
    The first thing I'd do would be to free up the rather large amount of memory required to load the original image. After the smaller "$tmp" image is created you don't need it any more so call imagedestroy($src) to release it.

    There is really no point in writing an image to disk then immediately reading the same image again. This requires encoding the image as a jpeg and then decoding the jpeg back into memory as a bitmap again which is both slow and pollutes your clean source image with jpeg artifacts created during the compression. Get rid of all that file handling stuff and just use the image you already have. $orig_image = $tmp;

    There's also no reason to call move_uploaded_file() as you intend to discard the uploaded file in favor of the version that has been re-sampled to 600px wide.

    If I were you I'd just start rewriting the script from the beginning like this:

    make sure a valid image was uploaded
    create a re-sampled image that is max 600px wide from the uploaded image
    free up the memory used by the uploaded image
    create a re-sampled thumbnail from the 600px wide image
    compress the 600px image to disk
    free up the memory used by the 600px image
    compress the thumb to disk
    free up the memory used by the thumb
    ... any other stuff you need to do


    Hope this was of some help.

    btw imagejpeg() compresses an image into jpeg format. The manual is your friend

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles