Solved

how to Upload and resize an image

Posted on 2003-10-29
6
340 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now