Jump to content

Archived

This topic is now archived and is closed to further replies.

CSmith1128

Need Help Uploading Images...

Recommended Posts

[!--fonto:Tahoma--][span style=\"font-family:Tahoma\"][!--/fonto--]Hello.. I need some help uploading images...

First, I would like to have a page where a user can upload multiple images from 1 place (up to 3 images). So I would like to have 3 browse fields on the page and when they click submit they will be uploaded.

Next, I would like to resize EACH image to 4 different sizes, then store each one in a different folder. I do not need to store the original image, only the resized images.

And I need each image to be resized so they are not blurry or distorted. So I need an image to be 100 pixels x 80 pixels. The image can be smaller if it needs to be so it won't be distorted, but it cannot be any larger.

Could someone help me with this? :-)

Thank you,
Chris [!--fontc--][/span][!--/fontc--]

Share this post


Link to post
Share on other sites
A few manual pages to get started on:

[a href=\"http://www.php.net/features.file-upload\" target=\"_blank\"]http://www.php.net/features.file-upload[/a]

[a href=\"http://www.php.net/imagecreatefromstring\" target=\"_blank\"]http://www.php.net/imagecreatefromstring[/a]

[a href=\"http://www.php.net/imagecopyresampled\" target=\"_blank\"]http://www.php.net/imagecopyresampled[/a]

fundamentals: you can use imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name'])) to create images from uploaded files. You can loop through multiple uploaded files by enclosing it in a while(next($_FILES)) loop.

once you've created the image, you'll need to resize it if it's larger than your current image, so you'll have to verify image dimentions... You can find the width and height of created image using imagesx() and imagesy() respectively.

the math for resizing images gets tricky. here's an example of landscape orentation:
[code]$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));
$newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$max_width));
imagecopyresampled($newim,$im,0,0,0,0,$max_width,((imagesy($im)/imagesx($im))*$max_width),imagesx($im),imagesy($im));[/code]
and portrait orentation:
[code]$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));
$newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$max_height),$max_height);
imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$max_height),$max_height,imagesx($im),imagesy($im));[/code]

you can find the image orentation by comparing imagesx to imagesy.

you can save images using somthing like imagejpeg(), imagepng(), imagegif(), etc.

hope that helps

Share this post


Link to post
Share on other sites
A quick quistion michaellunsford afther an image is resampled to
the size does the oreginal get placed in the same folder cheers.

Share this post


Link to post
Share on other sites
the original uploaded file is located in your /tmp/ directory (most likely) and will be deleted by the system eventually. In order to save a file someplace that won't be deleted, you have to copy or move it yourself. you can use move_uploaded_file(), or (since you have created and resized an image using PHP) you can save the image using imagejpeg (or any of the other image save functions).

again, the original uploaded image should eventually be deleted by the system.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.