[英]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);
?>
你可以看到,我有圖像問題我怎么能做對嗎?
我的水印
您的代碼工作正常,基本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.