Jump to content

Recommended Posts

Posted

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

Posted

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
Posted

That's perfect, thank you!

I'm fairly new to PHP (more of a JS knowledge) so these kind of things are still new to me. But very useful to learn. So thanks!

b

  • Like 1
Posted

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
  • 2 weeks later...
Posted

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
  • 4 months later...
Posted

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 ;-)

Posted

@PeterDK,

What didn't work? Don't you get an image output? Do you get errors? The code works for me..Make sure your replace $page->images with the actual name of your image field, e.g. $page->bilder...

Posted

The 'cropping false' technique didn't work. I didn't get the desired effect.

I got image output, put exactly the same as with 'cropping on default true'....

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