1
urbanspacema
Thumbnail creation after/during upload

Ciao, there is a function in XOOPS 2.3.3 for making a thumbnail from uploaded image?

if no, this code is always valid?
http://dev.xoops.org/modules/xfsnippet/detail.php?type=snippet&snippet_id=1

tnx
Urban

2
urbanspacema
Re: Thumbnail creation after/during upload

ok i solved and sharing the code.

the functions
function timemachine_createthumb($name$path$savepath$new_w=100$new_h=100$quality=80){

    
$savefile $savepath.$name;
    
$savepath $savefile;
    
    if(!
file_exists($savepath)){
        
            
// Get image location
            
$image_path $path ."/".$name;
            
            
// Load image
            
$img null;
            
$ext end(explode('.'$image_path));
            if (
$ext == 'jpg' || $ext == 'jpeg') {
                
$img = @imagecreatefromjpeg($image_path);
            } else if (
$ext == 'png') {
                
$img = @imagecreatefrompng($image_path);
            
// Only if your version of GD includes GIF support
            
} else if ($ext == 'gif') {
                
$img = @imagecreatefrompng($image_path);
            }
            
            
// If an image was successfully loaded, test the image for size
            
if ($img) {
            
                
// Get image size and scale ratio
                
$width imagesx($img);
                
$height imagesy($img);
                
$scale min($new_w/$width$new_h/$height);
            
                
// If the image is larger than the max shrink it
                
if ($scale 1) {
                    
$new_width floor($scale*$width);
                    
$new_height floor($scale*$height);
            
                    
// Create a new temporary image
                    
$tmp_img imagecreatetruecolor($new_width$new_height);
            
                    
// Copy and resize old image into new image
                    
imagecopyresized($tmp_img$img0000,
                                     
$new_width$new_height$width$height);
                    
imagedestroy($img);
                    
$img $tmp_img;
                }
            }
            
            
// Create error image if necessary
            
if (!$img) {
                
$img imagecreate($new_w$new_h);
                
imagecolorallocate($img,0,0,0);
                
$c imagecolorallocate($img,70,70,70);
                
imageline($img,0,0,$new_w,$new_h,$c2);
                
imageline($img,$new_w,0,0,$new_h,$c2);
            }
    
            
            
// output the image as a file to the output stream

            
Imagejpeg($img,$savepath,$quality);

    }
    return 
$savefile;
}


the upload
//Form birthday_img    
        
include_once XOOPS_ROOT_PATH."/class/uploader.php";
        
$uploaddir_birthday_img XOOPS_ROOT_PATH."/uploads/timemachine/birthday/birthday_img/";
        
$uploaddir_birthday_thumbs_img XOOPS_ROOT_PATH."/uploads/timemachine/birthday/birthday_thumbs_img/";
        
$uploader_birthday_img = new XoopsMediaUploader($uploaddir_birthday_img$xoopsModuleConfig["birthday_img_mimetypes"], $xoopsModuleConfig["birthday_img_size"], nullnull);

        if (
$uploader_birthday_img->fetchMedia("birthday_img")) {
            
$uploader_birthday_img->setPrefix(str_replace(" ","_",$_REQUEST["birthday_who"]));
            
$uploader_birthday_img->fetchMedia("birthday_img");
            if (!
$uploader_birthday_img->upload()) {
                
$errors $uploader_birthday_img->getErrors();
                
redirect_header("javascript:history.go(-1)",3$errors);
            } else {
                
$obj->setVar("birthday_img"$uploader_birthday_img->getSavedFileName());
                
                
//genero la thumbnail
                
$name $uploader_birthday_img->getSavedFileName();
                
$path $uploaddir_birthday_img;
                
$savepath =$uploaddir_birthday_thumbs_img;
                
                
$new_w $xoopsModuleConfig["birthday_img_thumb_width"];
                
$new_h $xoopsModuleConfig["birthday_img_thumb_height"];
                
$thumb timemachine_createthumb($name$path$savepath$new_w$new_h$quality=80);
            }


note
$xoopsModuleConfig["birthday_img_thumb_width"] and other, are in my module preferences.

3
Peekay
Re: Thumbnail creation after/during upload
  • 2010/11/13 23:53

  • Peekay

  • XOOPS is my life!

  • Posts: 2335

  • Since: 2004/11/20


As derya pointed out in the code snippet comments
Quote:
Should be better to use imagecopyresampled instead of imagecopyresized if your temp img is created with imagecreatetruecolor, because it keeps truecolor.

This makes a BIG difference to the thumbnail quality if they are any larger than an avatar.
A thread is for life. Not just for Christmas.

Login

Who's Online

149 user(s) are online (55 user(s) are browsing Support Forums)


Members: 0


Guests: 149


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Dec 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits