调整图像大小和填充短边

时间:2014-09-06 15:40:40

标签: php gd

下面的代码调整了图像的大小,但我对它有了新的要求。

要求:我想用任何颜色添加填充到短边。所以当它缩小到100(w)X150(h)时,它应该保存为填充后的150X150。

对于填充选项,我看过这两个帖子但未能实现。请帮我修改我的代码。

提前致谢

enter image description here

$this->defaults['width'] = 100;
$this->defaults['height'] = 150;


private function createThumbnail($sourceImage, $targetImage)
{
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);

    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            $sourceGdImage = imagecreatefromgif($sourceImage);
            break;
        case IMAGETYPE_JPEG:
            $sourceGdImage = imagecreatefromjpeg($sourceImage);
            break;
        case IMAGETYPE_PNG:
            $sourceGdImage = imagecreatefrompng($sourceImage);
            break;
    }

    if ($sourceGdImage === false)
    {
        return false;
    }

    $sourceAspectRatio = ($sourceWidth / $sourceHeight);
    $thumbnailAspectRatio = ($this->defaults['width'] / $this->defaults['height']);

    if ($sourceWidth <= $this->defaults['width'] && $sourceHeight <= $this->defaults['height'])
    {
        $thumbnailWidth = $sourceWidth;
        $thumbnailHeight = $sourceHeight;
    }
    elseif ($thumbnailAspectRatio > $sourceAspectRatio)
    {
        $thumbnailWidth = (int) ($this->defaults['height'] * $sourceAspectRatio);
        $thumbnailHeight = $this->defaults['height'];
    }
    else
    {
        $thumbnailWidth = $this->defaults['width'];
        $thumbnailHeight = (int) ($this->defaults['width'] / $sourceAspectRatio);
    }

    $thumbnailGdImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
    imagecopyresampled($thumbnailGdImage, $sourceGdImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            imagegif($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumbnailGdImage, $targetImage, 9);
            break;
    }
    imagedestroy($sourceGdImage);
    imagedestroy($thumbnailGdImage);

    return true;
}

1 个答案:

答案 0 :(得分:0)

<强> SOLUTION:

thumbnailDefaults:
    height: 200
    width: 200
    red: 200
    green: 0
    blue: 0

private function createThumbnailWithPadding($sourceImage, $targetImage)
{
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);

    $sourceGdImage = imagecreatefromstring(file_get_contents($sourceImage));

    //Determine scale based on the longest edge
    if ($sourceHeight > $sourceWidth)
    {
        $scale = ($this->thumbnailDefaults['height'] / $sourceHeight);
    }
    else
    {
        $scale = ($this->thumbnailDefaults['width'] / $sourceWidth);
    }

    //Calculate new image dimensions
    $thumbnailWidth =  ($sourceWidth * $scale);
    $thumbnailHeight =  ($sourceHeight * $scale);

    //Determine offset coordinates so that new image is centered
    $offsetX = (($this->thumbnailDefaults['width'] - $thumbnailWidth) / 2);
    $offsetY = (($this->thumbnailDefaults['height'] - $thumbnailHeight) / 2);

    //Create new image and fill with background colour
    $thumbnailGdImage = imagecreatetruecolor($this->thumbnailDefaults['width'], $this->thumbnailDefaults['height']);

    //Set background colour
    $bgColor = imagecolorallocate(
        $thumbnailGdImage,
        $this->thumbnailDefaults['red'],
        $this->thumbnailDefaults['green'],
        $this->thumbnailDefaults['blue']
    );

    //Fill background colour
    imagefill($thumbnailGdImage, 0, 0, $bgColor);
    //Copy and resize original image into center of new image
    imagecopyresampled($thumbnailGdImage, $sourceGdImage, $offsetX, $offsetY, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);

    //clearstatcache();

    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            imagegif($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumbnailGdImage, $targetImage, 9);
            break;
    }

    imagedestroy($sourceGdImage);
    imagedestroy($thumbnailGdImage);

    return true;
}