Jump to content


Photo

image->size() without crop

solved image crop

  • Please log in to reply
2 replies to this topic

#1 statestreet

statestreet

    Jr. Member

  • Members
  • PipPip
  • 43 posts
  • 11

Posted 07 April 2012 - 11:26 PM

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()?

#2 Pete

Pete

    Administrator

  • Administrators
  • 1,754 posts
  • 652

  • LocationChester, England

Posted 08 April 2012 - 02:27 AM

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?

#3 statestreet

statestreet

    Jr. Member

  • Members
  • PipPip
  • 43 posts
  • 11

Posted 08 April 2012 - 02:47 PM

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" />';
};






Also tagged with one or more of these keywords: solved, image, crop

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users