Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2009, 11:48 PM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Mr. Thumb ( Resize Class )

Been awhile since I've posted something useful. I hope this is. This is a rebuild of my ThumbIt script, and my resizing snipper here.

Mr. Thumb is finally ported to OOP for easy use. Maybe someone will come across it here, and if so, I hope it's helpful!

Mr. Thumb does not cache it's images, I'm hoping to add it soon if I come back to this.

However it does support transparency for both PNG images and GIF images.

mrthumb.class.php
PHP Code:
<?php

    
/***************************************
    * Mr. Thumb 
    * Version 1.0 ( Build 1 )
    * By Jordan Thompson (WASasquatch)
    ****************************************/
    
     
class MrThumb {
     
        
// This is your array of supported image formats. 
        // Exclude types you don't want rendered.
        
var $valid_ext = array( 'png''jpg''jpeg''gif''bmp''wbmp' );
     
        
// Whether or not that script should continue
        
var $halt false;
        
        
// Image Configuration array and Source Image
        
var $image = array();
        var 
$s_image;
        
        
// Mr. Thumb Version
        
var $name 'Mr. Thumb';
        var 
$version '1.0';
        var 
$build 1;
        var 
$developer 'Jordan Thompson';
        var 
$contact 'jordanslost at gmail';

        public function 
about () {
        
            echo 
$this->name.' Version '.$this->version.' build '.$this->build;
            echo 
'<br />By '.$this->developer.' ('.$this->contact.')';

        }
        
        public function 
render $source ) {
        
            
$this->s_image $source;
            list( 
$this->image['width'], $this->image['height'] ) = getimagesize$source );
            
$this->image['extension'] = strtolowerpreg_replace'/^.*\.([^.]+)$/D''$1'$this->s_image ) );
            if ( ! ( 
in_array$this->image['extension'], $this->valid_ext ) ) ) {
                echo 
'Invalid format!';    
                
$this->halt true;    
            }
            switch ( 
$this->image['extension'] ) {
                case 
'png';
                    
$this->image['render'] = imagecreatefrompng$this->s_image );
                    
imagealphablending$this->image['render'], false );
                    
imagesavealpha$this->image['render'], true );
                break;
                case 
'jpg';
                    
$this->image['render'] = imagecreatefromjpeg$this->s_image );
                break;
                case 
'jpeg';
                    
$this->image['render'] = imagecreatefromjpeg$this->s_image );
                break;
                case 
'gif';
                    
$this->image['render'] = imagecreatefromgif$this->s_image );
                break;
                case 
'bmp';
                    
$this->image['render'] = imagecreatefromwbmp$this->s_image );
                break;
                case 
'wbmp';
                    
$this->image['render'] = imagecreatefromwbmp$this->s_image );
                break;
            }
        
        }
        
        public function 
contrain $width$height ) {
        
            if ( ! ( 
$this->halt ) ) {
                if ( 
$this->image['extension'] == 'gif' ) {
                    
$this->image['composite'] = imagecreatetruecolor$width$height );
                    
imagecopyresample$this->image['composite'], $this->image['render'], 0000$width$height$this->image['width'], $this->image['height'] );
                    
$this->image['colorcount'] = imagecolorstotal$this->image['render'] );
                    
imagetruecolortopalette$this->image['composite'], true$this->image['colorcount'] );
                    
imagepalettecopy$this->image['composite'], $this->image['render'] );
                    
$this->image['transparentcolor'] = imagecolortransparent$this->image['render'] );
                    
imagefill$this->image['composite'], 00$this->image['transparentcolor'] );
                    
imagecolortransparent$this->image['composite'], $this->image['transparentcolor'] );
                } else {
                    
$this->image['composite'] = imagecreatetruecolor$width$height );
                    
imagecopyresample$this->image['composite'], $this->image['render'], 0000$width$height$this->image['width'], $this->image['height'] );
                }
            } else {
                echo 
'Execution halted!';
            }
        
        }
        
        public function 
