Jump to content

image->size() without crop


statestreet
 Share

Recommended Posts

I'm building a little photography portfolio site and I'm running into a question with image->size(). For most applications, the sizing to fit and center-crop is great, but for this site, I want to have a maximum width and maximum height, so that if the photo is narrower than the specified size, it gets smaller and has space on the sides, and if it's shorter, it gets space on the top and bottom.

In other words, I'm looking to size the image such that it never gets cropped; you see the whole image even if it means making it smaller. Is there a way to do that with size()?

Link to comment
Share on other sites

You would have to write some logic to check the width and height using $image->width() and $image->height() to see which side you want to scale it down on, but then I think it's just a case of using $image->size() and only putting in the width or height

For exams I think I you had an image that was landscape and you wanted to scale to a width of 400px you should be able to do $image->size(400,0) and to scale by height only switch the numbers around.

This is all off the top of my head but I'm pretty sure that setting 0 as one of the dimensions just let's PW work out that other dimension for you.

Was that what you were after?

  • Like 2
Link to comment
Share on other sites

Ah yes, that's what I needed! It turns out $image->width() and $image->height() are actually rendering functions, so I was able to just do this:

$mainimage = $page->image;
if($mainimage->width >= $mainimage->height) {
 echo '<img src="' . $page->image->width(768)->url . '" alt="' . $page->title . '" id="displayimg" />';
} else {
 echo '<img src="' . $page->image->height(575)->url . '" alt="' . $page->title . '" id="displayimg" />';
};
  • Like 5
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...