Results 1 to 4 of 4
  1. #1
    New Pligger bilicc's Avatar
    Joined
    Jul 2009
    Posts
    14

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

    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:tongue:.
    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.

  2. #2
    New Pligger alpapa23's Avatar
    Joined
    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!

  3. #3
    New Pligger bilicc's Avatar
    Joined
    Jul 2009
    Posts
    14
    my pleasure

  4. #4
    New Pligger bloggermania's Avatar
    Joined
    Aug 2009
    Posts
    4

    It's work

    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


Similar Threads

  1. Insert images in rss feeds uploaded with "Upload Module"
    By Leonel78 in forum Questions & Comments
    Replies: 0
    Last Post: 06-24-2011, 02:20 AM
  2. Is There AModule For "Thumbs Up" & "Thumbs Down" of article?
    By Johnathan in forum Questions & Comments
    Replies: 1
    Last Post: 06-21-2010, 11:47 PM
  3. Simple Question about thumbs and Upload module
    By lipsmega in forum Questions & Comments
    Replies: 0
    Last Post: 02-21-2010, 01:07 PM
  4. simple "image upload -> link image" module
    By jirihrabakne in forum Questions & Comments
    Replies: 1
    Last Post: 02-09-2009, 10:12 PM
  5. Replies: 2
    Last Post: 02-06-2009, 06:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Pligg Modules and Pligg Templates from Pligg Pro Donate to Pligg CMS Dreamhost Web Hosting Host Gator Web Hosting