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.

CommentFileSizeAuthor
#2 aspect-chaining.png313.58 KBdman
add_square.patch2 KBCrookedNumber
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dman’s picture

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...

dman’s picture

FileSize
313.58 KB

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.

small-landscape [Scale And Crop] width: 200, height: 100
small-portrait [Scale And Crop] width: 100, height: 200
small-square [Scale And Crop] width: 140, height: 140
small-square-or-portrait [Aspect Switcher] Portrait size: small-portrait. Landscape size: small-square (switch at 1:1.25)
small-3-aspects [Aspect Switcher] Portrait size: small-square-or-portrait. Landscape size: small-landscape (switch at 1:.75)

 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.  

dman’s picture

note the ratio adjustment option is only in todays -dev checkout!
Code was evaluated months ago, but only rewritten and rolled in just now.

oscarcarlsson’s picture

Wouldn'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.

dman’s picture

Tri-state,
yeah, can be done, give it a go

Berliner-dupe’s picture

Version: 6.x-1.6 » 7.x-1.3

Works patch in top for D7 too or is an alternative available?

Best Regards
Matthias

dman’s picture

It can be done with logic, no patch. See above https://drupal.org/node/612986#comment-2209526

sarah’s picture

Wondering how I've never seen this before. You are *awesome*.