Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2021-02-06 17:58:24

giz
Member
From: New Zealand
Registered: 2004-07-26
Posts: 199
Website

Re: How to calculate article image aspect ratio?

Its not very pretty, but works:

<txp:php>
	$h = image_info(array('type' => 'h'));
	$w = image_info(array('type' => 'w'));
	$r = $h/$w;
	global $orientation;
	$orientation = 'square';
	if($r > 1.1) {
		$orientation = 'portrait';
	} else if($r < 0.9) {
		$orientation = 'landscape';
	}
	global $variable;
	$variable['image-orientation'] = $orientation;
</txp:php>
<img src="blah" class="<txp:variable name="image-orientation" />" alt />

Offline

#14 2021-02-08 04:32:55

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 285
Website

Re: How to calculate article image aspect ratio?

giz wrote #328589:

Its not very pretty, but works:

<txp:php>...

Thank you!


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#15 2021-02-08 11:59:38

nobi-wan
Member
Registered: 2021-02-05
Posts: 16

Re: How to calculate article image aspect ratio?

giz wrote #328589:

Its not very pretty, but works:

<txp:php>...

This looks like it should answer all my prayers, at least from the last 24 hours. However, like the txp hack I am I’ve added it to my article form, not knowing any better, and I get it thrown back at me with a

Tag error: ……. “Warning: Division by zero while parsing form default on page default”.

What’s the proper implementation?

Oh, I also tweaked the code to be tighter on the parsing out of ratios with < or > than 1. Wrong?

if($r > 1) { $orientation = ‘portrait’; } else if($r < 1) { $orientation = ‘landscape’; }

ps: can’t get bc. to work!

Offline

#16 2021-02-08 18:03:18

giz
Member
From: New Zealand
Registered: 2004-07-26
Posts: 199
Website

Re: How to calculate article image aspect ratio?

nobi-wan wrote #328620:

What’s the proper implementation?

The snippet needs to be within a <txp:images /> tag, so it knows which image(s) you’re specifying.

Oh, I also tweaked the code to be tighter on the parsing out of ratios with < or > than 1. Wrong?

No, that should work if you don’t need to allow for square images.

Offline

#17 2021-02-08 18:28:44

nobi-wan
Member
Registered: 2021-02-05
Posts: 16

Re: How to calculate article image aspect ratio?

Thanks!

Offline

#18 2021-02-09 11:16:15

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 285
Website

Re: How to calculate article image aspect ratio?

This became quite a popular thread, I noticed.

Once again, many thanks for all the input and assistance!


JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

Board footer

Powered by FluxBB