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,
Or if you don't want to do that, you can view the source here:
How to use:
To re size it by a certain height and width:
To re size it by a certain height:
To re size by a certain width:
To re size to a certain scale:
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,
You must be registered for see links
. If you want to download/view the source you can
You must be registered for see links
.Or if you don't want to do that, you can view the source here:
PHP:
<?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;
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( $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;
}
}
?>
How to use:
To re size it by a certain height and width:
PHP:
<?php
$IR = new ImageResizer( "image.jpg" );
$IR->resizeImage( 400, 800 );
$IR->saveImage( "image_resized.jpg" );
?>
To re size it by a certain height:
PHP:
<?php
$IR = new ImageResizer( "image.jpg" );
$IR->toHeight( 400 );
$IR->saveImage( $saveImageString );
?>
To re size by a certain width:
PHP:
<?php
$IR = new ImageResizer( "image.jpg" );
$IR->toWidth( 400 );
$IR->saveImage( $saveImageString );
?>
To re size to a certain scale:
PHP:
<?php
$IR = new ImageResizer( "image.jpg" );
$IR->scaleImage( 400 );
$IR->saveImage( $saveImageString );
?>