PHP GD 未正确重新采样图像(未正确缩放)

PHP GD Not resampling image correctly (not scaling properly)

我正在尝试使用 GD 即时对图像重新采样。

基本上我正在尝试模仿 CSS 的 background-size: cover;

我的class取文件的路径名,然后cover方法取元素的维度覆盖

首先,这是代码:

<?php

class Image{
    
    public $filepath;
    public $width;
    public $height;
    public $mime;
    public $image;
    public $landscape;
    public $imageFunct;
    public $compression;
    
    public function __construct($fn){
        
        // The path to the /img directory
        $ImgPath = realpath(dirname(dirname(dirname(__FILE__))))."/img";
        
        // Find the actual path, look in all relevant directories
        $fp = $ImgPath."/$fn";
        if(!file_exists($fp)) $fp = $ImgPath."/backgrounds/$fn";
        if(!file_exists($fp)) throw new Exception("Image source file does not exist: $fp"); 
        
        $this->filepath = $fp;
        $data = getimagesize($fp);
        $this->width = $data[0];
        $this->height = $data[1];
        $this->landscape = $this->width > $this->height;
        $this->mime = $data['mime'];
        switch($this->mime){
            case("image/png"):
                $this->image = imagecreatefrompng($this->filepath);
                $this->imageFunct = 'imagepng';
                $this->compression = 9;
                break;
            case('image/jpeg'):
            case('image/pjpeg'):
            case('image/x-jps'):
                $this->image = imagecreatefromjpeg($this->filepath);
                $this->imageFunct = 'imagejpeg';
                $this->compression = 100;
                break;
            case('image/gif'):
                $this->image = imagecreatefromgif($this->filepath);
                $this->imageFunct = 'imagegif';
                break;
            default:
                throw new Exception("Invalid image type. Only excepts PNG, JPG, and GIF. You entered a {$this->mime} type image.");
        }
    }
    
    /**
     * scales the image to cover the dimensions provided
     * @param type $width
     * @param type $height
     * @param type $output_mimetype (defaults to the original image's mimtype)
     */
    public function cover($width, $height, $output_mimetype=''){
        
        $imgRatio = $this->height/$this->width;
        $canvasRatio = $height/$width;
        
        if ($canvasRatio > $imgRatio){
            $finalHeight = $height;
            $scale = $finalHeight / $this->height;
            $finalWidth = $this->width * $scale;
        }else{
            $finalWidth = $width;
            $scale = $finalWidth / $this->width;
            $finalHeight = $this->height * $scale;
        }
        
        // Resize the image
        $thumb = imagecreatetruecolor($finalWidth, $finalHeight);
        imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $finalWidth, $finalHeight, $this->height, $this->height);
        
        // Get output details
        switch(strtoupper($output_mimetype)){
            case('JPG'):
            case('JPEG'):
                $mimetype = 'image/jpeg';
                $funct = 'imagejpeg';
                $compression = 100;
                break;
            case('PNG'):
                $mimetype = 'image/png';
                $funct = 'imagepng';
                $compression = 9;
                break;
            case('GIF'):
                $mimetype = 'image/gif';
                $funct = 'imagegif';
                $compression = null;
                break;
            default:
                $mimetype = $this->mime;
                $funct = $this->imageFunct;
                $compression = $this->compression;
        }
        
        // Output and clear memory
        header('Content-Type: '.$mimetype);
        
        // Get and call the image creation 
        // function with the correct compression/quality
        if(!empty($compression)) 
            $funct($thumb, null, $compression);
        else 
            $funct($thumb, null);
        
        imagedestroy($thumb);
    }
    
}

这是原始源图像

调用 $img->cover(1669, 556) 后,我得到了一张比例正确的图像,但它没有拉伸原始图像以适应宽度,而是用黑色填充左侧的 space。这是输出:

如何让图像拉伸以填充黑色 space...?

顺便说一句,风景图像似乎使用相同的代码工作得很好。

没关系,我只是智障,仅此而已..

imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $finalWidth, $finalHeight, $this->height, $this->height);

可能不应该使用高度作为宽度..