Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

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)

Posted

You sentence doesn't make much sense, but anyway, the image fields max width and height settings is for when uploading images and nothing else.

Posted

sorry for being unclear, I meant:

but in that case a portrait image would have the desired width, but exceed the max-height of 320px
Posted

(currently trying to port a modx MaxiGallery to processwire)

Kunane,

If you have the time, when you are done, please do a short write-up how you approached/accomplished this. It will help others :). Thanks!

  • Like 1
Posted

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.

Posted

$thumburl = $image->width > $image->height ? $image->size(450,0)->url : $image->size(0,320);

echo "<img src='$thumburl'/>";

  • Like 2
  • 7 years later...
Posted


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;

 

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
×
×
  • Create New...