2013-12-08 80 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);