. */ /** * This file contains the Scale filter class. * * @author Dinu Florin * @package Modules * @subpackage Images */ /** * Scale and crop an image. * Scale the image and crop an image to force it to the exact dimensions given keeping the aspect ratio. * * @author Dinu Florin * @package Modules * @subpackage Images */ class ScaleAndCropImage extends AbstractImageFilter { protected $width; protected $height; /** * Constructor. * * @param mixed $width The new width of the image, use an integer for an absolute size or a string ("w%") for a relative size. * @param mixed $height The new height of the image, use an integer for an absolute size or a string ("h%") for a relative size. */ public function __construct($width, $height) { assert('is_string($width) or is_numeric($width)'); assert('is_string($height) or is_numeric($height)'); $this->width = $width; $this->height = $height; } /** * 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 desired dimensions if(is_string($this->width) && substr($this->width, -1, 1) == '%') { $w = (int)$this->width; if($w>0) $this->width = $w/100*$imgw; } if(is_string($this->height) && substr($this->height, -1, 1) == '%') { $h = (int)$this->height; if($h>0) $this->height = $h/100*$imgh; } //Scale $width = $this->width; $height = $this->height; $w = round($imgw * $this->height / $imgh); $h = round($imgh * $this->width / $imgw); if(($this->height-$h)>($this->width-$w)) { $width = $w; } else { $height = $h; } $scale = new ScaleImage($width+2, $height+2, false); $newImage = new Image(); $newImage->setResource($img); $newImage->applyFilter($scale); //Crop $imgw = $newImage->width; $imgh = $newImage->height; $x = round($imgw/2 - $this->width/2); $y = round($imgh/2 - $this->height/2); $crop = new CropImage($x, $y, $this->width, $this->height); $newImage->applyFilter($crop); $image->setResource($newImage->getResource()); } } ?>