Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2026-01-05 07:48:05

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,606
Website

Quality attribute depending on image type on, variable set?

While optimising some image shortcode with automatic thumbnail generation, I am trying to conditionally insert the quality attribute based on the type of image: .webp or .jpg The former can confortably do with a stonger compression or lower quality setting than the latter. As much as possible I try to fully automate this to avoid having the user need to add something

My attempt use a two variables as seen below:

<txp:variable name="img-type"><txp:image_info type="ext" /></txp:variable>
<txp:variable name="img-quality" default="75"><txp:if_variable name="img-type" value=".webp">55</txp:if_variable></txp:variable>

<img src="…" srcset="<txp:image_url thumbnail width="400" quality='<txp:variable name="img-quality" />' 400w,… >">

That works fine, but I wonder if there is a more optimal way? And in the code above I don’t take .avif images under consideration. Yet. I ought to add it for the future.

My initial attempt used:

<txp:variable name="img-quality" default="75"><txp:if_variable name="img-type" value=".webp">55</txp:if_variable></txp:variable>

<img src="…" srcset="<txp:image_url thumbnail width="400" <txp:variable name="img-quality" /> 400w

But that resulted in a complete chaos, due to nesting tags and quotes, with the txp:image_url thumbnail unparsed…

Suggestions welcome.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

#2 2026-01-05 09:28:27

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,144
Website GitHub

Re: Quality attribute depending on image type on, variable set?

phiw13 wrote #342090:

My initial attempt used… but that resulted in a complete chaos, due to nesting tags and quotes, with the txp:image_url thumbnail unparsed…

Aren’t you just missing the quality attribute itself? It looks like nesting is only needed once …

<txp:variable name="img-type"><txp:image_info type="ext" /></txp:variable>
<txp:variable name="img-quality" default="75" trim>
    <txp:if_variable name="img-type" value=".webp, .avif" match="any">55</txp:if_variable>
</txp:variable>
<img src="…" srcset="<txp:image_url thumbnail width="400" quality='<txp:variable name="img-quality" />' /> 400w, …">

If you need different compression for avif and webp, make two separate if_variable lines. You might also be able to use txp:evaluate in place of the img-type variable.

Other ideas: I can’t remember exactly if you can do trim="." or ltrim="." in the first variable. If so, you could strip the dot off .ext for easier handling.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2026-01-05 11:05:48

phiw13
Plugin Author
From: South-Western Japan
Registered: 2004-02-27
Posts: 3,606
Website

Re: Quality attribute depending on image type on, variable set?

jakob wrote #342091:

If you need different compression for avif and webp, make two separate if_variable lines. You might also be able to use txp:evaluate in place of the img-type variable.

Other ideas: I can’t remember exactly if you can do trim="." or ltrim="." in the first variable. If so, you could strip the dot off .ext for easier handling.

I have to try again with txp:evaluate, my first pass yesterday didn’t yield what I wanted. And yes, trim="." or ltrim="." work in this case too. Not sure if that brings better perf. Didn’t think about match=any thanks for that tip.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
phiw13 on Codeberg

Offline

Board footer

Powered by FluxBB