Resizing images using php

前端 未结 7 1864
慢半拍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:54

    Add fitToSquare function to SimpleImage class, this functions calculate the x and y coordinates of the source image, then crop as square. I haven't test my function:) But it seems ok.

       function fitToSquare($width) {
          $new_image = imagecreatetruecolor($width, $height);
    
          $sourceX = 0;
          $sourceY = 0;
    
          if($this->getWidth() > $this->getHeight())
          {
              $sourceX = ($this->getWidth() - $this->getHeight()) / 2;
          }
          else
          {
              $sourceY = ($this->getHeight() - $this->getWidth()) / 2;
          }
    
          imagecopyresampled($new_image, $this->image, 0, 0, $sourceX, $sourceY, $width, $height, $this->getWidth(), $this->getHeight());
          $this->image = $new_image;
       }  
    

提交回复
热议问题