Upload and Resize an Image with PHP

Are you looking for image upload and Resize PHP script. I had implemented a simple PHP script to re-sizing image into different dimensions. It's very useful to your web projects to save hosting space and bandwidth to reduce the original image to compressed size.

hello i m jack here i just see u php tutorial ,upload and resize an image with php ,but i face a problem i using the script to upload a pngfiles format but after upload my files background is black colour. the oroginal files is transparency.how to make the png files still keep the transparency? thank you .

Why does this fail for large file sized images? For example, I can upload all the windows sample pictures which range in size from 27 KB to 103 KB. However, I can’t upload an image that is 1 MB such as this one http://tinyurl.com/yg4zfak

I believe it hangs up on the line $src = imagecreatefromjpeg($uploadedfile); (or png, gif).

How can I make it so this script will allow all sizes of images?Thanks.

nice script and as stated above this is alot for newbies if you could please help with how and where the changes could go am a learn as you go and am building my site from tutorials, so if you could please help as to where in page this goes so that the images could show I would really appreciate it my site is not up yet but am gonna place this code in the section so that visitor could post there pics of the party they have being and so! its gonna go on the section called subetusfotos.html Thanks Maria and Rey

I have a better solution for the png transparencyelse if($extension=="png"){ $img=imagecreatefrompng($upload); //If our image have white background imagealphablending($temp,false);//we capture the bg of our image $color=imagecolorallocatealpha($temp, 0, 0, 0, 127);//fill the bg we capture before to put it in the resized oneimagefill($temp,0,0,$color);//we restore the transparency(bg) imagesavealpha($temp,true);//then we follow the steps of our friend =) imagecopyresampled($temp,$img,0,0,0,0,$newwidth,$newheight,$width,$height); imagepng($temp,$p.'_m'.".".$extension); I hope it helps you :D move_uploaded_file($upload,$p.".".$extension);

Works great but I have a problem with gif extension. Uploaded gif images is black.Output:Warning: imagecreatefromgif() [function.imagecreatefromgif]: '/tmp/phpThbsBn' is not a valid GIF file in /home/rapidm/public_html/test/untitled.php on line 70

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/rapidm/public_html/test/untitled.php on line 79

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/rapidm/public_html/test/untitled.php on line 85

What happens when an image already exist with the file name? As I see it's doesn't upload an image if another image is already uploaded with the same name... Is it possible to rename uploaded image if another image already exists?

Remember to give permissions to folders, the file wont upload to the server if you dont give the right permissions.

@Srinivas

Thanks friend, you helped me a lot.

@ every reader

If you are using a form to store data in the server and in the database, you may be using an ID for each entry to the DB, just include the ID and that way you will be able to find who or which entry the image belongs to.

Hy, This is new Multi Upload with Highslide. Ther wear som problems with naming images by time so i aded something. Stil if you wont add more pictures yust add more variables in array $slike = array(file, file1, file2, file3, .....) http://www.box.net/shared/n85bj1hbh1

Great adds to the script, could you please tell me how to give each uploaded picture their own path so that I can add 3 image paths straight into a mysql database and call each picture straight from the database?

Could you please help me? I want to resize 5 images to be a square size like this http://img251.imageshack.us/img251/4406/tarsan200x200.jpg(keep original ratio image in the middle of white background.)

also, I want to use these 5 pictures in my next php file. However I cannot find what their name in your code. Could you please point me to the name of each one. So I can use them in my next php file.

""""also, I want to use these 5 pictures in my next php file. However I cannot find what their name in your code. Could you please point me to the name of each one. So I can use them in my next php file.""""

From above, I mean....

How can I write a code to show pic1, pic2, pic3, pic4, pic5 on my webpage. These 5 pictures will be shown horizontaly inside a big frame.

hi muthu , look on your server path like ../image some times this is the reason because things don't work that happens to me on godaddy every time i create some application with fireworks ore Dreamweaver , like all Java script generated by them , what i find on those cases is that i have to make some little changes on my script let me give you an example : ORIGINAL : img src="imagenes/spacer.gif" width="7" height="1" border="0" alt="" I HAVE TO CHANGED TO :img src="/imagenes/spacer.gif" width="7" height="1" border="0" alt=""

Great script - here is the fix to correct the file upload validation error that always says file uploaded succesfully - even when there is no file. Basically $_FILES always returns true so you have to go by the file size. I added this at the end (after the very last $change variable update)

hello guys, i am trying to use this script but i want it to be able to upload 7 files... 6 of them i want to have name 01.jpg, 02, 03.... 06.jpg and the other one i want to be named profile.jpg.. how can i do this, like to auto-rename it and in case the person who upload them wants to replace one of them how can i do this? like how can i make them able to erase a specific file out of the 7 they already uploaded and how can i let the script know eactly which one is missing and rename it just like that (lets say they erased file 05.jpg.. i want the script to identify 05.jpg is missing and thats the file name it should use for the new file....) or if its too complicated i would like to just replace an specific existing file with a new one... ughhh this is so complicated i dont even know how to explain it... so frustrating!

excelent tutorial, is the easy way to make some thumbnails. I have a nice but complicated script to make 3 thumbs square and 1 proportional with the original size. with your example I will make shorter.thx again.

Very simple and nice, i had a upload script already and i looked up a lot on how to resize images, at first i thought cropping and resizing are the same thing but then i learned its not, i first cropped images randomly which would have cropped any portion of the image...then i realized, all i wanted to do is make it look smaller so resizing is what i wanted..So i came on this :D and loved it, learned a lot from it..

What I am wondering about only ONE person commented on here, which surprises me. Anyway, the "js/___.js" files for: displaymsg.js and ajaxdelete.js are not included, what effect or impact do they have on this script?

Warning: move_uploaded_file(upload/cap_bg.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\inetpub\vhosts\domain.com\subdomains\f\httpdocs\up\upload_pic_ac.php on line 11

Your code nice. I face a problems that I insert 60 image and ok but when I insert 61 Image then not insert. Image not insert,but when I insert name then insert.Image+name two field data not insert. Without image and single field insert.

Can you tell about how to catch the filename and filename1 into database?when i choose an image it doesn't catch filename and filename1 yet, although i click Upload button.but i want to let the filename and filename1 appear while i have choose the image file.....Can You Tell Me Please,,,, Please......Thank You Before...... :)

Cheers man, I sat down to write this myself to resize 2 images on the fly but a quick Google for the file extension function and I came accross this post so now you have saved me the time :) much appreciated.

Srinivas, i read your blog from 8,9 months and i've learned a lot of things from you. Now i've decided to thank you for all, and i'm sure you've helped a lot of people like me. Thanks again for your great job!

Hi, prior to PHP5.2 this script worked but i recently upgraded to 5.3.xx and i start getting 403 error below:"Access forbidden! You don't have permission to access the requested object. It is either read-protected or not readable by the server. If you think this is a server error, please contact the webmaster. Error 403 localhost 6/2/2012 8:59:05 AM Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1"Can you tell me what happened?Thanks in advance. Sameul

Great script sir!but i have a problem with this script that when i set image width like 500kb then it all times upload 500kb while when i use a small size of image like 200kb so it's not upload 200kb while it's upload 500kb.