Resizing images using php

前端 未结 7 1868
慢半拍i
慢半拍i 2021-02-10 16:17

I\'m using a neat little php script to resize my images to fit in a 300x300 pixel square, while keeping the aspect ratio. I got the script from here.

Here is the whole s

7条回答
  •  醉酒成梦
    2021-02-10 16:44

    you cannot do resizetowidth and then resizetoheight.
    this will in fact resize it to 363x300.
    create a new function which preserves the aspect ratio:

    function scale_kar($maxwidth,$maxheight) { 
      $width = $this->getWidth();
      $height = $this->getheight();
      if($width > $height) {
              $ratio = $maxwidth / $width;
              $height = $height * $ratio;
              $this->resize($maxwidth,$height);
      }
      if($width < $height) {
              $ratio = $maxheight / $height;
              $width = $width * $ratio;
              $this->resize($width,$maxheight);
      }
    }
    

    then call it with:

     $image->scale_kar(300,300); 
    

提交回复
热议问题