Skip to content

Commit

Permalink
Added auto_sizes support for SVG vector images [#3533]
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Feb 12, 2022
1 parent c4e10cf commit 3a45748
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v1.7.31
## mm/dd/2022

1. [](#new)
* Added auto_sizes support for SVG vector images [#3533](https://github.com/getgrav/grav/pull/3533)

# v1.7.30
## 02/07/2022

Expand Down
18 changes: 7 additions & 11 deletions system/src/Grav/Common/Page/Medium/VectorImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public function __construct($items = [], Blueprint $blueprint = null)
{
parent::__construct($items, $blueprint);

$height = false;
$width = false;

if (!extension_loaded('simplexml')) {
if ($this->mime !== 'image/svg+xml' || !\extension_loaded('simplexml')) {
return;
}

Expand All @@ -41,7 +38,7 @@ public function __construct($items = [], Blueprint $blueprint = null)
}

$xml = simplexml_load_string(file_get_contents($path));
$attr = $xml->attributes();
$attr = $xml ? $xml->attributes() : null;

if (!$attr instanceof \SimpleXMLElement) {
return;
Expand All @@ -50,14 +47,13 @@ public function __construct($items = [], Blueprint $blueprint = null)
if ($attr->width > 0 && $attr->height > 0) {
$width = (int)$attr->width;
$height = (int)$attr->height;
} elseif ($attr->viewBox && count($size = explode(' ', $attr->viewBox)) === 4) {
$width = (int)$size[2];
$height = (int)$size[3];
} elseif ($attr->viewBox && \count($size = explode(' ', $attr->viewBox)) === 4) {
[,$width,$height,] = $size;
}

if ($width && $height) {
$this->def('width', $width);
$this->def('height', $height);
if (isset($width, $height)) {
$this->def('width', (int)$width);
$this->def('height', (int)$height);
}
}
}

0 comments on commit 3a45748

Please sign in to comment.