? ? Pending

User tests: Successful: Unsuccessful:

avatar laoneo
laoneo
25 Feb 2022

Pull Request for pr #37022.

Summary of Changes

This is a followup of #37022 which uses HTMLHelper instead of LayoutHelper.

Testing Instructions

See #37022.

Actual result BEFORE applying this Pull Request

Image is rendered.

Expected result AFTER applying this Pull Request

Image is rendered.

avatar laoneo laoneo - open - 25 Feb 2022
avatar laoneo laoneo - change - 25 Feb 2022
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 25 Feb 2022
Category Front End com_content com_finder com_tags com_users Libraries Modules
2ed743a 25 Feb 2022 avatar laoneo cs
avatar dgrammatiko
dgrammatiko - comment - 25 Feb 2022

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 ?

avatar dgrammatiko
dgrammatiko - comment - 26 Feb 2022

@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;
	}
avatar laoneo
laoneo - comment - 27 Feb 2022

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.

avatar laoneo laoneo - change - 27 Feb 2022
Labels Added: ?
avatar dgrammatiko
dgrammatiko - comment - 27 Feb 2022

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

avatar dgrammatiko
dgrammatiko - comment - 27 Feb 2022

I have tested this item ? unsuccessfully on 8cdc8bd


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37148.

The output is missing the width, height and loading attribute, so this is a performance regression (this PR will increase the CLS [Layout shift])

Before
Screenshot 2022-02-27 at 15 05 15

With this PR
Screenshot 2022-02-27 at 15 02 53

avatar dgrammatiko dgrammatiko - test_item - 27 Feb 2022 - Tested unsuccessfully
avatar laoneo
laoneo - comment - 27 Feb 2022

And this works without the pr? Because all this pr does is to use the htmlhelper and then forward to the layout.

avatar dgrammatiko
dgrammatiko - comment - 27 Feb 2022

@laoneo check my previous comment, the htmlhelper is stripping the url but then the layout doesn’t have the needed data…

avatar laoneo
laoneo - comment - 2 Mar 2022

Can you check again. It should work now.
For the other testers, please also test with normal urls and with invalid ones.

avatar Quy
Quy - comment - 14 Apr 2022

There are additional ones to convert.

avatar laoneo
laoneo - comment - 15 Apr 2022

This one here changes the ones from pr #37022. We can do the other ones as followup.

avatar N6REJ
N6REJ - comment - 26 Apr 2022

Using the testing instructions buried in the comments

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 is full width
image
after applying the patch the image is it's normal size.
image

avatar laoneo
laoneo - comment - 27 Apr 2022

Where did you add that code?

avatar Quy
Quy - comment - 28 Apr 2022

I have tested this item successfully on 141b2e9


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37148.

avatar Quy Quy - test_item - 28 Apr 2022 - Tested successfully
avatar N6REJ
N6REJ - comment - 30 Apr 2022

Where did you add that code?

Very beginning of the body.

avatar laoneo
laoneo - comment - 8 May 2022

@N6REJ can you test it with the 4.1 branch or with the stable package?

avatar N6REJ
N6REJ - comment - 10 May 2022

I have tested this item successfully on 141b2e9

Works as expected. Rendered code looks much nicer.

Using todays 4.1 pull


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37148.

Pre-patch:
image

Post Patch:
image
code used...

<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>
avatar N6REJ N6REJ - test_item - 10 May 2022 - Tested successfully
avatar Quy Quy - change - 11 May 2022
Status Pending Ready to Commit
avatar Quy
Quy - comment - 11 May 2022

RTC


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37148.

avatar bembelimen bembelimen - change - 17 May 2022
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: ?
avatar bembelimen bembelimen - close - 17 May 2022
avatar bembelimen bembelimen - merge - 17 May 2022
avatar bembelimen
bembelimen - comment - 17 May 2022

Thx

avatar DelPoint
DelPoint - comment - 25 May 2022

@laoneo HTMLHelper_ code in files of com_tags (default.php, list.php etc.) doesn't take into account the back-end settings of "Alt Text", "Caption" & "Float" for the tag's Teaser/Full Image. See below:
image

avatar DelPoint
DelPoint - comment - 25 May 2022

@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:

image

As you can see Image Description (Alt Text) and "No Description" settings are related.

There are 4 cases possible:

  1. Alt Text missing + "No Desc." = false (unset/unselected)
    => no output of "alt" property
  2. Alt Text missing + "No Desc." = true (set/selected/thicked)
    => no output of "alt" property
  3. Alt Text exist + "No Desc." = false
    => output of "alt" property
  4. Alt Text exist + "No Desc." = true
    => no output of "alt" property, as in this case the "No Desc." should overwrite the existing "Alt Text"

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:
image

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.

avatar laoneo
laoneo - comment - 26 May 2022

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.

avatar DelPoint
DelPoint - comment - 27 May 2022

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.

avatar svenbluege
svenbluege - comment - 29 May 2022

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.

Add a Comment

Login with GitHub to post a comment