SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

PHP Thumbnail Image creation script

Creating thumbnail images is a very common requirement. Many scripts use this to create thumbnails of uploaded images or any stored images. We will learn how to create thumbnail images while uploading images to the server. Please read the tutorials on file upload to know how to upload files to the server.Here is the file to create the form to upload the file. This is same as our file upload tutorial

<FORM ENCTYPE="multipart/form-data" ACTION="addimgck.php" METHOD=POST>
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>

 
We will now see the addimgck.php file and check the code to create the thumbnail image. We will upload the file to the upimg directory and check to see if file upload is successful or not.

$add="upimg/$userfile_name"; // the path with the file name where the file will be stored, upload is the directory name.
if(move_uploaded_file ($userfile, $add)){
echo "Successfully uploaded the mage";
chmod("$add",0777);
}else{echo "Failed to upload file Contact Site admin to fix the problem";
exit;}


Now the image is uploaded to the directory and from that image we will create thumbnail image of it. We will first set the height and width of the thumbnail  images to be  generated. Then we will check the type of the file and now we are checking for file type of gif and jpeg and if the image is not of this type then we are terminating the script giving an error message.


///////// Start the thumbnail generation//////////////
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail imaage
$tsrc="thimg/$userfile_name"; // Path where thumb nail image will be stored

if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
exit;}


Now let us start with gif file thumb nail image creation. We will first read the height and width of the uploaded image and then resize it to our thumbnail image size. Note that in some GD library support GIF version is not included so to check that we have used one if condition and  accordingly used jpeg support.  We will be using imagecreatetruecolor to retain the actual color combination of the main picture.

/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
if (@$userfile_type=="image/gif"){
$im=ImageCreateFromGIF($add);
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);            // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);

if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$tsrc);
}
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$tsrc);
}

chmod("$tsrc",0777);


At the end we have given proper file permission to manage the file. Now we will move to JPG file creation.

////////////// starting of JPG thumb nail creation//////////
if($userfile_type=="image/pjpeg"){
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$tsrc);
chmod("$tsrc",0777);
}
//////////////// End of JPG thumb nail creation //////////


Same way we can add other graphics format like BMP, PNG etc to the system

Download the ZIP file here  thumbnail.zip

Discuss this tutorial at forum


 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.