【问题标题】:Image GD Resizing Issue图像 GD 大小调整问题
【发布时间】:2017-12-09 17:08:08
【问题描述】:

所以我尝试拍摄两张大图像(但稍后我将总共组合 6 张图像),将它们调整为我从 photoshop 拍摄的 x、y 宽度、高度,然后将它们组合成一张 460 x 230 大小的图像。

这是我正在使用的代码

<?php

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagealphablending($src, false);
imagesavealpha($src, true);


//imagescale($dest, 396, 161.92);
$some = imagecreate(460, 230);

$dest2 = resize($dest, 396, 162);
$src2 = resize($src, 79.19, 79.19);

//imagecopyresized($dest, $dest, 0, 0, 0, 0, 396, 161.92, 1098, 449);
imagecopyresized($src, $src, 10, 10, 0, 0, 79.19, 79.19, 256, 256);
//$img2 = imagecopymerge($dest, $src, 0, 0, 0, 0, 256, 256, 100); //have to play with these numbers for it to work for you, etc.
imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50);

header('Content-Type: image/png');
imagepng($dest, 'merged2.png');
imagepng($dest2);
//file_put_contents('merged.png', $contents);
imagedestroy($dest);
imagedestroy($src);
imagedestroy($some);
imagedestroy($dest2);
imagedestroy($src2);
imagedestroy($img2);
//imagedestroy($then);

function resize($img, $width, $height, $stretch = false)
    {
        $temp = imagecreatetruecolor($width, $height);
        imagealphablending($temp, true);
        imagesavealpha($temp, true);

        $bg = imagecolorallocatealpha($temp, 0, 0, 0, 0); // Background color
        imagefill($temp, 0, 0, $bg);

        if ($stretch)
        {
            imagecopyresampled($temp, img, 0, 0, 0, 0, $width, $height, imagesx($img), imagesy($img));
        }
        else
        {
            if (imagesx($img) <= $width && imagesy($img) <= $height)
            {
                $fwidth = imagesx($img);
                $fheight = imagesy($img);
            }
            else
            {
                $wscale = $width / imagesx($img);
                $hscale = $height / imagesy($img);
                $scale = min($wscale, $hscale);
                $fwidth = $scale * imagesx($img);
                $fheight = $scale * imagesy($img);
            }
            imagecopyresampled($temp,
                $img,
                ($width - $fwidth) / 2, ($height - $fheight) / 2,
                0, 0,
                $fwidth, $fheight,
                imagesx($img), imagesy($img)
            );
        }
        return $temp;
    }

问题是渲染的图像非常褪色 因为这条线:

imagecopymerge($dest2, $src2, 0, 0, 0, 0, 460, 230, 50);

如果我将 PCT 值 50 更改为 100,它会显示一个带有黑色背景的图像(掩盖另一张图像),但如果我将其更改为 0,它只会显示另一张带有黑色背景的图像(掩盖另一张图片) 如果该值为 0 或 100,则显示的图像是全彩色的。如何将这两个图像合并在一起,同时保持它们的透明度和色彩活力?

【问题讨论】:

  • 我也尝试过使用这些值:imagealphablending($dest, true); imagesavealpha($dest, true); imagealphablending($src, true); imagesavealpha($src, true);

标签: php html image png gd


【解决方案1】:

使用imagecopy 代替imagecopymerge。您还需要在复制时正确指定源图像的尺寸:

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');

$dest2 = resize($dest, 396, 162);
imagedestroy($dest);
$src2 = resize($src, 79, 79); // should be int not float.
imagedestroy($src);

// the last 2 params must match the width/height of the $src2 image.
imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79);
imagedestroy($src2);

header('Content-Type: image/png');
imagepng($dest2);

imagedestroy($dest2);

您不需要更改 $dest$src 的 alpha 设置,因为它们不会被渲染 - 您渲染在您的 resize 函数中创建并返回的新图像资源。因此,您确实需要稍微更改功能:

function resize($img, $width, $height, $stretch = false)
{
    $temp = imagecreatetruecolor($width, $height);
    imagealphablending($temp, false); // changed to false.
    imagesavealpha($temp, true);
    ...

编辑:

您最好只使用imagescale 函数而不是使用您自己的resize 函数:

$dest = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/hero/ana/career-portrait.png');
$src = imagecreatefrompng('https://blzgdapipro-a.akamaihd.net/game/rank-icons/season-2/rank-6.png');

$dest2 = imagescale($dest, 396);
imagealphablending($dest2, false);
imagesavealpha($dest2, true);
$src2 = imagescale($src, 79);

imagecopy($dest2, $src2, 0, 0, 0, 0, 79, 79);

header('Content-Type: image/png');
imagepng($dest2);

imagedestroy($dest);
imagedestroy($src);
imagedestroy($dest2);
imagedestroy($src2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2012-08-12
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2012-10-17
    相关资源
    最近更新 更多