<?php
Require (' resize_img.php ');
How to use the class:
Makes a simple thumbnail of the 100x100 and saves the image then outputs it.
$imgresize = new Resize_img ();
$imgresize->sizelimit_x = 100;
$imgresize->sizelimit_y = 100;
$imgresize->keep_proportions = true;
$imgresize->output = ' PNG ';
if ($imgresize->resize_image (' c:/xampp/htdocs/wannabe/image_bin/public/treeshadows_001.gif ') = = False)
{
Echo ' error! ';
}
Else
{
$imgresize->save_resizedimage (' c:/xampp/htdocs/wannabe/image_bin/public/thumbnails/', ' treeshadows_001 ');
$imgresize->output_resizedimage ();
}
$imgresize->destroy_resizedimage ();
?>
resize_img.php file
<?php
/**
* by Daniel M. Story <admin@danstory.com>
*/
Class Resize_img
{
var $image _path = ';
Holds the image path
var $sizelimit _x = 250;
The limit of the image width
var $sizelimit _y = 250;
The limit of the image height
var $image _resource = ';
Holds the image resource
var $keep _proportions = true;
If true it keeps tutorial the image proportions when resized
var $resized _resource = ';
Holds the resized image resource
var $hasGD = false;
var $output = ' SAME ';
Can be JPG, GIF, PNG, or SAME (SAME'll save as old type)
function resize_img ()
{
if (function_exists (' Gd_info ')) {$this->HASGD = true;}
}
function Resize_image ($image _path)
{
if ($this->HASGD = = False) {return false;}
No GD installed on the server!
List ($img _width, $img _height, $img _type, $img _attr) = @getimagesize ($image _path);
This is going to get the image width, height, and format
if ($img _width!= 0) | | ($img _width!= 0))
Make sure it is loaded correctly
{
Switch ($img _type)
{
Case 1:
Gif
$this->image_resource = @imagecreatefromgif ($image _path);
if ($this->output = = ' SAME ') {$this->output = ' GIF ';}
Break
Case 2:
Jpg
$this->image_resource = @imagecreatefromjpeg ($image _path);
if ($this->output = = ' SAME ') {$this->output = ' JPG ';}
Break
Case 3:
Png
$this->image_resource = @imagecreatefrompng ($image _path);
if ($this->output = = ' SAME ') {$this->output = ' PNG ';}
}
if ($this->image_resource = = ") {return false;}
It wasn ' t able to load the image
}
else{return false;}
Something happened!
if ($this->keep_proportions = = True)
{
if ($img _width-$this->sizelimit_x) > ($img _height-$this->sizelimit_y))
{
If the width of the IMG is greater than the size limit we scale by width
$scalex = ($this->sizelimit_x/$img _width);
$scaley = $scalex;
}
Else
If the height of the IMG is greater than the size limit we scale by height
{
$scalex = ($this->sizelimit_y/$img _height);
$scaley = $scalex;
}
}
Else
{
$scalex = ($this->sizelimit_x/$img _width);
$scaley = ($this->sizelimit_y/$img _height);
Just make the image fit the image size limit
if ($scalex > 1) {$scalex = 1;}
if ($scaley > 1) {$scaley = 1;}
Don ' t make it streches the image
}
$new _width = $img _width * $SCALEX;
$new _height = $img _height * $scaley;
$this->resized_resource = @imagecreatetruecolor ($new _width, $new _height);
Creates an image resource, with the width and height of the size limits (or new resized proportion)
if (function_exists (' Imageantialias ')) {@imageantialias ($this->resized_resource, True);}
Helps in the quality of the image being resized
@imagecopyresampled ($this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new _width, $new _height, $img _ width, $img _height);
Resize the iamge onto the resized resource
@imagedestroy ($this->image_resource);
Destory Old Image Resource
return true;
}
function Save_resizedimage ($path, $name)
{
Switch (Strtoupper ($this->output))
{
Case ' GIF ':
Gif
@imagegif ($this->resized_resource, $path. $name. '. gif ');
Break
Case ' JPG ':
Jpg
@imagejpeg ($this->resized_resource, $path. $name. '. jpg ');
Break
Case ' PNG ':
Png
@imagepng ($this->resized_resource, $path. $name. '. png ');
}
}
function Output_resizedimage ()
{
$the _time = time ();
Header (' last-modified: '. Date (' d, D M Y h:i:s ', $the _time). ' GMT ');
Header (' Cache-control:public ');
Switch (Strtoupper ($this->output))
{
Case ' GIF ':
Gif
Header (' content-type:image/gif ');
@imagegif ($this->resized_resource);
Break
Case ' JPG ':
Jpg
Header (' content-type:image/jpg ');
@imagejpeg ($this->resized_resource);
Break
Case ' PNG ':
Png
Header (' content-type:image/png ');
@imagepng ($this->resized_resource);
}
}
function Destroy_resizedimage ()
{
@imagedestroy ($this->resized_resource);
@imagedestroy ($this->image_resource);
}
}
?>