Skip to content

Commit

Permalink
New Trait for decoding attribute in images (#3796)
Browse files Browse the repository at this point in the history
* New Trait for decoding attribute in images

* Update comments info

* decoding default in system/config/system.yaml and system/blueprints/config/system.yaml for the images.defaults.decoding value

* Fixed predefined option in the decoding attribute
  • Loading branch information
pmoreno.rodriguez committed Feb 3, 2024
1 parent cd2a7d8 commit ad8b1b7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions system/blueprints/config/system.yaml
Expand Up @@ -1300,6 +1300,17 @@ form:
auto: Auto
lazy: Lazy
eager: Eager

images.defaults.decoding:
type: select
size: small
label: PLUGIN_ADMIN.IMAGES_DECODING
help: PLUGIN_ADMIN.IMAGES_DECODING_HELP
highlight: auto
options:
auto: Auto
sync: Sync
async: Async

images.seofriendly:
type: toggle
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Expand Up @@ -168,6 +168,7 @@ images:
retina_scale: 1 # scale to adjust auto-sizes for better handling of HiDPI resolutions
defaults:
loading: auto # Let browser pick [auto|lazy|eager]
decoding: auto # Let browser pick [auto|sync|async]
watermark:
image: 'system://images/watermark.png' # Path to a watermark image
position_y: 'center' # top|center|bottom
Expand Down
40 changes: 40 additions & 0 deletions system/src/Grav/Common/Media/Traits/ImageDecodingTrait.php
@@ -0,0 +1,40 @@
<?php

/**
* @package Grav\Common\Media
* @author Pedro Moreno https://github.com/pmoreno-rodriguez
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Media\Traits;

use Grav\Common\Grav;

/**
* Trait ImageDecodingTrait
* @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)
{
if (null === $value) {
$value = Grav::instance()['config']->get('system.images.defaults.decoding', 'auto');
}

// Validate the provided value (similar to loading)
if ($value !== null && $value !== 'auto') {
$this->attributes['decoding'] = $value;
}

return $this;
}

}
2 changes: 2 additions & 0 deletions system/src/Grav/Common/Page/Medium/ImageMedium.php
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 ad8b1b7

Please sign in to comment.