Skip to content

Commit

Permalink
Add Vector image auto_sizes support
Browse files Browse the repository at this point in the history
  • Loading branch information
xf- authored and mahagr committed Feb 12, 2022
1 parent 2c252c4 commit c4e10cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
3 changes: 2 additions & 1 deletion system/src/Grav/Common/Page/Medium/MediumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ public static function fromArray(array $items = [], Blueprint $blueprint = null)
return new ImageMedium($items, $blueprint);
case 'thumbnail':
return new ThumbnailImageMedium($items, $blueprint);
case 'animated':
case 'vector':
return new VectorImageMedium($items, $blueprint);
case 'animated':
return new StaticImageMedium($items, $blueprint);
case 'video':
return new VideoMedium($items, $blueprint);
Expand Down
63 changes: 63 additions & 0 deletions system/src/Grav/Common/Page/Medium/VectorImageMedium.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* @package Grav\Common\Page
*
* @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Page\Medium;

use Grav\Common\Data\Blueprint;


/**
* Class StaticImageMedium
* @package Grav\Common\Page\Medium
*/
class VectorImageMedium extends StaticImageMedium
{
/**
* Construct.
*
* @param array $items
* @param Blueprint|null $blueprint
*/
public function __construct($items = [], Blueprint $blueprint = null)
{
parent::__construct($items, $blueprint);

$height = false;
$width = false;

if (!extension_loaded('simplexml')) {
return;
}

$path = $this->get('filepath');
if (!$path || !file_exists($path) || !filesize($path)) {
return;
}

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

if (!$attr instanceof \SimpleXMLElement) {
return;
}

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];
}

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

0 comments on commit c4e10cf

Please sign in to comment.