Resize an image when uploaded

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Resize an image when uploaded

Post 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.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Resize an image when uploaded

Post 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!
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Resize an image when uploaded

Post 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?
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Re: Resize an image when uploaded

Post 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);                
    }
}
?>
Last edited by icesolid on Mon Aug 17, 2009 2:14 pm, edited 3 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Resize an image when uploaded

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Resize an image when uploaded

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Resize an image when uploaded

Post 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.
Post Reply