Jump to content


Photo

How to get image scaled down proportionally (given a constraint)

images scale

  • Please log in to reply
5 replies to this topic

#1 landitus

landitus

    Distinguished Member

  • Members
  • PipPipPip
  • 96 posts
  • 6

  • LocationBuenos Aires, Argentina

Posted 12 January 2012 - 03:35 PM

Is it possible to get an image scaled down proportionally, like for example, I need it to fit in a 400px box (no height constraint). And the images might have different heights that would respond to the given constraint? Right now it seems I need to specify the width and height of the image that it placed, but the problem is I have images of different sizes and proportions.

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3380

  • LocationAtlanta, GA

Posted 12 January 2012 - 04:06 PM

You can specify 0 for the width or height to have it be proportional to the other dimension. Or you can just use the width(x) or height(y) functions rather than size(x, y) function. So in your case, I think what you are asking for is this:

$image = $page->image->width(400);


#3 landitus

landitus

    Distinguished Member

  • Members
  • PipPipPip
  • 96 posts
  • 6

  • LocationBuenos Aires, Argentina

Posted 12 January 2012 - 06:00 PM

Genius! Thanks a lot!

#4 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 16 January 2012 - 10:05 AM

With this method, any smaller image will be also resized, right? Is there any API way of doing something that only resizes bigger images?

#5 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 905

  • LocationVihti, Finland

Posted 16 January 2012 - 10:11 AM

There might be better way to do it, but this should work:

if ($image->width > 32 || $image->height > 32) $img = $image->size(32,32);
else $img = $image;


#6 diogo

diogo

    Hero Member

  • Moderators
  • 2,068 posts
  • 1179

  • LocationPorto, Portugal

Posted 16 January 2012 - 12:35 PM

Thanks Apeisa, I used it like this
if($image->width > 500) $image = $image->width(500); ?>
<img src="<?php echo $image->url?>" alt="">

but would be very nice to have a maxwidth() in PW





Also tagged with one or more of these keywords: images, scale

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users