Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Forked from giorrrgio/ImagineResizer.php
Created February 10, 2014 09:02
Show Gist options
  • Save dkesberg/8912612 to your computer and use it in GitHub Desktop.
Save dkesberg/8912612 to your computer and use it in GitHub Desktop.
"require": {
[...]
"imagine/Imagine": "dev-master"
},
<?php
namespace Acme\MyBundle\Service;
use Symfony\Component\HttpFoundation\File\File;
use Imagine\Image\ImagineInterface;
use Imagine\Image\BoxInterface;
use Imagine\Image\Point;
use Imagine\Image\Box;
class ImagineResizer
{
protected $imagine;
protected $mode;
protected $box;
public function __construct(ImagineInterface $imagine, BoxInterface $box, $mode = ImageInterface::THUMBNAIL_OUTBOUND) {
$this->imagine = $imagine;
$this->mode = $mode;
$this->box = $box;
}
public function resize(File $file, $destination)
{
$file->move($destination, $file->getClientOriginalName());
$filename = $file->getClientOriginalName();
$image = $this->imagine->open($destination . '/' . $filename);
//original size
$srcBox = $image->getSize();
//we scale on the smaller dimension
if ($srcBox->getWidth() > $srcBox->getHeight()) {
$width = $srcBox->getWidth()*($this->box->getHeight()/$srcBox->getHeight());
$height = $this->box->getHeight();
//we center the crop in relation to the width
$cropPoint = new Point((max($width - $this->box->getWidth(), 0))/2, 0);
} else {
$width = $this->box->getWidth();
$height = $srcBox->getHeight()*($this->box->getWidth()/$srcBox->getWidth());
//we center the crop in relation to the height
$cropPoint = new Point(0, (max($height-$this->box->getHeight(),0))/2);
}
$box = new Box($width, $height);
//we scale the image to make the smaller dimension fit our resize box
$image = $image->thumbnail($box, \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND);
//and crop exactly to the box
$image->crop($cropPoint, $this->box)
->save($destination . '/' . $filename);
return $file->getClientOriginalName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment