[PHP] ImageResizer by m0nsta.

Results 1 to 4 of 4
  1. #1
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    [PHP] ImageResizer by m0nsta.

    I've always been interested in making one of these, but never had time to be honest.

    I seen a thread the other day like this and wanted to code my own.

    It's usage is very simple and slightly different to the other one, I have also well commented this too so you know what it does and when.

    To use the ImageResizer, click here. If you want to download/view the source you can click here.

    Or if you don't want to do that, you can view the source here:

    PHP Code:
    <?php
    /**
      * Simple PHP GD image resizer coded by Mark Eriksson
      * Mark Eriksson (C) 2011
      * http://www.mark-eriksson.com
      * http://www.mark-eriksson.com/projects/ImageResizer/
      */

    class ImageResizer
    {
        
    /**
          * These variables below are what will be used throughout the script to be able to make the actual script work.
          * @var - $img      - image  - The image identifier which will be represented in the main constructor function.
          * @var - $img_type - string - The type of image that will be retrieved from the image. JPG, GIF, PNG.
          */
        
    var $img;
        var 
    $img_type;
        
        
    /**
          * This function will load the image into the resizer and will be stored in $this->img.
          * @param - $location - string - The image that we will be resizing.
          */
        
    public function __construct$location )
        {
            
    $file_information getimagesize$location );
            
    $this->img_type   $file_information];
            switch ( 
    $this->img_type )
            {
                case 
    IMAGETYPE_JPEG:
                    
    $this->img imagecreatefromjpeg$location );
                break;
                
                case 
    IMAGETYPE_GIF:
                    
    $this->img imagecreatefromgif$location );
                break;
                
                case 
    IMAGETYPE_PNG:
                    
    $this->img imagecreatefrompng$location );
                break;
            }
        }
        
        
    /**
          * This function will basically save the image in the cache, compress it if neccessary and apply permission specified in the parameters.
          * @param - $location - string  - The original path of the file we will be resizing.
          * @param - $img_type - string  - The type/format of image to choose from: jpeg, gif or png.
          * @param - $quality  - integer - The amount of quality to apply on the image (min: 0, max: 100, default: 75 ).
          * @param - $perms    - integer - The permissions to apply to the file.
          */
        
    public function saveImage$location$img_type IMAGETYPE_JPEG$quality 75$perms null )
        {
            switch ( 
    $img_type )
            {
                case 
    IMAGETYPE_JPEG:
                    
    imagejpeg$this->img$location$quality );
                break;
                
                case 
    IMAGETYPE_GIF:
                    
    imagegif$this->img$location );
                break;
                
                case 
    IMAGETYPE_PNG:
                    
    imagepng$this->img$location );
                break;
            }
            if ( 
    $perms !== null )
            {
                
    chmod$location$perms );
            }
        }
        
        
    /**
          * This function will just output the image normally. It is similar to saveImage() but does a little less work.
          * @param - $img_type - string - The type/format of image to choose from: jpeg, gif or png.
          */
        
    public function outputImage$img_type IMAGETYPE_JPEG )
        {
            switch ( 
    $img_type )
            {
                case 
    IMAGETYPE_JPEG:
                    
    imagejpeg$this->img );
                break;
                
                case 
    IMAGETYPE_GIF:
                    
    imagegif$this->img$location );
                break;
                
                case 
    IMAGETYPE_PNG:
                    
    imagepng$this->img$location );
                break;
            }
        }
        
        
    /**
          * This function is very simple - all it does is returns the width of the original image.
          */
        
    public function imgWidth()
        {
            return 
    imagesx$this->img );
        }
        
        
    /**
          * This function is very simple - all it does is returns the height of the original image.
          */
        
    public function imgHeight()
        {
            return 
    imagesy$this->img );
        }
        
        
    /**
          * This function scales the image to a specified width but keeps the dimension ratio the same and works out the height automatically.
          * @param - $width - integer - The width to scale to.
          */
        
    public function toWidth$width )
        {
            
    define"RATIO"$width $this->imgWidth() );
            
    define"HEIGHT"$this->imgHeight() * RATIO );
            
    $this->resizeImage$widthHEIGHT );
        }
        
        
    /**
          * This function scales the image to a specified height but keeps the dimension ratio the same and works out the width automatically.
          * @param - $height - integer - The height to scale to.
          */
        
    public function toHeight$height )
        {
            
    define"RATIO"$height $this->imgHeight() );
            
    define"WIDTH"$this->imgWidth() * RATIO );
            
    $this->resizeImageWIDTH$height );
        }
        
        
    /**
          * This function will scale the image to a specific value.
          * @param - $scale - integer - The amount to scale by.
          */
        
    public function scaleImage$scale )
        {
            
    $width  $this->imgWidth() * $scale 100;
            
    $height $this->imgHeight() * $scale 100;
            
    $this->resizeImage$width$height );
        }
        
        
    /**
          * This function is the main part really - it does all the resizing after all the other neccessary functions have been seen to.
          * @param - $width  - integer - The width to resize the image to.
          * @param - $height - integer - The height to resize the image to.
          */
        
    public function resizeImage$width$height )
        {
            
    $newImage imagecreatetruecolor$width$height );
            
    imagecopyresampled$newImage$this->img0000$width$height$this->imgWidth(), $this->imgHeight() );
            
    $this->img $newImage;
        }
    }
    ?>
    Last edited by Markshall; 29-09-11 at 12:20 AM.


  2. #2
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP] ImageResizer by m0nsta.

    Looks nice :)

    I got a resizer I use for a cpl of projects that looks like this, it's modified to be a Model for CodeIgniter but it's very easily changed to a normal class...

    I think yours might be easier to use but I like the one I use as well so ^^ but I think I would go with yours if I wanted a very fast and simple way :P

    Spoiler:

    PHP Code:
    <?php



    class resizemodel extends CI_Model {
       
       var 
    $image;
       var 
    $image_type;
     
       function 
    load($filename) {
          
    $image_info getimagesize($filename);
          
    $this->image_type $image_info[2];
          if( 
    $this->image_type == IMAGETYPE_JPEG ) {
             
    $this->image imagecreatefromjpeg($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_GIF ) {
             
    $this->image imagecreatefromgif($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_PNG ) {
             
    $this->image imagecreatefrompng($filename);
          }
       }
       function 
    save($filename$image_type=IMAGETYPE_JPEG$compression=75$permissions=null) {
          if( 
    $image_type == IMAGETYPE_JPEG ) {
             
    imagejpeg($this->image,$filename,$compression);
          } elseif( 
    $image_type == IMAGETYPE_GIF ) {
             
    imagegif($this->image,$filename);         
          } elseif( 
    $image_type == IMAGETYPE_PNG ) {
             
    imagepng($this->image,$filename);
          }   
          if( 
    $permissions != null) {
             
    chmod($filename,$permissions);
          }
       }
       function 
    output($image_type=IMAGETYPE_JPEG) {
          if( 
    $image_type == IMAGETYPE_JPEG ) {
             
    imagejpeg($this->image);
          } elseif( 
    $image_type == IMAGETYPE_GIF ) {
             
    imagegif($this->image);         
          } elseif( 
    $image_type == IMAGETYPE_PNG ) {
             
    imagepng($this->image);
          }   
       }
       function 
    getWidth() {
          return 
    imagesx($this->image);
       }
       function 
    getHeight() {
          return 
    imagesy($this->image);
       }
       function 
    resizeToHeight($height) {
          
    $ratio $height $this->getHeight();
          
    $width $this->getWidth() * $ratio;
          
    $this->resize($width,$height);
       }
       function 
    resizeToWidth($width) {
          
    $ratio $width $this->getWidth();
          
    $height $this->getheight() * $ratio;
          
    $this->resize($width,$height);
       }
       function 
    scale($scale) {
          
    $width $this->getWidth() * $scale/100;
          
    $height $this->getheight() * $scale/100
          
    $this->resize($width,$height);
       }
       function 
    resize($width,$height) {
          
    $new_image imagecreatetruecolor($width$height);
          
    imagecopyresampled($new_image$this->image0000$width$height$this->getWidth(), $this->getHeight());
          
    $this->image $new_image;   
       }      
    }


    And some easy usage (keep in mind that it's for CI so the first line is well, for CI lol, replace that with whatever or however u wanna do it):
    Spoiler:

    PHP Code:
       $this->load->model('resizemodel');
       
    $image = new resizemodel();
       
    $image->load('imgname.jpg');
       
    $image->resizeToWidth(640);
       
    $image->save('imgname.jpg'); 
    Last edited by Justei; 29-09-11 at 12:09 AM.

  3. #3
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] ImageResizer by m0nsta.

    Thanks for the feedback Justei :). And yeah I based mine off the one you posted by completely recoded it in my own way :P
    I decided to do
    PHP Code:
    <?php
    $IR 
    = new ImageResizer"image.png" );
    ?>
    Rather than:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer();
    $IR->load"image.png" );
    ?>
    Oh yeah, I forgot to say how to use it!

    To re size it by a certain height and width:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer"image.jpg" );
                                
    $IR->resizeImage400800 );
                                
    $IR->saveImage"image_resized.jpg" );
    ?>
    To re size it by a certain height:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->toHeight400 );
    $IR->saveImage$saveImageString );
    ?>
    To re size by a certain width:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->toWidth400 );
    $IR->saveImage$saveImageString );
    ?>
    To re size to a certain scale:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->scaleImage400 );
    $IR->saveImage$saveImageString );
    ?>

  4. #4
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP] ImageResizer by m0nsta.

    Quote Originally Posted by m0nsta. View Post
    Thanks for the feedback Justei :). And yeah I based mine off the one you posted by completely recoded it in my own way :P
    I decided to do
    PHP Code:
    <?php
    $IR 
    = new ImageResizer"image.png" );
    ?>
    Rather than:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer();
    $IR->load"image.png" );
    ?>
    Oh yeah, I forgot to say how to use it!

    To re size it by a certain height and width:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer"image.jpg" );
                                
    $IR->resizeImage400800 );
                                
    $IR->saveImage"image_resized.jpg" );
    ?>
    To re size it by a certain height:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->toHeight400 );
    $IR->saveImage$saveImageString );
    ?>
    To re size by a certain width:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->toWidth400 );
    $IR->saveImage$saveImageString );
    ?>
    To re size to a certain scale:
    PHP Code:
    <?php
    $IR 
    = new ImageResizer$tmp_name );
    $IR->scaleImage400 );
    $IR->saveImage$saveImageString );
    ?>
    Hah I kinda figured since there were some things in common, just something about the code seemed familiar but a bit different, in any case yours is a little improved one I guess :P



Advertisement