Jump to content

max width + max height setting for images on upload (keeping aspect ratio)


fenton
 Share

Recommended Posts

Hi there,

is there a way to set a max-width + max-height setting for images on upload and keep the aspect ratio?

e.g. a landscape format picture shouldnd't be wider that 450px, as well as not exceed 320px in height and a portrait format image shouldn't be higer than 320px

currently image->size(450,320,$options) will always produce an image 450px wide and 320px high even when it's a portrait format image is

is this somehow possible?

thanks a lot, cheers, j

Link to comment
Share on other sites

There's a max width and max height settings on the image field input configuration.

So use size() and keep aspect ratio you can leave on empty or 0, or use width().

$image->size(450,0)->url
Link to comment
Share on other sites

thanks Soma!

but in that case a portrait image would have the desired width, but exceed the max-width of 320px. Ideally, in that case, the max-height of 320px should be taken into account)

(currently trying to port a modx MaxiGallery to processwire)

Link to comment
Share on other sites

I guess you are talking about template output only?

You'd then have to build some additional logik and check which side is larger and resize that. You can get the size by $image->height and width.

Link to comment
Share on other sites

  • 7 years later...


Snippet variation to achieve a optical better result.
A little snippet addition to this older but maybe still useful post. If you use the orientation of an image to decide on your resize method you will often get an optical uneven impression, especially if aspect ratios of the images are very different (i.e. a row of logos).

In this case it could be better to define a maximum width and a maximum height value which both should not exceed. Here's the variaton of  @Soma's snippet to achieve that.

$mW = 180; // max width 
$mH = 80; // max height
$thumbUrl = ($mH / $image->height) > ($mW / $image->width) ? $image->size($mW,0)->url : $image->size(0,$mH)->url;

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...