User tests: Successful: Unsuccessful:
Status | New | ⇒ | Pending |
Category | ⇒ | Front End com_content com_finder com_tags com_users Libraries Modules |
@laoneo some more meaningful feedback here
Insert this in the index.php of cassiopeia (somewhere in the body, just so that it would render):
echo HTMLHelper::_('image', 'images/powered_by.png#joomlaImage://local-images/powered_by.png?width=294&height=44', 'Some Alt Text', ['class' => 'background-image']);
The image renders correctly, but the passed URL is cleaned from the #
part which is problematic as it would never get the width/height and also disregards the loading attribute.
Possible solution:
public static function image($file, $alt, $attribs = null, $relative = false, $returnPath = 0)
{
$returnPath = (int) $returnPath;
if ($returnPath !== -1) {
$newFile = static::cleanImageURL($file);
$includes = static::includeRelativeFiles('images', $newFile->url, $relative, false, false);
$newFile->url = \count($includes) ? $includes[0] : null;
$file = $newFile->url . '#' . $newFile->adapter . '?width=' . $newFile->attributes['width'] . '&height=' . $newFile->attributes['height'];
}
// If only path is required
if ($returnPath === 1) {
$newFile = (static::cleanImageURL($file));
$includes = static::includeRelativeFiles('images', $newFile->url, $relative, false, false);
$newFile->url = \count($includes) ? $includes[0] : null;
return $newFile->url;
}
// Ensure we have a valid default for concatenating
if ($attribs === null || $attribs === false) {
$attribs = [];
}
// When it is a string, we need convert it to an array
if (is_string($attribs)) {
$attributes = [];
foreach (explode(' ', $attribs) as $attribute) {
if (strpos($attribute, '=') === false) {
$attributes[$attribute] = '';
continue;
}
list($key, $value) = explode('=', $attribute);
$attributes[$key] = trim($value, '"');
}
$attribs = $attributes;
}
$attribs['src'] = $file;
$attribs['alt'] = $alt;
return LayoutHelper::render('joomla.html.image', $attribs);
}
But it needs couple of lines changed here:
public static function cleanImageURL($url)
{
$obj = new \stdClass;
$obj->attributes = [
'width' => 0,
'height' => 0,
'adapter' => '',
];
if (!strpos($url, '?'))
{
$obj->url = $url;
return $obj;
}
$mediaUri = new Uri($url);
// Old image URL format
if ($mediaUri->hasVar('joomla_image_height'))
{
$height = (int) $mediaUri->getVar('joomla_image_height');
$width = (int) $mediaUri->getVar('joomla_image_width');
$mediaUri->delVar('joomla_image_height');
$mediaUri->delVar('joomla_image_width');
}
else
{
// New Image URL format
$fragmentUri = new Uri($mediaUri->getFragment());
$width = (int) $fragmentUri->getVar('width', 0);
$height = (int) $fragmentUri->getVar('height', 0);
$obj->adapter = $fragmentUri->getVar('scheme', '') . '://' . $fragmentUri->getVar('host', '');
}
if ($width > 0)
{
$obj->attributes['width'] = $width;
}
if ($height > 0)
{
$obj->attributes['height'] = $height;
}
$mediaUri->setFragment('');
$obj->url = $mediaUri->toString();
return $obj;
}
I guess we do that in another pr. This here is just a followup from yours. There are a lot of places we can optimize the image rendering.
Labels |
Added:
?
|
But you are undoing all the lazy loading and the rest of the attributes in the images here, so in that sense the pr is wrong
I have tested this item
The output is missing the width, height and loading attribute, so this is a performance regression (this PR will increase the CLS [Layout shift])
And this works without the pr? Because all this pr does is to use the htmlhelper and then forward to the layout.
Can you check again. It should work now.
For the other testers, please also test with normal urls and with invalid ones.
There are additional ones to convert.
Where did you add that code?
I have tested this item
Where did you add that code?
Very beginning of the body.
I have tested this item
Works as expected. Rendered code looks much nicer.
Using todays 4.1 pull
<div class="site-grid">
<?php echo HTMLHelper::_('image', 'images/powered_by.png#joomlaImage://local-images/powered_by.png?width=294&height=44', 'Some Alt Text', ['class' => 'background-image']); ?>
</div>
Status | Pending | ⇒ | Ready to Commit |
RTC
Status | Ready to Commit | ⇒ | Fixed in Code Base |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2022-05-17 18:52:38 |
Closed_By | ⇒ | bembelimen | |
Labels |
Added:
?
|
Thx
@laoneo com_tags/tags/default.php: logical error in echo HTMLHelper::_ code, backend settings are incorrectly implemented (also the original code with LayoutHelper is wrong).
Backend settings:
As you can see Image Description (Alt Text) and "No Description" settings are related.
There are 4 cases possible:
The above logic is not followed currently here:
https://github.com/Digital-Peak/joomla-cms/blob/141b2e9b3dc251346de70a85b27b245bf15d9bbc/components/com_tags/tmpl/tags/default.php#L26
INMHO this would be the appropriate code:
Note that 2nd empty negated, and the 2 "empties" are in logical "OR" relation. That would mirror the above logic for the mentioned 4 possible cases.
Cheers.
Thanks for the report. But I do not think this is related to this pr. Can you make a new pr with your suggestion so we can analyze the issue as this pr is closed. Thanks for understanding.
No problem, I've expected a similar reply. I've reported it here because I've seen you are heavily involved in this Layout->HTMLHelper conversion thingie, and thought maybe it is a good opportunity to warn you if you just take the old code with LayoutHelper and you simply replace it with the HTMLHelper there are still some inherent issues not resolved... so we have to touch the same file/code again... and I hate to run in circles :)
I'll open a new issue about these problems.
I have a small issue with the code change. For components which use dynamic links to render images or use the anchor.
URL with potential problems: http://foo.bar/image#2
or http://foo.bar/image?width=200
HTMLHelper.php:726
// Get the arguments positions
$pos1 = strpos($file, '?');
$pos2 = strpos($file, '#');
// Check if there are arguments
if ($pos1 !== false || $pos2 !== false)
{
// Get the path only
$path = substr($file, 0, min($pos1, $pos2));
// Determine the arguments is mostly the part behind the #
$arguments = str_replace($path, '', $file);
}`
This code expects a ? and a # to work correctly. If one of them is not in the $file-string, the $path-substring will be empty. This creates a issue with the URLs above. min(a,b)
returns false if a or b are false.
May I ask why?
The only reason that someone should use the
HTMLHelper
is for the relative path (which obviously none of the user uploaded images will never use).Also, at least from my point of view, all the HTML fragments should use JLayouts as the overriding is way more friendly for front end devs, HTMLHelper actually needs a plugin.
What I'm trying to say is that it was a conscious decision NOT to use the HTMLHelper for reasons I stated above, but if the project thinks that the HTMLHelper is the right way, well no comments...
EDIT: ok I'm an idiot, I should have checked all the files..
THIS IS FINE?