繁体   English   中英

在PNG图像PHP中添加PNG水印

[英]Adding PNG watermark in PNG image PHP

结果: 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

您的代码工作正常,基本PNG图像(而不是水印)似乎有问题,因为如果您尝试使用另一个png的代码,它可以正常工作,使用jpg,它也可以正常工作。

这似乎是因为原始的PNG是PNG8,因为当转换为PNG32时它工作正常。

你可以试试这个。 它在我的项目中工作正常。

$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); 

水印图像应采用以下建议格式之一:

  • PNG-8(推荐)
  • 颜色:256或更少
  • 透明度:开/关

  • GIF

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

  • JPEG

  • 颜色:真实的颜色
  • 透明度:不适用

imagecopymerge功能无法正确处理PNG-24图像; 因此不建议这样做。

如果您使用Adobe Photoshop创建水印图像,建议您使用“Save for Web”命令并使用以下设置:

文件格式:PNG-8,非隔行扫描

颜色减少:选择性,256色

抖动:扩散,88%

透明度:开,哑光:无

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM