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[ 2 ]; 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;
/** * 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( $width, HEIGHT ); }
/** * 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->resizeImage( WIDTH, $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->img, 0, 0, 0, 0, $width, $height, $this->imgWidth(), $this->imgHeight() ); $this->img = $newImage; } } ?>
29-09-11
Justei
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
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