Skip to content

Instantly share code, notes, and snippets.

@crisu83
Created July 10, 2013 21:25
Show Gist options
  • Save crisu83/5970474 to your computer and use it in GitHub Desktop.
Save crisu83/5970474 to your computer and use it in GitHub Desktop.
<?php
/**
* 'filters' => array(
* 'myFilter' => array(
* array('thumbnail', 'width' => 120, 'height' => 90, 'mode' => 'inset'),
* ),
* ),
*/
use Imagine\Filter\FilterInterface;
use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Imagine\Image\PointInterface;
/**
* Base class for the all Imagine filters.
*/
abstract class ImagineFilter extends CComponent
{
// Map of the supported imagine filters.
public static $builtInFilters = array(
'applyMask' => 'ApplyMaskFilter',
'crop' => 'CropFilter',
'flipHorizontally' => 'FlipHorizontallyFilter',
'flipVertically' => 'FlipVerticallyFilter',
'resize' => 'ResizeFilter',
'rotate' => 'RotateFilter',
'thumbnail' => 'ThumbnailFilter',
);
/**
* @var FilterInterface
*/
protected $_filter;
/**
* Loads the actual filter.
* @return FilterInterface
*/
abstract public function load();
/**
* Applies the filter to the given image interface.
* @param ImageInterface $image the image interface.
* @return ImageInterface
*/
public function apply(ImageInterface $image)
{
return $this->_filter->apply($image);
}
/**
* Creates a filter of a specific type.
* @param string $name the filter type.
* @param array $params filter options.
* @return ImagineFilter the filter.
*/
public static function createFilter($name, $params = array())
{
if (isset(self::$builtInFilters[$name]))
$className = Yii::import(self::$builtInFilters[$name], true);
else
$className = Yii::import($name, true);
/* @var ImagineFilter $filter */
$filter = new $className;
foreach ($params as $key => $value)
$filter->$key = $value;
$filter->_filter = $filter->load();
return $filter;
}
/**
* Creates a Imagine box object.
* @param integer $width the width in pixels.
* @param integer $height the height in pixels.
* @return BoxInterface the object.
*/
protected function _createBox($width, $height)
{
return new Box($width, $height);
}
/**
* Creates a Imagine point object.
* @param integer $x the position on the x-axis.
* @param integer $y the position on the y-axis.
* @return PointInterface the object.
*/
protected function _createPoint($x, $y)
{
return new Point($x, $y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment