Advertisement
henke37

fitImageInBox

Mar 7th, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.     Fits an image in a given box, scaling it up or down as needed, retaining the aspect ratio of the image
  5.    
  6.     Input:
  7.     $width, the width of the box
  8.     $heigth, the height of the box
  9.     $img->width, the width of the image
  10.     $img->height, the height of the image
  11.    
  12.     Output:
  13.     $width, the width of the scaled image
  14.     $height, the height of the scaled image
  15.     */
  16.     private function fitImageInBox(&$width,&$height,$img) {
  17.         $ar=$img->width/$img->height;
  18.  
  19.         $newW=$height*$ar;
  20.         $newH=$width/$ar;
  21.  
  22.         if($newW>$width) {
  23.             $height=$newH;
  24.         } else {
  25.             $width=$newW;
  26.         }
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement