?
Solved

how to Upload and resize an image

Posted on 2003-10-29
6
Medium Priority
?
380 Views
Last Modified: 2008-03-06
I hope this is a relatively easy request... what i want to do is i want to upload an image to my website and i want it to resize it when it uploads.  So for example on my form i fill out the name, location(directory to where it's going) and pick which image i want to upload then click submit... then i would like it to resize the file ... i've heard of using functions like imageCopyResize() but am unsure how... php.net didn't give very good examples and i was hoping someone could assist me or give me some example code.

Thanks Again

Adam
0
Comment
Question by:Adam409
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 300 total points
ID: 9647813
hi,

i use this function, came from the php manual i think. it's for jpgs only

/* resizeToFile resizes a picture and writes it to the harddisk
*  
* $sourcefile = the filename of the picture that is going to be resized
* $dest_x   = X-Size of the target picture in pixels
* $dest_y   = Y-Size of the target picture in pixels
* $targetfile = The name under which the resized picture will be stored
* $jpegqual   = The Compression-Rate that is to be used
*/

function resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual = "80")
{


/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");

$source_x = $picsize[0];
$source_y  = $picsize[1];
$source_id = imageCreateFromJPEG("$sourcefile");

/* Create a new image object (not neccessarily true colour) */

$target_id=imagecreatetruecolor($dest_x, $dest_y);

/* Resize the original picture and copy it into the just created image
  object. Because of the lack of space I had to wrap the parameters to
several lines. I recommend putting them in one line in order keep your
  code clean and readable */


$target_pic=imagecopyresampled($target_id,$source_id,
                             0,0,0,0,
                              $dest_x,$dest_y,
                              $source_x,$source_y);

/* Create a jpeg with the quality of "$jpegqual" out of the
  image object "$target_pic".
  This will be saved as $targetfile */

imagejpeg ($target_id,"$targetfile",$jpegqual);

return true;

}

you need to make sure your form is multipart/form-data, eg.
<form enctype="multipart/form-data" action="<? print $_SERVER["PHP_SELF"]; ?>" method="POST">

then you have your input type="file" and input type="filename" or something

from the script processing the data, you can access the file through the $_FILES global variable (in versions of php after 4.1.0, $HTTP_POST_FILES before). so for example you may have copy($_FILES["filename"]["tmp_name"], "../images/yourdirectory/" . $_POST["filename"]); first. then you can use the function in this way:

resizeToFile("../images/yourdirectory/" . $_POST["filename"]); , "150", $height, "../images/yourdirectory/thumbnails/" . $_POST["filename"]);

cheers,

loz
0
 
LVL 3

Expert Comment

by:zero303
ID: 9648700
http://www.devarticles.com/art/1/608

Following link may be of use....
0
 

Author Comment

by:Adam409
ID: 9687484
Thanks lozloz

I tried to get a few of those functions to work like imagecreatetruecolor() and it said at www.php.net "Note: This function was added in PHP 4.0.6 and requires GD 2.0.1 or later".  I did a phpinfo() on what version of GD Library my web hosting company is using and it said GD Version 1.6.2 or higher... i'm not quite sure what this means exactly ... does this mean that this function should work or is the GD Library version not recent enough??

Adam
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:lozloz
ID: 9687690
it looks like you need to update your gd version. to be honest, it might be worth asking them to upgrade their version of php since 4.0.6 is pretty old..

loz
0
 

Author Comment

by:Adam409
ID: 9687943
That 4.0.6 was what they said was needed on php.net not what my webhosting company is currently running... but my version of GD does need to be upgraded i think.  

Anyways thanks for all your help lozloz

Adam
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9688002
alright, tell me if its still not working after the upgrade

loz
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question