Skip to content

Commit

Permalink
New Trait for decoding attribute in images
Browse files Browse the repository at this point in the history
  • Loading branch information
pmoreno.rodriguez committed Jan 29, 2024
1 parent cd2a7d8 commit 959acda
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
42 changes: 42 additions & 0 deletions system/src/Grav/Common/Media/Traits/ImageDecodingTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

namespace Grav\Common\Media\Traits;

use Grav\Common\Grav;

/**
* Trait ImageLoadingTrait
* @package Grav\Common\Media\Traits
*/

trait ImageDecodingTrait
{
/**
* Allows to set the decoding attribute from Markdown or Twig
*
* @param string|null $value
* @return $this
*/
public function decoding($value = null)
{
$validValues = ['sync', 'async', 'auto'];

if (null === $value) {
$value = Grav::instance()['config']->get('system.images.defaults.decoding', 'auto');
}

// Validate the provided value
if ($value && in_array($value, $validValues, true)) {
$this->attributes['decoding'] = $value;
}

return $this;
}
}
2 changes: 2 additions & 0 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Grav\Common\Media\Interfaces\ImageMediaInterface;
use Grav\Common\Media\Interfaces\MediaLinkInterface;
use Grav\Common\Media\Traits\ImageLoadingTrait;
use Grav\Common\Media\Traits\ImageDecodingTrait;
use Grav\Common\Media\Traits\ImageMediaTrait;
use Grav\Common\Utils;
use Gregwar\Image\Image;
Expand All @@ -30,6 +31,7 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
{
use ImageMediaTrait;
use ImageLoadingTrait;
use ImageDecodingTrait;

/**
* @var mixed|string
Expand Down

0 comments on commit 959acda

Please sign in to comment.