Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SVGs #38

Merged
merged 4 commits into from
Feb 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],

"require": {
"bolt/filesystem": "^2.0",
"bolt/filesystem": "^2.1",
"contao/imagine-svg": "^0.1.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked at the code, and I find it hard to believe they aren't going to keep 0.x a stable series, i.e using ^ is anything but a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That supports >= 0.1.2 and < 0.2.0. Or are you saying they'll break BC on a 0.1.x release?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just that while semver says that Ys are majors when X is a zero, nearly nobody actually deos that, hence one of the reasons Composer switched their default constraint marker to strict.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nearly nobody actually deos that

You mean people don't break BC with 0.Y releases or they do?

^ has always had that logic. Are you saying we should use ~ here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that in my observation, very few people are aware that 0.Y counts as major in SemVer and behave as if it were a minor. I'm pretty sure I've seen core developers to it to.

For a require, ~ might make sense in most places, but project dependant, ad if I don't know for sure, I usually go for the loose option.

Third, we stay as-is, come back in a year and see which wat it went 😉

"doctrine/cache": "^1.4",
"ext-gd": "*",
"php": "^5.5.9 || ^7.0"
Expand Down
50 changes: 47 additions & 3 deletions src/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
namespace Bolt\Thumbs;

use Bolt\Filesystem\Exception\IOException;
use Contao\ImagineSvg\Imagine as SvgImagine;
use Exception;
use Imagine\Image\Box;
use Imagine\Exception\RuntimeException as ImagineRuntimeException;
use RuntimeException;

/**
Expand All @@ -13,6 +16,8 @@
*/
class Creator implements CreatorInterface
{
/** @var SvgImagine */
protected $svgImagine;
/** @var bool */
protected $limitUpscaling;
/** @var Color */
Expand All @@ -21,11 +26,13 @@ class Creator implements CreatorInterface
/**
* Creator constructor.
*
* @param bool $limitUpscaling
* @param Color $background
* @param bool $limitUpscaling
* @param SvgImagine $svgImagine
* @param Color $background
*/
public function __construct($limitUpscaling = true, Color $background = null)
public function __construct($limitUpscaling = true, SvgImagine $svgImagine = null, Color $background = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're ignoring BC?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I didn't want to go past background since it wasn't provided in service provider.

{
$this->svgImagine = $svgImagine ?: new SvgImagine();
$this->limitUpscaling = (bool) $limitUpscaling;
$this->background = $background ?: Color::white();
}
Expand Down Expand Up @@ -131,6 +138,25 @@ protected function resize(Transaction $transaction)
$fit = $transaction->getAction() === Action::FIT;
$border = $transaction->getAction() === Action::BORDER;

if ($transaction->getSrcImage()->getMimeType() === 'image/svg+xml') {
try {
return $this->resizeSvg($transaction);
} catch (ImagineRuntimeException $e) {
// If the image is the fallback image throw exception
if ($transaction->getSrcImage()->getFullPath() === $transaction->getErrorImage()->getFullPath()) {
throw new RuntimeException(
'There was an error with the thumbnail image requested and additionally the fallback image could not be displayed.',
0,
$e
);
}

// Fallback to error image
$transaction->setErrorImage($transaction->getSrcImage());
return $this->create($transaction);
}
}

try {
$img = ImageResource::createFromString($transaction->getSrcImage()->read());
} catch (Exception $e) {
Expand Down Expand Up @@ -186,4 +212,22 @@ protected function resize(Transaction $transaction)

return $img->toString();
}

/**
* Resize SVG image.
*
* @param Transaction $transaction
*
* @return string
*/
protected function resizeSvg(Transaction $transaction)
{
$image = $this->svgImagine->load($transaction->getSrcImage()->read());

$target = $transaction->getTarget();

$image->resize(new Box($target->getWidth(), $target->getHeight()));

return $image->get('svg');
}
}
20 changes: 10 additions & 10 deletions src/ImageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ImageResource
{
/** @var resource */
protected $resource;
/** @var Image\Type */
/** @var Image\TypeInterface */
protected $type;
/** @var Image\Info */
protected $info;
Expand All @@ -29,15 +29,15 @@ class ImageResource
protected static $normalizeJpegOrientation = true;

/**
* ImageResource constructor.
* Constructor.
*
* Either type or info need to be provided.
*
* @param resource $resource A GD resource
* @param Image\Type $type Image type
* @param Image\Info $info Image info
* @param resource $resource A GD resource
* @param Image\TypeInterface $type Image type
* @param Image\Info $info Image info
*/
public function __construct($resource, Image\Type $type = null, Image\Info $info = null)
public function __construct($resource, Image\TypeInterface $type = null, Image\Info $info = null)
{
if (!is_resource($resource) || !get_resource_type($resource) === 'gd') {
throw new InvalidArgumentException('Given resource must be a GD resource');
Expand Down Expand Up @@ -106,12 +106,12 @@ public static function createFromString($data)
/**
* Creates a new image given the width and height.
*
* @param Dimensions $dimensions Image dimensions
* @param Image\Type $type Type of image
* @param Dimensions $dimensions Image dimensions
* @param Image\TypeInterface $type Type of image
*
* @return ImageResource
*/
public static function createNew(Dimensions $dimensions, Image\Type $type)
public static function createNew(Dimensions $dimensions, Image\TypeInterface $type)
{
$resource = imagecreatetruecolor($dimensions->getWidth(), $dimensions->getHeight());
if ($resource === false) {
Expand Down Expand Up @@ -148,7 +148,7 @@ public function getDimensions()
/**
* Returns the image type.
*
* @return Image\Type
* @return Image\TypeInterface
*/
public function getType()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function respond(Transaction $transaction)
$this->saveStaticThumbnail($transaction->getRequestPath(), $thumbnail);

// Return thumbnail
return new Thumbnail($image, $thumbnail);
return new Thumbnail($transaction->getSrcImage(), $thumbnail);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Bolt\Thumbs;

use Contao\ImagineSvg\Imagine as SvgImagine;
use Silex\Application;
use Silex\ServiceProviderInterface;

Expand Down Expand Up @@ -33,7 +34,11 @@ public function register(Application $app)
});

$app['thumbnails.creator'] = $app->share(function ($app) {
return new Creator($app['thumbnails.limit_upscaling']);
return new Creator($app['thumbnails.limit_upscaling'], $app['imagine.svg']);
});

$app['imagine.svg'] = $app->share(function () {
return new SvgImagine();
});

$app['thumbnails.finder'] = $app->share(function ($app) {
Expand Down
12 changes: 12 additions & 0 deletions tests/CreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class CreatorTest extends \PHPUnit_Framework_TestCase
protected $landscapeImage;
/** @var Image 427x640 */
protected $portraitImage;
/** @var Image */
protected $svgImage;

public function setup()
{
$this->fs = new Filesystem(new Local(__DIR__ . '/images'));
$this->logoJpg = $this->fs->getImage('generic-logo.jpg');
$this->landscapeImage = $this->fs->getImage('samples/sample1.jpg');
$this->portraitImage = $this->fs->getImage('samples/sample2.jpg');
$this->svgImage = $this->fs->getImage('samples/nut.svg');
}

/**
Expand Down Expand Up @@ -171,6 +174,15 @@ public function testPortraitBorder()
$this->assertDimensions($expected, $result);
}

public function testSvg()
{
$transaction = new Transaction($this->portraitImage, Action::RESIZE, new Dimensions(200, 500));

$result = (new Creator())->create($transaction);

$this->assertDimensions(new Dimensions(200, 299), $result);
}

/**
* @param Dimensions $expected
* @param Dimensions|string $actual
Expand Down
6 changes: 6 additions & 0 deletions tests/images/samples/nut.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.