. */ /** * This file contains the Crop filter class. * * @author Dinu Florin * @package Modules * @subpackage Images */ /** * Crop filter. * Crop an image starting at an absolute or relative point and having an absolute size crop area. * * @author Dinu Florin * @package Modules * @subpackage Images */ class CropImage extends AbstractImageFilter { protected $width; protected $height; protected $x; protected $y; /** * Constructor. * * @param mixed $x Position of crop region, use an integer for absolute positioning or a string ("x%") for relative positioning. * @param mixed $y Position of crop region, use an integer for absolute positioning or a string ("y%") for relative positioning * @param number $width The width of the cropping region. * @param number $height The height of the cropping region. */ public function __construct($x, $y, $width, $height) { assert('is_numeric($x) or is_string($x)'); assert('is_numeric($y) or is_string($y)'); assert('is_numeric($width)'); assert('is_numeric($height)'); $this->width = $width; $this->height = $height; $this->x = $x; $this->y = $y; } /** * Apply to an image * * @param Image $image The image to apply the filter to */ public function applyTo(Image $image) { $img = $image->getResource(); $imgw = imagesx($img); $imgh = imagesy($img); //Calculate the position if(is_string($this->x) && substr($this->x, -1, 1) == '%') { $w = (int)$this->x; if($w>0) $this->x = floor($w/100*$imgw); } if(is_string($this->y) && substr($this->y, -1, 1) == '%') { $h = (int)$this->y; if($h>0) $this->y = floor($h/100*$imgh); } if($this->x > $imgw || $this->y > $imgh) {trigger_error('ImageCrop: x or y out of bounds', E_USER_WARNING);} //Crop the image $newImg = imagecreatetruecolor($this->width, $this->height); imagecopy($newImg, $img, 0, 0, $this->x, $this->y, $this->width, $this->height); $image->setResource($newImg); } } ?>