Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#2 | aspect-chaining.png | 313.58 KB | dman |
add_square.patch | 2 KB | CrookedNumber |
Comments
Comment #1
dman CreditAttribution: dman commentedIt'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...
Comment #2
dman CreditAttribution: dman commentedUsing 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.
Comment #3
dman CreditAttribution: dman commentednote the ratio adjustment option is only in todays -dev checkout!
Code was evaluated months ago, but only rewritten and rolled in just now.
Comment #4
oscarcarlsson CreditAttribution: oscarcarlsson commentedWouldn't it be a bit more simple just to allow inserting 0 as one of the values while specifying the portrait / landscape sizes? Ie, instead of inserting 500x333 px and 333x500 px (for 3:2), allowing people to insert 500x0 and 0x500, so that the max size in each dimension is kept but the ratio can be adjusted automagically? That would also solve the square-dimension problem.
Comment #5
dman CreditAttribution: dman commentedTri-state,
yeah, can be done, give it a go
Comment #6
Berliner-dupe CreditAttribution: Berliner-dupe commentedWorks patch in top for D7 too or is an alternative available?
Best Regards
Matthias
Comment #7
dman CreditAttribution: dman commentedIt can be done with logic, no patch. See above https://drupal.org/node/612986#comment-2209526
Comment #8
sarah CreditAttribution: sarah commentedWondering how I've never seen this before. You are *awesome*.