Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to Upload and resize an image

Posted on 2003-10-29
6
Medium Priority
?
388 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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 i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

916 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