在 PHP 中向 PNG 图像添加 PNG 水印

4

结果: http://i.stack.imgur.com/p1kVz.png

我正在尝试将一个PNG复制到另一个PNG文件,但是我不知道为什么它们会出现这样的情况。

    <?php // File and new size
$filename = 'watermark.png';

// Get new sizes
list($width, $height) = getimagesize($filename);

// Load

$resim = 'http://www.petrominpng.com.pg/images/logo_big.png';

$ext = end(explode('.', $resim));


if($ext == "jpeg" || $ext == "jpg")
{
$thumb = imagecreatefromjpeg($resim); 
}
else if($ext == "png")
{
$thumb = imagecreatefrompng($resim); 
}

$sx = imagesx($thumb);
$sy = imagesy($thumb);

if($sx >= $sy)
{
    $sxd = $sx/2;
    $degisim = $sxd/$width;
    /*
    echo $sxd." ".$width."  ";
    echo $sxd-$width." |";
    */
    $sxy = $height * $degisim;
    /*
    echo " $sxy $height | $degisim";
    exit();
    */
}
else
{
    $sxy = $sy/2;
    $degisim = $sxy/$height;
    /*
    echo $sxd." ".$width."  ";
    echo $sxd-$width." |";
    */
    $sxd = $width * $degisim;
    /*
    echo " $sxy $height | $degisim";
    exit();
    */
}

$source = imagecreatefrompng($filename);

// Resize
imagecopyresized($thumb, $source, $sx/5, $sy/4, 0, 0, $sxd, $sxy, $width, $height);

// Output
header('Content-type: image/png');
imagepng($thumb);
imagedestroy($thumb);


?>

您可以看到,我在处理图片方面遇到了问题,如何解决?
我的水印。 http://i.stack.imgur.com/TZFCa.png
3个回答

1
你可以尝试这个。在我的项目中它运行得很好。

$stamp = imagecreatefrompng('watermark.png');
$im = imagecreatefrompng('source.png');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 1;
$marge_bottom = 1;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// OUTPUT IMAGE:
header("Content-Type: image/png");
imagesavealpha($im, true);
imagepng($im, NULL); 

1
你的代码运行良好,似乎基础PNG图像(而非水印)存在问题,因为如果你尝试使用另一个png文件,它可以正常工作;使用jpg也能正常工作。
看起来是由于原始PNG是PNG8格式,因为当转换为PNG32格式时,它可以正常工作。

我该如何将它转换为PNG32? - user2678561
FYI...在Photoshop中,即使你说要创建32位PNG,它仍然会生成8位PNG。 - Homer6
使用Imagick可以实现$im = new Imagick($image); $im->setImageDepth(32); $im->setImageFormat('PNG32'); $im->writeImage($filename); - Liam J
哦,当我尝试将其转换为32位时,我无法选择透明背景,并且我想自己转换它而不是使用PHP。 - user2678561
这是您的图像的PNG32版本:http://s22.postimg.org/vgouhyhb5/image.png - Liam J

0
水印图像应采用以下推荐格式之一:
  • PNG-8(推荐)
  • 颜色:256或更少
  • 透明度:开/关

  • GIF

  • 颜色:256或更少
  • 透明度:开/关

  • JPEG

  • 颜色:真彩色
  • 透明度:不适用
由于 imagecopymerge 函数不能正确处理 PNG-24 图像,因此不建议使用。 如果您正在使用 Adobe Photoshop 创建水印图像,则建议使用“另存为 Web”命令,并使用以下设置:

文件格式:PNG-8,非交错

颜色还原:选择性,256 色

抖动:扩散,88%

透明度:开,底色:无

透明度抖动:扩散透明度抖动,100%


当我按照你说的做时,http://u1312.hizliresim.com/1j/8/vb7hn.png 现在就是这样。 - user2678561

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接