[英]Watermarking a .png, .gif or .jpg with a .png, .gif or .jpg
我正在嘗試編寫一個函數,將任何圖像(.png,.jpg或.gif)與另一個也可能是其中一種格式的圖像進行水印。 我希望這兩個圖像保持透明度。 我不關心gif動畫。 不幸的是,水印在不應該的地方變得透明,並且不會完全透明。 我使用了一些隨機圖像來測試這個腳本,我的結果如下所示:
雖然水印圖像看起來像這樣:
這是我的代碼:
function watermark_get_left()
{
return NCFG_TEMPLATE_DIR . 'images/watermark_left.png';
}
function watermark_get_right()
{
return NCFG_TEMPLATE_DIR . 'images/watermark_right.png';
}
function create_img($path, $type)
{
switch($type)
{
case NCFG_IMAGE_JPG:
$img = imagecreatefromjpeg($path);
return $img;
case NCFG_IMAGE_GIF:
$img = imagecreatefromgif($path);
return $img;
case NCFG_IMAGE_PNG:
$img = imagecreatefrompng($path);
imagealphablending($img, false);
imagesavealpha($img, true);
return $img;
default:
return FALSE;
}
}
function img_transparency($new_img, $img, $type)
{
if($type == NCFG_IMAGE_GIF)
{
$transparent_index = imagecolortransparent($img);
if($transparent_index != -1)
{
$transparent_color = imagecolorsforindex($img, $transparent_index);
$transparent_new = imagecolorallocate($new_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
$transparent_new_index = imagecolortransparent($new_img, $transparent_new);
imagefill($new_img, 0, 0, $transparent_new_index);
}
}else if($type == NCFG_IMAGE_PNG){
imagecolortransparent($new_img, imagecolorallocatealpha($new_img, 0, 0, 0, 127));
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
}
}
function image_watermark($path, $new_path, &$w_height)
{
$type = get_image_type($path);
list($width, $height) = getimagesize($path);
if(!($img = create_img($path, $type)))
{
return FALSE;
}
$l_path = watermark_get_left();
$r_path = watermark_get_right();
if($l_exists = file_exists($l_path))
{
$l_type = get_image_type($l_path);
list($l_width, $l_height) = getimagesize($l_path);
if(!($l_img = create_img($l_path, $l_type)))
{
return FALSE;
}
}
if($r_exists = file_exists($r_path))
{
$r_type = get_image_type($r_path);
list($r_width, $r_height) = getimagesize($r_path);
if(!($r_img = create_img($r_path, $r_type)))
{
return FALSE;
}
}
$w_height = max(($r_exists ? $r_height : 0), ($l_exists ? $l_height : 0));
echo("w_height: $w_height<br/>");
$new_width = $width;
$new_height = $height + $w_height;
$new_img = imagecreatetruecolor($new_width, $new_height);
$bg_color = imagecolorallocate($new_img, 255,255,255);
imagefilledrectangle($new_img, 0, $height, $new_width - 1, $new_height - 1, $bg_color);
img_transparency($new_img, $img, $type);
imagecopy($new_img, $img, 0, 0, 0, 0, $width, $height);
if($l_exists)
{
imagecopy($new_img, $l_img, 0, $height, 0, 0, $l_width, $l_height);
}
if($r_exists)
{
imagecopy($new_img, $r_img, $new_width - $r_width, $height, 0, 0, $r_width, $r_height);
}
switch($type)
{
case NCFG_IMAGE_JPG:
imagejpeg($new_img, $new_path);
break;
case NCFG_IMAGE_GIF:
imagegif($new_img, $new_path);
break;
case NCFG_IMAGE_PNG:
imagepng($new_img, $new_path, 9);
break;
}
imagedestroy($new_img);
imagedestroy($img);
return TRUE;
}
我也嘗試過imagecopymerge,但無濟於事。 我很感激你的幫助。
編輯:我的圖像顯示在此處,與我的預期不同。 帶水印的圖像在白色背景上看起來是正確的,但它不在任何其他背景上。
您可以使用ImageMagick CLI進行此操作。 這是一個班輪:
composite -dissolve 50% -geometry +20+10 -gravity SouthWest watermark.png input.png output.png
這將使watermark.png
之上input.png
用的不透明度50%
在SouthWest
角落的偏移+20, +10
像素。 您可以根據自己的喜好調整它,並在PHP中使用shell_exec
:
shell_exec('composite ...');
參考: http : //www.imagemagick.org/script/command-line-options.php
示例圖片:
編輯:如果您還想擴展原始圖像的高度(例如,20像素),則需要先執行此操作:
convert original.png -bordercolor transparent -border 0x20 -background transparent -gravity NorthWest -extent +0+20 input.png
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.