2013-12-08 81 views
4

结果:http://i.stack.imgur.com/p1kVz.png添加PNG水印PNG图片PHP

我试图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

回答

1

你的代码工作正常,看来东西是错误的基础PNG图像(不是水印),因为如果你尝试的代码与其他PNG,它工作正常,与一个JPG,它也可以正常工作。

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

+0

如何将其转换为PNG32 –

+0

仅供参考...在Photoshop中,即使你说要创建一个32位PNG,它仍然会生成一个8位PNG – Homer6

+0

这可能与imagick $ im = new Imagick($ image); $ im-> setImageDepth(32); $ im-> setImageFormat('PNG32'); $ im-> writeImage($ filename); –

0

水印图像应在下列推荐 格式之一:

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

  • GIF

  • 颜色:25 6个或更少
  • 透明度:开/关

  • JPEG

  • 颜色:真彩色
  • 透明度:N/A

的imagecopymerge函数不正确处理PNG- 24 图片;因此不推荐。

如果您在使用Adobe Photoshop创建水印图像,它是 建议您使用“另存为网页”命令与以下 设置:

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

颜色还原:选择性,256个色

抖动:扩散,88%

透明度:开,磨砂:无

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

+0

当我只是喜欢你所说的,http://u1312.hizliresim.com/1j/8/vb7hn.png现在就是这样 –

0

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

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