A "better" function to generate thumbs for upload module.

Register an Account
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2009, 01:15 AM
bilicc's Avatar
New Pligger
Pligg Version: 1.0.4
 
Join Date: Jul 2009
Posts: 14
The original generate thumbs function of upload module has a "problem". When you set the thumb size, let's say, 512x280, then you upload a image 700x466, you will get a thumb image 420x280, not 512x280 you expected! Because the algorithm inside the function only calculate the ratio, if your upload image ratio(width/height) is not equal to thumb size ratio, you'll get an image either width equals to thumb size width or height equals to thumb size height, not both. So I change the function, it will cut the extra, so the thumb image size will equal the size you've set exactly! Open file module/upload/upload_main.php, find the function generate_thumbs, replace it will the one below(You should backup your file first):
PHP Code:
function generate_thumbs($fname,$link_id,$settings,$orig_id,$only_size='')
{
    global 
$db$current_user;

    if (!(
$str = @file_get_contents($fname)))   return "Can't read file $fname"
    if (!(
$img = @imagecreatefromstring($str))) 
    return; 
    else    
    
$db->query("UPDATE ".table_prefix."files SET file_ispicture=1 WHERE file_id='$orig_id'");

    if (!
$settings['thumb']) return;
    if (!
$settings['sizes']) return;
    
$thumb_dir mnmpath $settings['thdirectory'];

    
// load image and get image size
    
$width  imagesx$img );
    
$height imagesy$img );
    
$error  '';
    foreach (
$settings['sizes'] as $size)
    {
    if (!
strstr($size,'x') || ($only_size && $only_size!=$size)) continue;
    list(
$maxw,$maxh) = split('[x]',$size);
    if (
$maxw <= || $maxh <= 0) continue;

    
// Thumbnail file name
    
if (preg_match('/([^\/]+)\.[^\/]+$/',$fname,$m) || preg_match('/([^\/]+)$/',$fname,$m))
        
$name $m[1];
    else
        
$name $fname;
    
$name "$name$size";
    
    
// calculate thumbnail size
    
$new_width $maxw;
    
$new_height $maxh;

    
$ratio_w=1.0 $new_width $width;
    
$ratio_h=1.0 $new_height $height;
    
$ratio=1.0;
    if( (
$ratio_w && $ratio_h 1) || ($ratio_w && $ratio_h 1)) 
    {
        if(
$ratio_w $ratio_h) {
            
$ratio $ratio_h
        }else {
            
$ratio $ratio_w;
        }        
        
$inter_w=(int)($new_width $ratio);
        
$inter_h=(int) ($new_height $ratio);
        
$inter_img=imagecreatetruecolor($inter_w $inter_h);
        
imagecopy($inter_img$img0,0,0,0,$inter_w,$inter_h);
        
$new_img=imagecreatetruecolor($new_width,$new_height);
        
imagecopyresampled($new_img,$inter_img,0,0,0,0,$new_width,$new_height,$inter_w,$inter_h);
    
        while (
file_exists("$thumb_dir/$name$i.jpg")) $i++;
        
$name "$name$i.jpg";
    
          if (!
imagejpeg$new_img"$thumb_dir/$name",$settings['quality'] ))
        
$error .= "Can't create thumbnail $thumb_dir/$name";
    }
    else
    {
        
$ratio=$ratio_h>$ratio_w$ratio_h $ratio_w;
        
$inter_w=(int)($width $ratio);
        
$inter_h=(int) ($height $ratio);
        
$inter_img=imagecreatetruecolor($inter_w $inter_h);
        
imagecopyresampled($inter_img,$img,0,0,0,0,$inter_w,$inter_h,$width,$height);
        
$new_img=imagecreatetruecolor($new_width,$new_height);
        
imagecopy($new_img$inter_img0,0,0,0,$new_width,$new_height);

        while (
file_exists("$thumb_dir/$name$i.jpg")) $i++;
        
$name "$name$i.jpg";
    
          if (!
imagejpeg$new_img"$thumb_dir/$name",$settings['quality'] ))
        
$error .= "Can't create thumbnail $thumb_dir/$name";
    }
    if(
$error == '')
        
$db->query("INSERT INTO ".table_prefix."files 
                SET file_size='$size',
                    file_orig_id='$orig_id',
                    file_user_id={$current_user->user_id},
                    file_link_id=$link_id,
                    file_ispicture=1,
                    file_real_size='"
.filesize("$thumb_dir/$name")."',
                    file_name='"
.$db->escape($name)."'");
    }
    return 
$error;

Save and upload, upload a new image(One condition:your upload image's width and height should greater than the thumb size you've set) and see what's the difference.
You can also see the function working in my upcoming site here:diggmovie.com, which I use the silverbullet template by skins4webs , and I've done some modification on it. Sorry for my poor English.

Last edited by bilicc; 07-21-2009 at 03:39 AM.
Reply With Quote
  #2 (permalink)  
Old 07-22-2009, 02:53 PM
New Pligger
 
Join Date: Jun 2009
Posts: 24
I was wondering this the other day when I started getting tiny thumbs. I'll try it out.

Update: This worked perfectly, thanks!

Last edited by alpapa23; 07-23-2009 at 07:40 PM.
Reply With Quote
  #3 (permalink)  
Old 07-23-2009, 10:52 PM
bilicc's Avatar
New Pligger
Pligg Version: 1.0.4
 
Join Date: Jul 2009
Posts: 14
my pleasure
Reply With Quote
  #4 (permalink)  
Old 08-25-2009, 05:02 AM
New Pligger
Pligg Version: 1.0 RC
 
Join Date: Aug 2009
Posts: 4
Quote:
Originally Posted by bilicc View Post
my pleasure

Thanks, it's work for me. But how to remove this disturbing message: "512x280 {image1_512x280}".

Sorry for my poor english

Reply With Quote
Reply

Tags
generate thumbs

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Video Plus - Embed Videos from Popular Web Hosts Yankidank Free Modules 434 02-09-2012 08:54 PM
Upload Module (File and Image Attachment) Yankidank Free Modules 268 02-03-2012 12:19 PM
Fixes to Profile Extra Fields Module v0.2 and Extended Profile Module v0.1 redwine Questions and Comments 11 12-20-2010 04:18 PM


Pligg Modules and Pligg Templates from Pligg Pro Find support on the Pligg CMS Forum - 24 hours a day! Make a donation to support Pligg CMS development