23/01/2012 4:30am

PHP | Working Example - Two operations on an image before outputting.


<?php


class ImageCropFromTop extends DataObjectDecorator {
function CropFromTop($width, $height) {
return $this->owner->getFormattedImage('CropFromTop', $width, $height);
}
function generateCropFromTop($gd, $width, $height) {
return $gd->cropFromTop($width, $height);
//$gd = $gd->resizeRatio($width, $height, true);
//return $gd->crop(0,0,$width,$height);
}
function CroppedImage($width, $height) {
return $this->CropFromTop($width, $height);
}
function CropFromCentre($width, $height) {
return $this->owner->getFormattedImage('CropFromCentre', $width, $height);
}
function generateCropFromCentre($gd, $width, $height) {
return $gd->croppedResize($width, $height);
}
}

class GDCropFromTop extends Extension {
function cropFromTop($width, $height) {
if(!$this->owner->hasGD()) return;
$width = round($width);
$height = round($height);
$newGD = imagecreatetruecolor($width, $height);
imagealphablending($newGD, false);
imagesavealpha($newGD, true);
$destAR = $width / $height;
if($this->owner->getWidth() > 0 && $this->owner->getHeight() > 0) {
$srcAR = $this->owner->getWidth() / $this->owner->getHeight();
if($destAR < $srcAR) {
$srcY = 0;
$srcHeight = $this->owner->getHeight();
$srcWidth = $this->owner->getHeight() * $destAR;
$srcX = ($this->owner->getWidth() - $srcWidth) / 2;
}
else {
$srcX = 0;
$srcWidth = $this->owner->getWidth();
$srcHeight = $this->owner->getWidth() / $destAR;
$srcY = 0;//($this->owner->getHeight() - $srcHeight) / 2;
}
imagecopyresampled($newGD, $this->owner->getGD(), 0, 0, $srcX, $srcY, $width, $height, $srcWidth, $srcHeight);
}
$output = clone $this->owner;
$output->setGD($newGD);
return $output;
}
}


Post Comment