Tutorial: How to make an Image Upload and Thumbnailer Script PHP

Tutorial: How to make an Image Upload and Thumbnailer Script PHP

4. The last 4 lines of this script, is where the cpu kicks in. Lets face it, it was getting lonely.

	$thumbnail = imagecreatetruecolor($thumbwidth, $thumbheight);
				imagecopyresampled($thumbnail, $srcimage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $srcimagex, $srcimagey);
				$success = imagejpeg($thumbnail, $thumbspath.$filename, 75);
				unlink($tmppath.$filename);

The first line sets up the canvas for the new image, with fancy colours. This resource (fancy word for where our canvas is stored) is stored into the $thumbnail variable aka pointer. Next up is imagecopyresampled($thumbnail, $srcimage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $srcimagex, $srcimagey);. This line basically transfer the large source canvas, shrinks it down with the dimensions you provide it, then smacks it *SMACK* onto the smaller thumbnail canvas. Good visualisation hey.

Next we want to save that smacked out image, or now thumbnail to a file. We choose jpeg compression because its nice. It is saved via this line: $success = imagejpeg($thumbnail, $thumbspath.$filename, 75); We first give it the resource, the $thumbnail canvas, then we set where we want to save it (remember those paths we set earlier) and a quality level out of 100, 3/4′s is nice to have. And yes, compressing a compressed image is bad, but you gotta do it. If this action was successful it outputs a 1, or a yes/true, if it failed it outputs a 0. We save this value for later, because its tasty.

Last file is not 100% needed, but if you have 1 trillion webusers on your website like i wish i had, then you will want to have it, or buy every hard drive known to man. So we will delete the temporary files, or the full sized images in the tmp directory we made. This is done by using the unlink() command, you just got to tell it what to delete. Which we did: unlink($tmppath.$filename);

Right you’re done!
Test your script, then look in the thumbs directory. Wow, thumbnails! But wait. Maybe we want to share our images with friends. Or maybe we don’t want them to look in our thumbs directory.

Posted by VoiDeT

Categorised under PHP
Bookmark the permalink or leave a trackback.

7 Comments

  1. paparanch

    i don’t whats the problem with this because when i uploaded a gif file…its not animating anymore…why?

    June 16, 2008 @ 2:21 am
  2. pouria

    hi :)
    i recive this error with every kind of images :
    File is not an image! Don’t waste my time!

    so whats the problem?

    August 7, 2008 @ 8:29 pm
  3. That’s cool. I test it and it really Great!!

    October 8, 2008 @ 1:15 pm
  4. rcarlos3

    i have the same problem as “pouria”,

    with FF its ok, but with IE it doesnt accept the imagees..

    whats the problem?

    February 19, 2009 @ 6:16 am
  5. ehi

    can you tell me how i can make a script that will list the uploaded image on a page for other people to use because the page is for site link back images

    March 28, 2009 @ 9:24 am
  6. VoiDeT

    Either use a database to reference to the files, or write a directory listing script! :) Plenty of them out there! My other tutorial shows you how to insert into a database also! Thank you

    March 28, 2009 @ 12:45 pm
  7. great work :D

    April 15, 2009 @ 9:05 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or