Manipulaition
<?php
$path = $_GET[`image`];
$w = $_GET[`w`];
$h = $_GET[`h`];
$style = $_GET[`style`];
if($style == "crop" && isset($_GET[`cropw`]) && isset($_GET[`croph`]))
{
if($_GET[`cropw`] == 0 || $_GET[`croph`] == 0)
{
$style = "fitcrop";
}
}
$imageDims = getimagesize($path); // Get image dimensions
$oWidth = $imageDims[0];
$oHeight = $imageDims[1];
$mime = split("/",$imageDims[`mime`]);
$type = $mime[1];
switch($type)
{
case `jpeg`:$image = imagecreatefromjpeg($path); // Create image
break;
case `gif`: $image = imagecreatefromgif($path); // Create image
imagesavealpha($image, true); // Turn on transparency
imagealphablending($image, false); // Turn off alphablending to retain alpha
$transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
// Set transparent color
imagefill($image, 0, 0, $transparentColor); // Make background image transparent
break;
case `png`:$image = imagecreatefrompng($path); // Create image
imagesavealpha($image, true); // Turn on transparency
imagealphablending($image, false); // Turn off alphablending to retain alpha
$transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
// Set transparent color
imagefill($image, 0, 0, $transparentColor); // Make background image transparent
break;
}
if($style == "fit")
{
$nW = $oWidth;
$nH = $oHeight;
if($nW > $nH)
{
for($i=0;$nH>$h;$i++)
{
$nH = $nH - ($oHeight/$oWidth);
}
for($j=0;$j<$i;$j++)
{
$nW = $nW - 1;
}
}
elseif($nH > $nW)
{
for($i=0;$nH>$h;$i++)
{
$nH = $nH - 1;
}
for($j=0;$j<$i;$j++)
{
$nW = $nW - ($oWidth/$oHeight);
}
}
$resizedImage = imagecreatetruecolor($nW,$nH);
imagecopyresampled($resizedImage,$image,0,0,0,0,$nW,$nH,$oWidth,$oHeight);
}
elseif($style == "crop")
{
$cropX = $_GET[`cropx`];
$cropY = $_GET[`cropy`];
$cropW = $_GET[`cropw`];
$cropH = $_GET[`croph`];
$cropImage = imagecreatetruecolor($cropW,$cropH);
imagecopyresampled($cropImage,$image,0,0,$cropX,$cropY,$cropW,$cropH,$cropW,$cropH);
$resizedImage = imagecreatetruecolor($w,$h);
imagecopyresampled($resizedImage,$cropImage,0,0,0,0,$w,$h,$cropW,$cropH);
}
elseif($style == "fitcrop")
{
$nW = $oWidth;
$nH = $oHeight;
if($nW > $nH)
{
for($i=0;$nH>$h;$i++)
{
$nH = $nH - ($oHeight/$oWidth);
}
for($j=0;$j<$i;$j++)
{
$nW = $nW - 1;
}
}
elseif($nH > $nW)
{
for($i=0;$nW>$w;$i++)
{
$nW = $nW - ($oWidth/$oHeight);
}
for($j=0;$j<$i;$j++)
{
$nH = $nH - 1;
}
}
$dX = ($w/2) - ($nW/2);
$dY = ($h/2) - ($nH/2);
$resizedImage = imagecreatetruecolor($w,$h);
imagecopyresampled($resizedImage,$image,$dX,$dY,0,0,$nW,$nH,$oWidth,$oHeight);
}
switch($type)
{
case `jpeg`: header("Content-type: image/jpeg");
imagejpeg($resizedImage);
break;
case `gif`: header("Content-type: image/gif");
imagegif($resizedImage);
break;
case `png`: header("Content-type: image/png");
imagepng($resizedImage);
break;
}
imagedestroy($resizedImage);
?>
Click here to download this file
SimpleImage
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=75, $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);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}