|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Making Thumbnails with PHP | ||
Chipmunk![]() Rank:Settler of Bobland Group: Head Administrator Posts: 2867 IP Logged PM ID and RPS ID: 1 PM [Chipmunk] View Member Photo | Last replied to on Wed Aug 08, 2007 21:33:37 Edit Post|Quote This tutorial will teach you how to make thumbnails from an upload script in PHP. I will use a 100*100 thumbnail as an example. You can change the dimensions to whatever you like. We will need to create 2 files, form.php and action.php. You also need to create a folder to store your photos that you created. Make sure that folder is chmodded to 777 so it will be able to create and store the thumbs in that folder. The action.php file should also go in the photos folder. For this script I'll create th folder photos. Now for the form.php file:
Ok, this is the basic form. As you can see if only has 1 field, the field that lets you cchoose the file you want to upload. Note that the input type must be file and enctype must be multipart/form-data in order for this to work, otherwise the file will not upload. The action is action on ../photos/upload.php because thats where the actual form to upload the thumbnails are. Now the action.php file:
This file is a bit complicated. In this file we specify the dimensions of the thumbnail, the types of file extensions that are allowed, what the name of the thumbnail is, creating a thumbnail from the actual picture, and resizing it. First $new_height and $new_width are our specified dimension of the thumbnails, you can change these to whatever you want to suit your needs. Next in $allowed_types, we created an array with the types of files that are allowed to be uploaded. As you can see we listed the common extension and types for .gif and .jpg file extensions. Next with the if(in_array) condition, we check if the file being uploaded was one of the allowed types, if it wasn't, it will display a message "only gifs and jpegs are allowed". If it is one of the allowed types then it will check to see whether it was a .gif or a .jpg and based on that if will either tell it to go to the .gif create image case or the .jpg create image case. $img is actually the variable that holds the created image and the functions @imagecreatefromgif and @imagecreatefromjpg are the two function that actually create a thumnail for either a gif or a jpg image. imagecopyresampled is a function that resizes the image we just created to the dimensions we specified earlier, the fields where I put zeros in are the field where you can specify color dilution within this function. If your just trying to create thumbs, leave these as zeros. $image_p now holds the resampled $img. The next few lines specify the location where the new image created will go and finally the imagegif or imagejpg function will put the new image in the place we specified. Now if you go into your photos folder you should see a 100*100 thumbnail of the image you just created. ----------------------------- Chipmunk, Supreme Administrator | ||
| jalejandrocp Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 13319 [PM jalejandrocp] RPS score: 0 RPS challenge | Posted at Wed Aug 08, 2007 21:33:37 Edit post|Quote HELLO, I HAVE MODIFIED THIS CODE TO DO THUMBNAILS, ADDING TO IT 3 LINES OF CODE. IT WAS FOR MAKING THAT THUMBNAILS NOT DESPROPORTIONATE. SORRY FOR MY ENGLISH JUST ADD TE CODE BETWEEN : //between this code... list($width, $height) = getimagesize($_FILES['thefile']['name']); //////////// MY CODE /////////////////////////// Crear tamaño del thumbnail en proporcion a la imagen.// $new_height = 100;// $porc_width = $new_height/$height;// $new_width = $porc_width*$width; $new_width = 100; $porc_height = $new_width/$width; $new_height = $porc_height*$height;//////////// MY CODE ///////////////////////// //... and between this code... $image_p = imagecreatetruecolor($new_width,$new_height); | ||
Page: 1 |