-
Notifications
You must be signed in to change notification settings - Fork 16
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
Support SVGs #38
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a00b469
Get src image from transaction again before giving it to Thumbnail as…
CarsonF 4bad1e3
Support SVGs
CarsonF 8c49d4a
Update to use Image\TypeInterface. Require bolt/filesystem 2.1 once i…
CarsonF 603f711
Setting `"bolt/filesystem": "^2.1"` now that it's been tagged.
bobdenotter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
|
@@ -13,6 +16,8 @@ | |
*/ | ||
class Creator implements CreatorInterface | ||
{ | ||
/** @var SvgImagine */ | ||
protected $svgImagine; | ||
/** @var bool */ | ||
protected $limitUpscaling; | ||
/** @var Color */ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're ignoring BC? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
@@ -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) { | ||
|
@@ -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'); | ||
} | ||
} |
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
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
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
Y
s are majors whenX
is a zero, nearly nobody actually deos that, hence one of the reasons Composer switched their default constraint marker to strict.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?There was a problem hiding this comment.
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 😉