I need help with image math.

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
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

I need help with image math.

Post by daedalus__ »

I'm really bad with math and I need help :(

Let's say I have an image:

image.jpg

On my page, image.jpg can be a maximum of 760 pixels wide. Now, when I display it at 760 pixels wide, I want it to be proporational still but I can't figure out the math.

Please help, I'm hopeless. :- /
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What was that you said earlier about reading the manual? :D

This should work for ya..

Code: Select all

list($width_orig, $height_orig) = getimagesize($MoveImageToPath);
      if ($MaxWidthProductThumbnail && ($width_orig < $height_orig)) {
        $width = ($MaxHeightProductThumbnail / $height_orig) * $width_orig;
        $height = $MaxHeightProductThumbnail;
      } else {
        $height = ($MaxWidthProductThumbnail / $width_orig) * $height_orig;
        $width = $MaxWidthProductThumbnail;
      }
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I know but I am honestly so bad at math the thought of it is more intimidating than mike tyson.

edit: i just realised that sounded stupid, ia m tired.

someone delete this topic lol
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Keep in mind that if the image is smaller than your minimum sizes, the sizes will be invalid. You should add code that checks to make sure that either the height or width is larger than 760 pixels.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Fixed and done, thanks so much.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

This is what I'm using:

Code: Select all

function ImageFitDimensions($src_w, $src_h, $dst_w, $dst_h) {
// returns dimensions of an image to fit into defined rectangle
  
  $ratio_img = $src_w / $src_h;
  
  if ($src_w <= $dst_w && $src_h <= $dst_h) {
    $w = $src_w;
    $h = $src_h;
  
  } else {
    if ($src_w > $dst_w) {
      $ratio_res = $dst_w / $src_w;
      
      $w = $src_w * $ratio_res;
      $h = $src_h * $ratio_res;
    
    } else if ($src_h > $dst_h) {
      $ratio_res = $dst_h / $src_h;
      
      $w = $src_w * $ratio_res;
      $h = $src_h * $ratio_res;
    }
    if ($w > $dst_w) {
      $ratio_res = $dst_w / $src_w;
      
      $w = $src_w * $ratio_res;
      $h = $src_h * $ratio_res;
    
    } else if ($h > $dst_h) {
      $ratio_res = $dst_h / $src_h;
      
      $w = $src_w * $ratio_res;
      $h = $src_h * $ratio_res;
    }
  }
  
  $w = Round($w);
  $h = Round($h);
  if ($w == 0) $w = 1;
  if ($h == 0) $h = 1;
  
  return Array($w, $h);
}
I hope it's not much more complicated that it should be :) Probably could be simplified.
Post Reply