Solved

how to Upload and resize an image

Posted on 2003-10-29
6
358 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
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 75 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

809 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