I have been trying to find a way to create a thumbnail file upon uploading an image. So basically, after the image has been uploaded, I will need to call a function to copy the image, resize it, and rename it to, for example, imagename_thumb.gif
I've got a working script to resize and rename a file in the directory. However, I am a PHP beginner and really struggles in finding out how to do this over the MCPUK mode.
All I can think of is this:
On the filemanager/browser/mcpuk/frmupload.html, on this line:
<form id="frmUpload" action="" target="frmUploadWorker" method="post" enctype="multipart/form-data" onSubmit="return OnSubmit();">
.. I put another onSubmit method, eg CreateThumbnail(), so it would be like this:
<form id="frmUpload" action="" target="frmUploadWorker" method="post" enctype="multipart/form-data" onSubmit="return OnSubmit(); CreateThumbnail()">
The problem is, I need to know which folder the file currently resides, and the name of the file I have just uploaded. Furthermore, I need to be able to automatically rewrite the old thumbnail, if exists.
Can someone help me on how to put this together?
Anyways, here's the script that I have tried (separately from fckeditor) to resize an image:
Thanks in advance for your help. I really need this. Thanks!
I've got a working script to resize and rename a file in the directory. However, I am a PHP beginner and really struggles in finding out how to do this over the MCPUK mode.
All I can think of is this:
On the filemanager/browser/mcpuk/frmupload.html, on this line:
<form id="frmUpload" action="" target="frmUploadWorker" method="post" enctype="multipart/form-data" onSubmit="return OnSubmit();">
.. I put another onSubmit method, eg CreateThumbnail(), so it would be like this:
<form id="frmUpload" action="" target="frmUploadWorker" method="post" enctype="multipart/form-data" onSubmit="return OnSubmit(); CreateThumbnail()">
The problem is, I need to know which folder the file currently resides, and the name of the file I have just uploaded. Furthermore, I need to be able to automatically rewrite the old thumbnail, if exists.
Can someone help me on how to put this together?
Anyways, here's the script that I have tried (separately from fckeditor) to resize an image:
<?php // find out the current size info $photo_filename = "big.jpg"; $new_photo_filename = "big_thumb.jpg"; $path = "/var/www/vhosts/theurl.com/images/"; $image_stats = GetImageSize($path.$photo_filename); $imagewidth = $image_stats[0]; $imageheight = $image_stats[1]; $img_type = $image_stats[2]; $new_w = 100; $ratio = ($imagewidth / $new_w); $new_h = round($imageheight / $ratio); // Find out if we need to resize it by checking to // see if the file needs resizing and making sure the // resized version does not exist yet if (($imagewidth > $new_w)&& (!file_exists($path.$new_photo_filename))) { // if this is a jpeg, resize as a jpeg if ($img_type==2) { $src_img = imagecreatefromjpeg($path.$photo_filename); $dst_img = imagecreate($new_w,$new_h); imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); $imageresize = imagejpeg($dst_img, $path.$new_photo_filename); if (!$imageresize) { echo ('Failed'); } else { echo ('Succeed'); } } elseif ($img_type==3) { // if image is a png, copy it $dst_img=ImageCreate($new_w,$new_h); $src_img=ImageCreateFrompng($path.$photo_filename); ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img)); Imagepng($dst_img, $path.$new_photo_filename); // Normally if image is neither png nor jpeg (ie, invalid image or a gif file), I use // the fullsize as the thumbnail and just resize it through the html size tags. For this // example tho, we’ll pretend we have a hacked version of Gdlib } elseif ($img_type==1) { $dst_img=ImageCreate($new_w,$new_h); $src_img=ImageCreateFromGif($path.$photo_filename); ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img)); ImageGif($dst_img, $path.$new_photo_filename); } else { // if it doesn’t show up as any of the valid formats, give an error } // endif img_type sequence } ?>
Thanks in advance for your help. I really need this. Thanks!
RE: Create a thumbnail file when uploading?