在PNG图像PHP中添加PNG水印

时间:2013-12-08 19:53:53

标签: 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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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); 

答案 2 :(得分:0)

  

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

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

  •   
  • GIF

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

  •   
  • JPEG

  •   
  • 颜色:真彩色
  •   
  • 透明度:不适用
  •   
     

imagecopymerge功能无法正确处理PNG-24   图片;因此不推荐。

     

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

     

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

     

颜色减少:选择性,256色

     

抖动:扩散,88%

     

透明度:开,哑光:无

     

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