Add third choice ("square") to aspect switcher
CrookedNumber - October 23, 2009 - 19:02
| Project: | Imagecache Actions |
| Version: | 6.x-1.6 |
| Component: | Canvas Actions Module |
| Category: | feature request |
| Priority: | minor |
| Assigned: | CrookedNumber |
| Status: | active |
Description
First of all, I loove the addition of the aspect switcher. I used to insert this kind of logic at the theming level -- it's a huge pain. Then I moved over to imagecache_customactions.
In fact, I still use imagecache_customactions for one small reason: it allows me to accommodate for the -- admittedly rare -- situation in which the user uploads a square image.
(Currently, if width == height, the image_cache picks the 'portrait' preset.)
It seems like a pretty simple fix. Patch (tested locally) attached.
| Attachment | Size |
|---|---|
| add_square.patch | 2 KB |

#1
It's a small, and (I guess) logical alternative. Although I'd probably string together two cascading aspect switchers to do this job...
if (is mostly wide) {
use wide setting
} else { # is portrait or square
# Nested aspect switcher!
if (is sorta square) {
use square setting
} else {
use portrait setting
}
}
With this you can constuct all sorts of preferences - especially the ability to treat mostly square images (like between 90-110%) as squares.
... actually, I see that depends on some unpublished code that allows for proportional calculations - I'll see if I can find why I haven't committed that patch yet...
#2
Using aspect switcher to create a 'square' setting.
This is done through "chaining" two switches - "is it quite wide or not?", then "is it quite tall or not?" which leaves us with "must be square then."
First we create the three sizes we will be using, small-landscape, small-square, small-portrait. I'll just set those up with scale_and_crop.
We want wide images up to a ratio of 1:0.75 to be rendered wide. We want squarish images, with an aspect between 1:0.75 and 1:1.25 to be rendered square, and anything taller to be rendered tall.
To do this, we chain 2 rules. We need to build them backwards, the smaller sub-rule first, but to understand, I'l list them top down.
Rule 1. is the master rule, 3-aspects
if ratio is less than 1:.75, use small-wide. If greater, proceed to rule 2.
Rule 2. square-or-portrait
if ratio is less than 1:1.25, use small-square. If greater, use small-portrait.
To do this, we use the aspect switcher to link to the two sizes, and the ratio adjustment to move the switching point a little. Set the ratio adjustment to 1.25
With these (5!) rules in place, you can get the desired effect. This is a little trickier than just making a 'square' setting, but it allows for the required fudge factor to handle almost-square images.
You can nudge the adjustment factor to be looser or tighter. You can create even more chained rules, and define a 'super-wide' size.
The illustration shows the result of this set-up on a collection of images. The listed dimensions are those of the source images. You'll see that the mostly-square ones are rendered square.
The rule being applied is: 1 Is it wide?
For image 250x300, the aspect is ( 250/300 = 0.83 ) Normally that number (less than 1) would be classified as 'portrait', and with the adjustment (*0.75) that is still true, so the processing passes through to the portrait preset.
rule #2 it it tall?
This preset however does a different set of maths, and multiplies the aspect by 1.25, producing a result that causes it to trigger to 'landscape' choice. 'landscape' at this point is set to be the 'square' preset. And we get what we wanted.
#3
note the ratio adjustment option is only in todays -dev checkout!
Code was evaluated months ago, but only rewritten and rolled in just now.