<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>
//Display file name, temp name and file type ,test your script///
echo "File Name: ".$_FILES['userfile']['name']."<br>";
echo "tmp name: ".$_FILES['userfile']['tmp_name']."<br>";
echo "File Type: ".$_FILES['userfile']['type']."<br>";
echo "<br><br>";
/////////////////////////////////////////////////////////////
// the path with the file name where the file will be stored,
// upload is the directory name.
$add="upimg/".$_FILES['userfile']['name'];
//echo $add;
if(move_uploaded_file ($_FILES['userfile']['tmp_name'],$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/".$_FILES['userfile']['name']; // Path where thumb nail image will be stored
//echo $tsrc;
if (!($_FILES['userfile']['type'] =="image/jpeg" OR $_FILES['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. We have used function_exists() to check supported image functions
if($_FILES['userfile']['type']=="image/gif")
{
$im=ImageCreateFromGIF($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
$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);
}////////// end of gif file thumb nail creation/////
////////////// starting of JPG thumb nail creation////
if($_FILES['userfile']['type']=="image/jpeg"){
$im=ImageCreateFromJPEG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
// Add this line to maintain aspect ratio
$n_height=($n_width/$width) * $height;
$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);
}
if (!($_FILES['userfile']['type'] =="image/jpeg" OR $_FILES['userfile']['type']=="image/gif" OR $_FILES['userfile']['type']=="image/png")){
echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
exit;}
Include these lines aftr JPG cration.
////////////// starting of PNG thumbnail creation//////////
if($_FILES['userfile']['type']=="image/png"){
$im=ImageCreateFrompng($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
Imagepng($newimage,$tsrc);
chmod("$tsrc",0777);
}
//////////////// End of PNG thumbnail creation //////////
$n_height=($n_width/$width) * $height;
parteek kumar | 24-07-2014 |
please give an best and short idea for make thubnail after image upload in php. |
Melvin | 15-10-2014 |
I will try your code soon.... |
Pooya.S | 17-10-2014 |
Great . Thank you very much. |
proPhet | 09-12-2014 |
The above code works well, thanks! However it does not allow for aspect ratio preservation. |
smo | 12-12-2014 |
One line of code is to be added to proportionately manage the height of thumbnail image. That part is added now. Thanks. |
navjit singh | 14-01-2015 |
thanx above code is very helpfull |
success4gande | 22-05-2015 |
Sir, How will the image upload and thumbnail php script look like if it was to be in an application form where applicant will need to upload image and immediately the browser uploads then the image previews or thumbnails in the form? |
smo1234 | 22-05-2015 |
To display thumbnail the file has to be uploaded first. Displaying directly from user local computer is not possible. |
Php Expert Programmer | 15-06-2017 |
Great tutorial. This tutorial was really helpful to create a thumbnail images. Thanks for this tutorial. |