Skip to content

Commit

Permalink
Merge #5 branch feature/event-listener into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Aug 4, 2016
2 parents bb80a5f + eb196f3 commit 5410142
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "~1.8",
"phpunit/phpunit": "~4.5",
"satooshi/php-coveralls": "~0.6"
"satooshi/php-coveralls": "~0.6",
"symfony/event-dispatcher": "~2.8|~3.0"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 28 additions & 0 deletions src/Event/ContaoImageEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\Image\Event;

/**
* Defines constants for the Contao image events.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
final class ContaoImageEvents
{
/**
* The contao.image.resize_image event is triggered when an image is resized.
*
* @var string
*
* @see Contao\Image\Event\ResizeImageEvent
*/
const RESIZE_IMAGE = 'contao.image.resize_image';
}
128 changes: 128 additions & 0 deletions src/Event/ResizeImageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\Image\Event;

use Contao\Image\ImageInterface;
use Contao\Image\ResizeCoordinatesInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* Allows to set a resized image.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ResizeImageEvent extends Event
{
/**
* @var ImageInterface
*/
private $image;

/**
* @var ResizeCoordinatesInterface
*/
private $coordinates;

/**
* @var string
*/
private $path;

/**
* @var array
*/
private $imagineOptions;

/**
* @var ImageInterface
*/
private $resizedImage;

/**
* Constructor.
*
* @param ImageInterface $image
* @param ResizeCoordinatesInterface $coordinates
* @param string $path
* @param array $imagineOptions
*/
public function __construct(
ImageInterface $image,
ResizeCoordinatesInterface $coordinates,
$path,
array $imagineOptions
) {
$this->image = $image;
$this->coordinates = $coordinates;
$this->path = $path;
$this->imagineOptions = $imagineOptions;
}

/**
* Returns the image object.
*
* @return ImageInterface
*/
public function getImage()
{
return $this->image;
}

/**
* Returns the coordinates object.
*
* @return ResizeCoordinatesInterface
*/
public function getCoordinates()
{
return $this->coordinates;
}

/**
* Returns the path.
*
* @return string
*/
public function getPath()
{
return $this->path;
}

/**
* Returns the Imagine options.
*
* @return array
*/
public function getImagineOptions()
{
return $this->imagineOptions;
}

/**
* Returns the resized image.
*
* @return ImageInterface
*/
public function getResizedImage()
{
return $this->resizedImage;
}

/**
* Sets the resized image.
*
* @param ImageInterface|null $image
*/
public function setResizedImage(ImageInterface $image = null)
{
$this->resizedImage = $image;
}
}
4 changes: 2 additions & 2 deletions src/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(array $img, array $sources)
*/
public function getImg($rootDir = null)
{
if ($rootDir === null) {
if (null === $rootDir) {
return $this->img;
}

Expand All @@ -60,7 +60,7 @@ public function getImg($rootDir = null)
*/
public function getSources($rootDir = null)
{
if ($rootDir === null) {
if (null === $rootDir) {
return $this->sources;
}

Expand Down
49 changes: 47 additions & 2 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Contao\Image;

use Contao\Image\Event\ContaoImageEvents;
use Contao\Image\Event\ResizeImageEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand All @@ -34,14 +37,24 @@ class Resizer implements ResizerInterface
*/
private $path;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* {@inheritdoc}
*/
public function __construct(ResizeCalculatorInterface $calculator, Filesystem $filesystem, $path)
{
public function __construct(
ResizeCalculatorInterface $calculator,
Filesystem $filesystem,
$path,
EventDispatcherInterface $eventDispatcher = null
) {
$this->calculator = $calculator;
$this->filesystem = $filesystem;
$this->path = (string) $path;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -112,6 +125,12 @@ private function executeResize(
$path,
array $imagineOptions
) {
$resizedImage = $this->getResizedImageFromEvent($image, $coordinates, $path, $imagineOptions);

if (null !== $resizedImage) {
return $resizedImage;
}

if (!$this->filesystem->exists(dirname($path))) {
$this->filesystem->mkdir(dirname($path));
}
Expand Down Expand Up @@ -155,4 +174,30 @@ private function createCachePath($path, ResizeCoordinatesInterface $coordinates)

return substr($hash, 0, 1).'/'.$pathinfo['filename'].'-'.substr($hash, 1).'.'.$pathinfo['extension'];
}

/**
* Returns a resized image from an event.
*
* @param ImageInterface $image
* @param ResizeCoordinatesInterface $coordinates
* @param string $path
* @param array $imagineOptions
*
* @return ImageInterface|null
*/
private function getResizedImageFromEvent(
ImageInterface $image,
ResizeCoordinatesInterface $coordinates,
$path,
array $imagineOptions
) {
if (null === $this->eventDispatcher) {
return null;
}

$event = new ResizeImageEvent($image, $coordinates, $path, $imagineOptions);
$this->eventDispatcher->dispatch(ContaoImageEvents::RESIZE_IMAGE, $event);

return $event->getResizedImage();
}
}
15 changes: 11 additions & 4 deletions src/ResizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Contao\Image;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand All @@ -22,11 +23,17 @@ interface ResizerInterface
/**
* Constructor.
*
* @param ResizeCalculatorInterface $calculator
* @param Filesystem $filesystem
* @param string $path
* @param ResizeCalculatorInterface $calculator
* @param Filesystem $filesystem
* @param string $path
* @param EventDispatcherInterface|null $eventDispatcher
*/
public function __construct(ResizeCalculatorInterface $calculator, Filesystem $filesystem, $path);
public function __construct(
ResizeCalculatorInterface $calculator,
Filesystem $filesystem,
$path,
EventDispatcherInterface $eventDispatcher = null
);

/**
* Resizes an Image object.
Expand Down
Loading

0 comments on commit 5410142

Please sign in to comment.