Resize/Rename Images with PHP
December 16, 2007 10:01 PM   Subscribe

Resizing/renaming images with PHP and keeping only the thumbnail

So I am very fresh when it comes to PHP. I started a little project to get my feet wet. Basically, I have a form. Lets say it consist of a field for your name, email address, a small text blurb, and a spot to upload a picture (actually, two pictures). When you submit the form, your name, email, and blurb are entered into a template below. The form is still on the page and you can change anything you don’t like and submit again if you would like.

Here is where I get stuck:
I want to take the images and resize them to a maximum width or height of 200px

I only want to save the thumbnails to a thumbnail directory – not the originals

I want to rename thumbnails to something like date+time+random_string.JPG/GIF/PNG/ETC

I then want to take that thumbnail and insert it into the template (I can do this)

I do not want to re-upload and resize the image if they change text in their blurb and resubmit the form.
I just need some direction here. Any resources you can point me to would be great. If you have better ideas, I would love to hear them. If you want to look at the page, to get a better idea of what I have going on, MeFiMail and I will send you a link. Don’t really want to post a link directly as I think my work kinda sucks.

Ohh yeah. I am hosted on Dreamhost and have access to GD Library and ImageMagick but have no clue how to use either yet (although I will learn if pointed in that direction).
posted by B(oYo)BIES to Computers & Internet (6 answers total) 2 users marked this as a favorite
 
I've experimented with this type of thing in PHP. Have you tried digging around at php.net? Lots of examples, for instance how to resample an image and how to save it to a file. All the relevant functions should be in the sidebar.

You'll need to be sure to specify the encoding type in the form since you're working with file uploads. enctype="multipart/form-data". That tripped me up the first time.
posted by Jeff Howard at 11:21 PM on December 16, 2007


Looking at the examples Jeff Howard should get you most of the way there. When a user uploads a file, the $_FILES superglobal will contain the image's temporary location. You can use the image functions (see Jeff Howard's link) to the image from that temp location into memory and then save the resized image.
posted by !Jim at 2:10 AM on December 17, 2007


I haven't used it yet, but I've had phpThumb bookmarked for the next time I need to do thumbnail generation.
posted by gwint at 5:32 AM on December 17, 2007


The nice thing about PHP is that you can do this without actually creating new images.

Learning PHP from O'Reilly has a good section on doing this sort of thing, but unfortunately my copy is at home right now...

So here's some code for resizing (this takes the original file and makes it six times smaller....
ImageCopyResampled is the function you want to look into


$to_open = $_GET['a'];
$src = ImageCreateFromJPEG($to_open);
$width =ImageSx($src);
$height = ImageSy($src);
$x = $width/6;
$y = $height/6;
$dst = ImageCreateTrueColor($x,$y);
ImageCopyResampled($dst,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-Type: image/png');
ImagePNG($dst);

posted by drezdn at 7:42 AM on December 17, 2007




a is the filename IIRC.
posted by drezdn at 7:42 AM on December 17, 2007


Also, the code above doesn't really solve your problem the way you want to solve it, it essentially creates a PNG file from a JPG on the fly.

You'll have to figure out how to save that file if you want, but this should show you some of the functions to use.
posted by drezdn at 7:44 AM on December 17, 2007


« Older Where I can I find the heavenly equivalent of form...   |   Bejeweled.. But Not Newer »
This thread is closed to new comments.