proportion $max_width$max_height ) {
        
            if ( ! ( 
$this->halt ) ) {
                if ( 
$this->image['extension'] == 'gif' ) {
                    
$this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width $this->image['width'] : $max_height/$this->image['height']; 
                    if( 
$this->image['width'] > $max_width || $this->image['height'] > $max_height ) { 
                        
$new_width $this->image['width'] * $this->image['ratio']; 
                        
$new_height $this->image['height'] * $this->image['ratio']; 
                    } else {
                        
$new_width $this->image['width']; 
                        
$new_height $this->image['height'];
                    } 
                    
$this->image['composite'] = imagecreatetruecolor$new_width$new_height );
                    
imagecopyresampled$this->image['composite'], $this->image['render'], 0000$new_width$new_height$this->image['width'], $this->image['height'] );
                    
$this->image['colorcount'] = imagecolorstotal$this->image['render'] );
                    
imagetruecolortopalette$this->image['composite'], true$this->image['colorcount'] );
                    
imagepalettecopy$this->image['composite'], $this->image['render'] );
                    
$this->image['transparentcolor'] = imagecolortransparent$this->image['render'] );
                    
imagefill$this->image['composite'], 00$this->image['transparentcolor'] );
                    
imagecolortransparent$this->image['composite'], $this->image['transparentcolor'] );
                } else {
                    
$this->image['ratio'] = ( $this->image['width'] > $this->image['height'] ) ? $max_width $this->image['width'] : $max_height/$this->image['height']; 
                    if( 
$this->image['width'] > $max_width || $this->image['height'] > $max_height ) { 
                        
$new_width $this->image['width'] * $this->image['ratio']; 
                        
$new_height $this->image['height'] * $this->image['ratio']; 
                    } else {
                        
$new_width $this->image['width']; 
                        
$new_height $this->image['height'];
                    } 
                    
$this->image['composite'] = imagecreatetruecolor$new_width$new_height );
                    
imagecopyresampled$this->image['composite'], $this->image['render'], 0000$new_width$new_height$this->image['width'], $this->image['height'] );
                }
            } else {
                echo 
'Execution halted!';
            }
        
        }
        
        public function 
output $quality 100 ) {
        
            if ( ! ( 
is_numeric$quality ) ) ) {
                
$quality 100;
            }
            if ( ! ( 
$this->halt ) ) {
                switch ( 
$this->image['extension'] ) {
                    case 
'png';
                        
header'Content-Type: image/png' );
                        
imagepng$this->image['composite'], nullnull );
                    break;
                    case 
'jpg';
                        
header'Content-Type: image/jpeg' );
                        
imagejpeg$this->image['composite'], null$quality );
                    break;
                    case 
'jpeg';
                        
header'Content-Type: image/jpeg' );
                        
imagejpeg$this->image['composite'], null$quality );
                    break;
                    case 
'gif';
                        
header'Content-Type: image/gif' );
                        
imagegif$this->image['composite'], null$quality );
                    break;
                    case 
'bmp';
                        
header'Content-Type: image/wbmp' );
                        
imagewbmp$this->image['composite'], nullnull );
                    break;
                    case 
'wbmp';
                        
header'Content-Type: image/wbmp' );
                        
imagewbmp$this->image['composite'], nullnull );
                    break;
                }
            } else {
                echo 
'Execution halted!';
            }
        }
        
        public function 
saveto $destination$filename$quality 100 ) {
        
            if ( ! ( 
is_numeric$quality ) ) ) {
                
$quality 100;
            }
            if ( ! ( 
$this->halt ) ) {
                switch ( 
$this->image['extension'] ) {
                    case 
'png';
                        
imagepng$this->image['composite'], $destination $filename '.' $this->image['extension'], null );
                    break;
                    case 
'jpg';
                        
imagejpeg$this->image['composite'], $destination $filename '.' $this->image['extension'], $quality );
                    break;
                    case 
'jpeg';
                        
imagejpeg$this->image['composite'], $destination $filename '.' $this->image['extension'], $quality );
                    break;
                    case 
'gif';
                        
imagegif$this->image['composite'], $destination $filename '.' $this->image['extension'], $quality );
                    break;
                    case 
'bmp';
                        
