Resizing images using php

前端 未结 7 1865
慢半拍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

    I fixed the function posted by @adenizc and created a pull-request to the SimpleImage library. Here is the function:

    /**
         * Square crop 
         *
         * This function crops the image to fit the given size which is both the width and the height
         *
         * @param int   $size
         *
         * @return SimpleImage
         */
    
        function square($size) {
            $new = imagecreatetruecolor($size, $size);
    
            $sourceX = 0;
            $sourceY = 0;
    
            if($this->width > $this->height) {
                $this->fit_to_height($size);
                $sourceX = ($this->width - $size) / 2;
            }
            else {
                $this->fit_to_width($size);
                $sourceY = ($this->height - $size) / 2;
            }
    
            imagealphablending($new, false);
            imagesavealpha($new, true);
            imagecopy($new, $this->image, 0, 0, $sourceX, $sourceY, $size, $size);
            $this->image = $new;
    
            return $this;
        }
    

提交回复
热议问题