<?php
class SimpleImage {
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=100, $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);
$bg = imagecolorallocate($new_image,255,255,255);
imagefill($new_image,0,0,$bg);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
function crop($filename,$width,$height)
{
list($w, $h, $type, $attr) = getimagesize($filename);
if( $this->image_type == IMAGETYPE_JPEG ) {
$src_im = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$src_im = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$src_im = imagecreatefrompng($filename);
}
$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = $width; // width
$src_h = $height; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y
$new_image = imagecreatetruecolor($src_w, $src_h);
$white = imagecolorallocate($new_image, 255, 255, 255);
imagefill($new_image, 0, 0, $white);
imagecopy($new_image, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
$this->image = $new_image;
}
}
$filename= "test.png";
$thumb = new SimpleImage();
$thumb->load($filename);
$thumb->resizeToWidth(120);
$thumb->crop($filename, 120, 120);
$thumb->save("test".$filename);
?>