imagewbmp$this->image['composite'], $destination $filename '.' $this->image['extension'], null );
                    break;
                    case 
'wbmp';
                        
imagewbmp$this->image['composite'], $destination $filename '.' $this->image['extension'], null );
                    break;
                }
            } else {
                echo 
'Execution halted!';
            }
        
        }
        
        public function 
clear_cache () {
        
            
imagedestroy$this->image['composite'] );
            
imagedestroy$this->image['render'] );
            unset( 
$this->image );
            unset( 
$this->s_image );
            
$this->halt false;
            
        }
        
    }

?>
Proportional Example (example.php)
PHP Code:
<?php

    
include './mrthumb.class.php';
        
    
// The image you are resizing. Can be a local path as well.
    
$image 'http://jordan.rave5.com/imgarch/clock.gif';
    
    
// In this example we are resizing the image in proportionate sizes.
    // Below we are specifying the MAX width and height.
    
$maxwidth 100// Pixels
    
$maxheight 130// Pixels

    // Start Mr. Thumb v1.0
    
$mrthumb = new MrThumb();
    
    
// Render the image
    
$mrthumb->render$image );
    
    
// Resize the image proportionately
    
$mrthumb->proportion$maxwidth$maxheight );

    
// Finally, output the image to the browser!
    
$mrthumb->output();
    
    
// Clean up after you are done!;)
    
$mrthumb->clear_cache();
    
?>
Constrained Example (example.php)
PHP Code:
<?php

    
include './mrthumb.class.php';
        
    
// The image you are resizing. Can be a local path as well.
    
$image 'http://jordan.rave5.com/imgarch/clock.gif';
    
    
// In this example we are resizing the image in proportionate sizes.
    // Below we are specifying the MAX width and height.
    
$width 100// Pixels
    
$height 100// Pixels

    // Start Mr. Thumb v1.0
    
$mrthumb = new MrThumb();
    
    
// Render the image
    
$mrthumb->render$image );
    
    
// Resize the image and constrain it's width and height
    
$mrthumb->constrain$width$height );

    
// Finally, output the image to the browser!
    
$mrthumb->output();
    
    
// Clean up after you are done!;)
    
$mrthumb->clear_cache();
    
?>
There is also a save function, if you want to just save the new thumbnail to a folder, where you would have another script load it.

PHP Code:
<?php

    
include './mrthumb.class.php';
        
    
// The image you are resizing. Can be a local path as well.
    
$image 'http://jordan.rave5.com/imgarch/clock.gif';
    
    
// Save to destination. Use a trailiing slash. 
    
$destination 'home/user/public_html/cache_images/';
    
    
// Name of the cached file.
    
$filename 'cache_clock';
    
    
// Quality of the image
    
$quality 100// percent
    
    // In this example we are resizing the image in proportionate sizes.
    // Below we are specifying the MAX width and height.
    
$maxwidth 100// Pixels
    
$maxheight 130// Pixels

    // Start Mr. Thumb v1.0
    
$mrthumb = new MrThumb();
    
    
// Render the image
    
$mrthumb->render$image );
    
    
// Resize the image proportionately
    
$mrthumb->proportion$width$height );

    
// Save the file to a specified directory, with a specified name.
    
$mrthumb->saveto$destination$filename$quality );
    
    
// Clean up after you are done!;)
    
$mrthumb->clear_cache();
    
?>

Last edited by Element; 09-20-2009 at 09:13 AM..
Element is offline   Reply With Quote
Users who have thanked Element for this post:
xdpirate (09-20-2009)
Old 09-20-2009, 06:13 AM   PM User | #2
xdpirate
New Coder

 
Join Date: Sep 2009
Location: Norway
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
xdpirate can only hope to improve
Very nice, I might use this on a website I'm planning!
xdpirate is offline   Reply With Quote
Old 09-20-2009, 09:15 AM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Thanks for the comment. I hope it does come in handy if you decide to use it.

Just a note, forgot to mention it. It supports PNG Alpha transparency, and GIF color transparency.

Last edited by Element; 09-20-2009 at 09:18 AM..
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:35 AM.


Advertisement
Log in to turn off these ads.