CodeCharge Studio
search Register Login  

Visual Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 A good script to create thumbnails with two dimensions restrictions

Print topic Send  topic

Author Message
GrzegorzL

Posts: 13
Posted: 02/04/2009, 3:29 PM

Maybe someone has a good script to create thumbnails with two dimensions restrictions.
A good-quality images at the same time making use of the gd library.
I tried to use the old script from 2004

http://forums.yessoftware.com/posts.php?post_id=27174

but it does have errors. with CCS 4.1

The script - which I work only to have the width dimensions restriction
View profile  Send private message
maxhugen

Posts: 272
Posted: 02/04/2009, 4:17 PM

I created to following to create a thumbnail of 100x100, but without distorting the aspect ratio (it crops part of the image as required.) Maybe the script will help you? You can see the results at www.gardenloco/res/.
function gl_ImageThumbnail($filename, $newfilename="", $max_width=100, $max_height=100, $withSampling=true) {     
    /* Create and save a thumbnail of the image, 100x100, cropped as necessary  
       so that the original aspect ratio is preserved */  
  
    $width = 0;    
    $height = 0;    
    
    $newwidth = 0;    
    $newheight = 0;    
  
    $offset_x = 0;  
    $offset_y = 0;  
  
    // check that that the file is in a format we can deal with  
    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));  
    switch ($ext) {  
        case 'jpg':  
        case 'jpeg':  
        case 'gif':  
        case 'png':  
            break;  
        default:  
            return false;  
    }  
  
    
    // If no new filename was specified, set the default    
    if(!$newfilename){   
        $path = pathinfo($filename, PATHINFO_DIRNAME);  
        $name = pathinfo($filename, PATHINFO_BASENAME);  
        $newfilename = $path."/tn/"."tn.".$name;     
    }    
        
    // Get original sizes     
    list($width, $height) = getimagesize($filename);     
  
    // Check if we need to resize for thumbnail  
    if ($width > $max_width || $height > $max_height)  
        $resize=true;  
  
    if ($resize) {  
        if ($width > $height) {    
            // We're dealing with max_width, so resize to suit  
            // height, then determine offset to crop width.  
            $newheight = $max_height;  
            $newwidth = ceil($width * ($max_height / $height));  
            $offset_x = ceil(($newwidth-$max_width)/2);  
        } else {  
            // We're dealing with max_height, so resize to suit  
            // width, then determine offset to crop height.  
            $newwidth = $max_width;  
            $newheight = ceil($height * ($max_width / $width));  
            $offset_y = ceil(($newheight-$max_height)/2);  
        }  
/*  
fb("newheight: ".$newheight);  
fb("newwidth: ".$newwidth);  
fb("offset_x: ".$offset_x);  
fb("offset_y: ".$offset_y);  
*/  
        // Create new image objects     
        $temp  = imagecreatetruecolor($newwidth, $newheight);     
        $thumb = imagecreatetruecolor($max_width, $max_height);     
    
        // Load the original based on it's extension    
        if ($ext=='jpg' || $ext=='jpeg')   
            $source = imagecreatefromjpeg($filename);     
        else if ($ext=='gif')    
            $source = imagecreatefromgif($filename);     
        else if ($ext=='png')     
            $source = imagecreatefrompng($filename);     
        
        // Resize the image with sampling if specified    
        if ($withSampling) {    
            imagecopyresampled($temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  
        } else     
            imagecopyresized(  $temp, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);     
  
        // Crop the resized image  
        imagecopy($thumb, $temp , 0, 0, $offset_x, $offset_y, $max_width, $max_height);  
        imagedestroy($temp);  
  
    } else {  
        // Create the thumbnail from original, based on it's extension    
        if ($ext=='jpg' || $ext=='jpeg')   
            $thumb = imagecreatefromjpeg($filename);     
        else if ($ext=='gif')    
            $thumb = imagecreatefromgif($filename);     
        else if ($ext=='png')     
            $thumb = imagecreatefrompng($filename);    
    }  
        
    // Save the new image     
    if ($ext=='jpg' || $ext=='jpeg')     
        return imagejpeg($thumb, $newfilename);     
    else if ($ext=='gif')     
        return imagegif($thumb, $newfilename);     
    else if ($ext=='png')     
        return imagepng($thumb, $newfilename);     
}  

HTH
_________________
Max
www.gardenloco.com | www.eipdna.com | www.chrisarminson.com
View profile  Send private message
GrzegorzL

Posts: 13
Posted: 02/09/2009, 1:10 AM

Thanks a lot.

I find this page yet http://pl.php.net/imagecopyresampled
This may be useful to someone
View profile  Send private message
datadoit
Posted: 02/09/2009, 5:29 AM

PHP and the available tools for it are just getting out of the this
world! For the life of me I can't understand why anyone chooses any
other language. :)

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.