Skip to content
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

New Trait for fetchPriority attribute in images #3850

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions system/blueprints/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,17 @@ form:
auto: Auto
sync: Sync
async: Async

images.defaults.fetchpriority:
type: select
size: small
label: PLUGIN_ADMIN.IMAGES_FETCHPRIORITY
help: PLUGIN_ADMIN.IMAGES_FETCHPRIORITY_HELP
highlight: auto
options:
auto: Auto
high: High
low: Low

images.seofriendly:
type: toggle
Expand Down
1 change: 1 addition & 0 deletions system/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ images:
defaults:
loading: auto # Let browser pick [auto|lazy|eager]
decoding: auto # Let browser pick [auto|sync|async]
fetchpriority: auto # Let browser pick [auto|high|low]
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/ImageFetchPriorityTrait.php
Original file line number Diff line number Diff line change
@@ -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 ImageFetchPriorityTrait
* @package Grav\Common\Media\Traits
*/

trait ImageFetchPriorityTrait
{
/**
* Allows to set the fetchpriority attribute from Markdown or Twig
*
* @param string|null $value
* @return $this
*/
public function fetchpriority($value = null)
{
if (null === $value) {
$value = Grav::instance()['config']->get('system.images.defaults.fetchpriority', 'auto');
}

// Validate the provided value (similar to loading and decoding attributes)
if ($value !== null && $value !== 'auto') {
$this->attributes['fetchpriority'] = $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 @@ -16,6 +16,7 @@
use Grav\Common\Media\Interfaces\MediaLinkInterface;
use Grav\Common\Media\Traits\ImageLoadingTrait;
use Grav\Common\Media\Traits\ImageDecodingTrait;
use Grav\Common\Media\Traits\ImageFetchPriorityTrait;
use Grav\Common\Media\Traits\ImageMediaTrait;
use Grav\Common\Utils;
use Gregwar\Image\Image;
Expand All @@ -32,6 +33,7 @@ class ImageMedium extends Medium implements ImageMediaInterface, ImageManipulate
use ImageMediaTrait;
use ImageLoadingTrait;
use ImageDecodingTrait;
use ImageFetchPriorityTrait;

/**
* @var mixed|string
Expand Down
Loading