. */ /** * This file contains the Scale filter class. * * @author Dinu Florin * @package Modules * @subpackage Images */ /** * Scale filter. * Scale an image to absolute or relative dimensions keeping the aspect ratio or not. It will scale the image to the maximum size that will * fit in the dimensions given. This will either shrink or grow the image to achieve it's goal. * * @author Dinu Florin * @package Modules * @subpackage Images */ class ScaleImage extends AbstractImageFilter { protected $width; protected $height; protected $absolute; protected $keepAR; /** * 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. * @param boolean $keepAR Keep the image aspect ratio when scaling (default=true) */ public function __construct($width, $height, $keepAR=true) { assert('is_string($width) or is_numeric($width)'); assert('is_string($height) or is_numeric($height)'); assert('is_bool($keepAR)'); $this->width = $width; $this->height = $height; $this->keepAR = $keepAR; } /** * 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; } //Calculate the actual dimensions if($this->keepAR) { $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; } } else { $width = $this->width; $height = $this->height; } //Scale the image $newImg = imagecreatetruecolor($width, $height); imagecopyresampled($newImg, $img, 0, 0, 0, 0, $width, $height, $imgw, $imgh); $image->setResource($newImg); } } ?>