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

[4.0] Disable lazyloading on images that have no dimensions or added on the first page view #30218

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion layouts/joomla/content/full_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<?php if (!empty($images->image_fulltext)) : ?>
<?php $imgfloat = empty($images->float_fulltext) ? $params->get('float_fulltext') : $images->float_fulltext; ?>
<figure class="float-<?php echo htmlspecialchars($imgfloat, ENT_COMPAT, 'UTF-8'); ?> item-image">
<img loading="lazy" src="<?php echo htmlspecialchars($images->image_fulltext, ENT_COMPAT, 'UTF-8'); ?>"
<img src="<?php echo htmlspecialchars($images->image_fulltext, ENT_COMPAT, 'UTF-8'); ?>"
alt="<?php echo htmlspecialchars($images->image_fulltext_alt, ENT_COMPAT, 'UTF-8'); ?>"
itemprop="image"/>
<?php if (isset($images->image_fulltext_caption) && $images->image_fulltext_caption !== '') : ?>
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/content/intro_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<figure class="float-<?php echo htmlspecialchars($imgfloat, ENT_COMPAT, 'UTF-8'); ?> item-image">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo Route::_(RouteHelper::getArticleRoute($displayData->slug, $displayData->catid, $displayData->language)); ?>">
<img loading="lazy" src="<?php echo htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8'); ?>"
<img src="<?php echo htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8'); ?>"
alt="<?php echo htmlspecialchars($images->image_intro_alt, ENT_COMPAT, 'UTF-8'); ?>"
itemprop="thumbnailUrl"
/>
</a>
<?php else : ?>
<img loading="lazy" src="<?php echo htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8'); ?>"
<img src="<?php echo htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8'); ?>"
alt="<?php echo htmlspecialchars($images->image_intro_alt, ENT_COMPAT, 'UTF-8'); ?>"
itemprop="thumbnailUrl"
>
Expand Down
4 changes: 2 additions & 2 deletions libraries/src/HTML/HTMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ public static function image($file, $alt, $attribs = null, $relative = false, $r
return $file;
}

// Default to lazy you can disable lazyloading by passing $attribs['loading'] = 'eager';
if (!isset($attribs['loading']))
// Default to lazy when the width and height are set you can disable lazyloading by passing $attribs['loading'] = 'eager';
if (!isset($attribs['loading']) && isset($attribs['width']) && isset($attribs['height']))
{
$attribs['loading'] = 'lazy';
}
Expand Down
8 changes: 6 additions & 2 deletions plugins/content/imagelazyload/imagelazyload.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ public function onContentPrepare($context, &$row, &$params, $page = 0)

foreach ($matches[0] as $image)
{
// Make sure we have a src but no loading attribute
if (strpos($image, ' src=') !== false && strpos($image, ' loading=') === false)
// Make sure we have a src, width and height but no loading attribute
if (strpos($image, ' src=') !== false
&& strpos($image, ' width=') !== false
&& strpos($image, ' height=') !== false
&& strpos($image, ' loading=') === false
)
Quy marked this conversation as resolved.
Show resolved Hide resolved
{
$lazyloadImage = str_replace('<img ', '<img loading="lazy" ', $image);
$row->text = str_replace($image, $lazyloadImage, $row->text);
Expand Down