调整图像大小并填充短边

Posted

技术标签:

【中文标题】调整图像大小并填充短边【英文标题】:Resizing images and padding short edges 【发布时间】:2014-09-06 15:40:39 【问题描述】:

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

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

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

Resize/crop/pad a picture to a fixed size Resize an image and fill gaps of proportions with a color

提前致谢

$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】:

解决方案:

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;

【讨论】:

以上是关于调整图像大小并填充短边的主要内容,如果未能解决你的问题,请参考以下文章

图像调整大小和裁剪

Dragonfly/imagemagick 调整大小和裁剪,并可能添加白色填充

在裁剪之前调整 UIImage 或 CIImage 的大小会拉伸图像吗?

调整图像大小并用颜色填充比例间隙

如何使 UITableView 不改变 imageView 的框架,或调整图像大小以填充框架大小?

水平调整 BMP 图像大小的问题 (C#)