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

时间:2015-11-18 17:55:29

标签: php image gd image-resizing

我正在尝试使用GD动态重新采样图像。

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

我的类获取文件的路径名,然后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);
    }

}

这是原始的源图像 Original Image

调用$img->cover(1669, 556)后,我得到一个正确比例的图像,但不是拉伸原始图像以适应宽度,而是用黑色填充左边的空间。这是输出:

Output image

如何让图像伸展以填充黑色空间......?

BTW,使用相同的代码,风景图像看起来效果很好。

1 个答案:

答案 0 :(得分:0)

没关系,我只是迟钝,这就是全部..

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

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