简体   繁体   English

在PNG图像PHP中添加PNG水印

[英]Adding PNG watermark in PNG image PHP

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

I'm trying to copy PNG to another PNG but I have no idea why they return like this 我正在尝试将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);


?>

You can see that, i have problem with images how can i make it right ? 你可以看到,我有图像问题我怎么能做对吗?

my watermark 我的水印

http://i.stack.imgur.com/TZFCa.png http://i.stack.imgur.com/TZFCa.png

Your code works fine, it appears that something is wrong with the base PNG image (not the watermark) because if you try the code with another png, it works fine, with a jpg, it also works fine. 您的代码工作正常,基本PNG图像(而不是水印)似乎有问题,因为如果您尝试使用另一个png的代码,它可以正常工作,使用jpg,它也可以正常工作。

It seems to be because the original PNG is a PNG8, because when converted to a PNG32 it works fine. 这似乎是因为原始的PNG是PNG8,因为当转换为PNG32时它工作正常。

You may try this. 你可以试试这个。 It works fine in my project. 它在我的项目中工作正常。

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

The watermark image should be in one of the following recommended formats: 水印图像应采用以下建议格式之一:

  • PNG-8 (recommended) PNG-8(推荐)
  • Colors: 256 or less 颜色:256或更少
  • Transparency: On/Off 透明度:开/关

  • GIF GIF

  • Colors: 256 or less 颜色:256或更少
  • Transparency: On/Off 透明度:开/关

  • JPEG JPEG

  • Colors: True color 颜色:真实的颜色
  • Transparency: n/a 透明度:不适用

The imagecopymerge function does not properly handle the PNG-24 images; imagecopymerge功能无法正确处理PNG-24图像; it is therefore not recommended. 因此不建议这样做。

If you are using Adobe Photoshop to create watermark images, it is recommended that you use "Save for Web" command with the following settings: 如果您使用Adobe Photoshop创建水印图像,建议您使用“Save for Web”命令并使用以下设置:

File Format: PNG-8, non-interlaced 文件格式:PNG-8,非隔行扫描

Color Reduction: Selective, 256 colors 颜色减少:选择性,256色

Dithering: Diffusion, 88% 抖动:扩散,88%

Transparency: On, Matte: None 透明度:开,哑光:无

Transparency Dither: Diffusion Transparency Dither, 100% 透明度抖动:扩散透明度抖动,100%

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

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