Proportional image resize

前端 未结 6 1005
南笙
南笙 2020-12-07 14:51

I\'m having a little bit of a problem scaling my images to a properly predefined size. I was wondering - since it is purely mathematics, if there\'s some sort of common logi

6条回答
  •  长情又很酷
    2020-12-07 15:45

    well I made this function to scale proportional, it uses a given width, height, and optionally the max width/height u want (depends on the given width and height)

       function scaleProportional($img_w,$img_h,$max=50)
       {
           $w = 0;
           $h = 0;
    
           $img_w > $img_h ? $w = $img_w / $img_h : $w = 1;
           $img_h > $img_w ? $h = $img_h / $img_w : $h = 1;
    
           $ws = $w > $h ? $ws = ($w / $w) * $max : $ws = (1 / $h) * $max;
           $hs = $h > $w ? $hs = ($h / $h) * $max : $hs = (1 / $w) * $max;
    
           return array(
               'width'=>$ws,
               'height'=>$hs
           );
       }
    

    usage:

                $getScale = scaleProportional(600,200,500);
                $targ_w = $getScale['width']; //returns 500
                $targ_h = $getScale['height']; //returns 16,6666667
    

提交回复
热议问题