Jump to content

Need Help Uploading Images...


CSmith1128

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--]
Link to comment
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
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.