Thumbnail Creation using uploadify.php
anoopsachari | Jun 14, 2010 | Comments 13
Hello friends i came across a good jquery flash file up-loader here . Do use it its simple and powerful .
The next main thing was generation of thumbnail using the same flash uploader after some homework found a solution for it . Do some simple updations in your existing code .
1. Create a folder named “thumbnails” under the upload folder .
2. Replace the code in uploadify.php [you can find it under scripts folder] with the following code .
if (!empty($_FILES))
{
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo "1";
}
$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3)))
{
case "jpg":
$image = imagecreatefromjpeg($targetFile);
break;
case "png":
$image = imagecreatefrompng($targetFile);
break;
case "gif":
$image = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width = 80; //New width of image
$height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions
$src_w = $imgsize[0];
$src_h = $imgsize[1];
$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
if($bool)
{
switch(strtolower(substr($targetFile, -3)))
{
case "jpg":
header("Content-Type: image/jpeg");
$bool2 = imagejpeg($picture,$targetPath."thumbnails/".$_FILES['Filedata']['name'],80);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture,$targetPath."thumbnails/".$_FILES['Filedata']['name']);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture,$targetPath."thumbnails/".$_FILES['Filedata']['name']);
break;
}
}
imagedestroy($picture);
imagedestroy($image);
echo '1';
You are done ! Hope it helped you .
Filed Under: ajax
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.







it’s well, i will to do it but not work;
now, i’m trying to do from your example;
i hope it work
oh! i use multiupload : true . Is it support?
@teerapong yes , its supports multiupload.
Hello Achari
Your code is very helpful!
But, is it possible to specify the Height rather than the Width? If so, how would that be done?
TIA
sorry, done, i just changed this…
$height = 130; //New height of image
$width = $imgsize[0]/$imgsize[1]*$height; //This maintains proportions
$src_h = $imgsize[1];
$src_w = $imgsize[0];
@Ian Loats Thankyou for your update . i would check and include it in my code .
Nice Code.. thanks
Thanks a lot you are the best my friend
Will this work with pdf files or do changes need to be made? If changes need made, what would they be. Thanks!
sir i create post command in my web site .
pls send the correct coding in mail.
pls.
by balaji
tamil nadu??
Wonderful script!
But… If I want a squared thumbnail?
How can I crop the image?
Hi I don’t know abt PHP but I’m working with asp.net can you place the same script for asp.net too so that it might help me and even other too.
Thanks once again.
Hi, nice script but i have a problem:
your script doesn’t rotate image (i get thumbs even horizontal also when the image are vertical)
i’ll try to fix it in this way:
$src_w = $imgsize[0];
$src_h = $imgsize[1];
$width = $src_w * 0.20; //20% of the orig image
$height = $src_h * 0.20; //20% of the orig image
$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
but doesn’t work
any idea how can i solve this? thank you
yaa..its really working!!!!