resize thumbnail to correct size (logic)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

resize thumbnail to correct size (logic)

Post by Skara »

Heh, this is bugging me.

Original was 1024 x 768.
Thumb ended up being 200 x 112.
Obviously not the same ratio...

Code: Select all

$new_h = 150;
  $new_w = 200;

  $old_x = imageSX($src_img);
  $old_y = imageSY($src_img);
  if ($old_x > $old_y) {
    $thumb_w = $new_w;
    $thumb_h = $old_y * ($new_h / $old_x);
  }
  elseif ($old_x < $old_y) {
    $thumb_w = $old_x * ($new_w / $old_y);
    $thumb_h = $new_h;
  }
  else {
    $thumb_w = $new_w;
    $thumb_h = $new_h;
  }
I need it to shrink so that the height is <= 150 and the width is <= 200.

Anyone want to have a go at fixing this?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Hah! min() is awesome.

Thanks a bunch!

Probably helps that it's 10 @ night here... ^^;
Post Reply