I wrote a quick class for image manipulation. It's quick, so it's not the best but it'll do everything you want. I also wrote pretty good documentation for it and wrote some examples. It'll help you with the resizing at least..
Class:
<?php
/*
@ImageClass v0.0.4
Started: 8/10/09
Last Edit: 8/24/09
By:>>Alexander Allen
@Vars>>
$image
$image>> Image Resource
$ext
$ext>> Image Resource Extension
$file
$file>> Filename of the original image
@Methods>>
__construct([$im]) << Creates a new Image class instance
$im>> (Optional) filename
setImage($im) << Sets, or replaces the current image instance
$im>> filename
resize($width, $height[, $porportional=FALSE[, $percent=NULL[, $max=NULL]]]) << Resizes the current image instance
$width>> New width for the image
$height>> New height for the image
$porportional>> (Optional) If set the image is scaled down portportionally
$percent>> (Optional) This is required if $porportional is set to true. It determines the size of the new image in % (0-100)
$max>> (Optional) This sets the max width or height allowed. If either is over the limit the image will be scaled down porportionally.
crop($width, $height, $x, $y) << Crops the current image instance
$width>> New width for the image
$height>> New height for the image
$x>> Determines the x start-point
$y>> Determines the y start-point
$position>> (Optional) When set it overrides the $x and $y positions given and crops to the set position. Valid values are:
>> CENTER, CENTER_LEFT, CENTER_RIGHT, TOP_CENTER, TOP_LEFT, TOP_RIGHT, BOTTOM_CENTER, BOTTOM_LEFT, BOTTOM_RIGHT
save($loc[, $compression=0]) << Saves the current image instance
$loc>> The path and file name to save the image to
$compression>> (Optional) Determines the compression level (Default: 0)
output() << Outputs the current image instance to the browser
No Parameters
Additional Notes>>
8/12/09>>
- Added optional position parameter to cropping.
- Added output()
- Changed $im parameters for __construct() and setimage(), it now takes a filename instead of a image resource
- Added var $file, contains the filename of the original Image Resource
- Added Example 2
8/23/09>>
- Modified resize() to support setting a max size (X|Y)
8/24/09>>
- Some small clean-ups / optimizations
- Better Error reporting
@Examples>>
Example 1>>
$image = new Image('test.png');
$image->resize(null, null, true, 50);
$image->save('images/newimage.png');
>> This example will first create an image from the resource 'test.png'.
It then resizes it porportionally to a 50% scale.
Finally the image is saved to images/newimage.png.
Example 2>>
<?php
include('image.class.php');
$image = new Image('test.gif');
$image->crop(50, 50, null, null, CENTER_LEFT);
$image->output();
?>
>> This example first creates an image resource from test.gif
It then crops it to a 50, 50 square using the CENTER_LEFT positioning
Finally the image is output and displayed in the browser.
*/
class Image
{
var $image, $ext, $file;
public function __construct($im=NULL)
{
define('CENTER', 0, true);
define('CENTER_LEFT', 1, true);
define('CENTER_RIGHT', 2, true);
define('TOP_CENTER', 3, true);
define('TOP_LEFT', 4, true);
define('TOP_RIGHT', 5, true);
define('BOTTOM_CENTER', 6, true);
define('BOTTOM_LEFT', 7, true);
define('BOTTOM_RIGHT', 8, true);
if(empty($im)) return true;
$info = @getimagesize($im);
$backtrace = debug_backtrace();
if(!$info)
return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
$ex = explode('/', $info['mime']);
$this->ext = $ex[1];
$func = 'imagecreatefrom' . $this->ext;
$this->image = @$func($im);
$this->file = $im;
return true;
}
public function setImage($im)
{
$info = @getimagesize($this->file);
$backtrace = debug_backtrace();
if(!$info)
return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
$ex = explode('/', $info['mime']);
$this->ext = $ex[1];
$func = 'imagecreatefrom' . $this->ext;
$this->image = @$func($im);
$this->file = $im;
return true;
}
public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
$backtrace = debug_backtrace();
if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
$info = Array(imagesx($this->image), imagesy($this->image));
if($porportional)
{
if(empty($max) && empty($percent)) return false;
if(empty($max))
{
$new_width = $info[0] * ($percent/100);
$new_height = $info[1] * ($percent/100);
}
else
{
if($info[0] < $max && $info[1] < $max) return false;
$new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max;
$new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max;
}
}
else
{
$new_width = $width;
$new_height = $height;
}
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
$this->image = $new_image;
return true;
}
public function crop($width, $height, $x, $y, $position=NULL)
{
$backtrace = debug_backtrace();
if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
$info = Array(imagesx($this->image), imagesy($this->image));
if($width > $info[0] || $height > $info[1]) return false;
if(!empty($position) && $position >= 0 && $position <=
{
switch($position)
{
case CENTER:
$x = ($info[0] - $width)/2;
$y = ($info[1] - $height)/2;
break;
case CENTER_LEFT:
$x = 0;
$y = ($info[1] - $height)/2;
break;
case CENTER_RIGHT:
$x = ($info[0] - $height);
$y = ($info[1] - $height)/2;
break;
case TOP_CENTER:
$x = ($info[0] - $width)/2;
$y = 0;
break;
case TOP_LEFT:
$x = 0;
$y = 0;
break;
case TOP_RIGHT:
$x = ($info[0] - $width);
$y = 0;
break;
case BOTTOM_CENTER:
$x = ($info[0] - $width)/2;
$y = ($info[1] - $height);
break;
case BOTTOM_LEFT:
$x = 0;
$y = ($info[1] - $height);
break;
case BOTTOM_RIGHT:
$x = ($info[0] - $width);
$y = ($info[1] - $height);
break;
default:
return false;
break;
}
}
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
$this->image = $new_image;
return true;
}
public function save($loc, $compression=0)
{
$backtrace = debug_backtrace();
if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
$func = 'image' . $this->ext;
return $func($this->image, $loc, $compression);
}
public function output()
{
$backtrace = debug_backtrace();
if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
header('Content-type: image/' . $this->ext);
$func = 'image' . $this->ext;
$func($this->image);
}
}
For your specific case where you want to resize images to 800X600 you can do something like this:
//include class
$im = new Image('image..');
$im->resize(800, 600);