Solved

how to Upload and resize an image

Posted on 2003-10-29
6
372 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 49
Pull Variable from URL  Use php template 1 36
Get data from two MySQL tables 6 30
Dump data from mysql to xls php 10 25
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
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…
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.

730 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