Jump to content

Resize image to long edge value


benjaminE
 Share

Recommended Posts

Hello,

Is there any way to resize images using processwire and PHP so that a maximum long edge value is respected?

For example, I would like it so that landscape images are resized to a maximum width of (say) 900px and portrait images to a maximum height of 900px, and in both cases the second dimension adjusts to maintain the natural aspect ratio.

Can this be done with processwire?

Many thanks in advance,

b

Link to comment
Share on other sites

Something like this?

$image = $page->images->first();

$img = null;
if ($image->width >= $image->height) {
  // Lancscape
  $img = $image->width(900);
} else {
  // Portrait
  $img = $image->height(900);
}

echo "<img src='{$img->url}' alt=''>";
  • Like 6
Link to comment
Share on other sites

there is also a short way to write it in PHP. Maybe you know it from JS:

$image = $page->images->first();

$img = ($image->width >= $image->height) ? $image->width(900) : $image->height(900);

echo "<img src='{$img->url}' alt=''>";
  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...

BTW: the intended way I think is to call size instead of width or height, but with disabled cropping:

$image = $page->images->first();

$options = array('cropping'=>false);

echo "<img src='{$image->size(900,900,$options)->url}' alt=''>";

:cool:

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

BTW: the intended way I think is to call size instead of width or height, but with disabled cropping:

$image = $page->images->first();

$options = array('cropping'=>false);

echo "<img src='{$image->size(900,900,$options)->url}' alt=''>";

:cool:

I tried that, but didn't work out for me. I'm too new with php, so maybe it's not a bug. Could some more proficient test this out? Then we might raise an issue on github ;-)

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