Created
November 26, 2013 15:40
-
-
Save mathewpeterson/7660627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Imagine; | |
use Avalanche\Bundle\ImagineBundle\Imagine\Filter\Loader\LoaderInterface; | |
class ThumbnailFilterService implements LoaderInterface, Imagine\Filter\FilterInterface | |
{ | |
const DEFAULT_WIDTH = 120; | |
const DEFAULT_HEIGHT = 120; | |
const DEFAULT_BACKGROUND_COLOR = '222'; // HEX x222 | |
const DEFAULT_ALPHA = 0; | |
protected $canvas_width; | |
protected $canvas_height; | |
protected $canvas_background_color; | |
protected $canvas_alpha; | |
public function load(array $options = array()) | |
{ | |
if(isset($options['size']) && count($options['size']) == 2) | |
{ | |
list($this->canvas_width, $this->canvas_height) = $options['size']; | |
} | |
else | |
{ | |
$this->canvas_width = self::DEFAULT_WIDTH; | |
$this->canvas_height = self::DEFAULT_HEIGHT; | |
} | |
$this->canvas_background_color = isset($options['background_color']) ? $options['background_color'] : self::DEFAULT_BACKGROUND_COLOR; | |
$this->canvas_alpha = isset($options['alpha']) ? $options['alpha'] : self::DEFAULT_ALPHA; | |
return $this; | |
} | |
public function apply(Imagine\Image\ImageInterface $image) | |
{ | |
$canvas_box = new Imagine\Image\Box($this->canvas_width, $this->canvas_height); | |
$image = $image->thumbnail($canvas_box, Imagine\Image\ImageInterface::THUMBNAIL_INSET); | |
$image_height = $image->getSize()->getHeight(); | |
$image_width = $image->getSize()->getWidth(); | |
$imagine = new Imagine\Gd\Imagine(); | |
$canvas = $imagine->create( | |
$canvas_box, | |
new Imagine\Image\Color($this->canvas_background_color, $this->canvas_alpha) | |
); | |
$center_x = abs($this->canvas_width - $image_width) / 2; | |
$center_y = abs($this->canvas_height - $image_height) / 2; | |
$start_point = new Imagine\Image\Point($center_x, $center_y); | |
/* | |
var_dump(array( | |
'canvas_alpha' => $this->canvas_alpha, | |
'canvas_width' => $canvas_box->getWidth(), | |
'canvas_height' => $canvas_box->getHeight(), | |
'image_width' => $image_width, | |
'image_height' => $image_height, | |
'center_x' => $center_x, | |
'center_y' => $center_y | |
)); die(); | |
*/ | |
return $canvas->paste($image, $start_point); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment