[英]Watermarking a .png, .gif or .jpg with a .png, .gif or .jpg
I'm trying to write a function that will watermark any image (.png, .jpg or .gif) with another image that might also be one of those formats. 我正在尝试编写一个函数,将任何图像(.png,.jpg或.gif)与另一个也可能是其中一种格式的图像进行水印。 I wish both of the images to retain transparency.
我希望这两个图像保持透明度。 I do not care about gif animation.
我不关心gif动画。 Unfortunately, the watermark gets transparent where it's not supposed to, and doesn't entirely get transparent where it is.
不幸的是,水印在不应该的地方变得透明,并且不会完全透明。 I used some random images available to me to test this script, and my result looks like this:
我使用了一些随机图像来测试这个脚本,我的结果如下所示:
While the watermark images look like this: 虽然水印图像看起来像这样:
Here's my code: 这是我的代码:
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;
}
I've also tried imagecopymerge, to no avail. 我也尝试过imagecopymerge,但无济于事。 I'd appreciate any help on this.
我很感激你的帮助。
Edit: My images were displayed here, unlike what I expected. 编辑:我的图像显示在此处,与我的预期不同。 The watermarked image looks correct on the white background, but it does not on any other background.
带水印的图像在白色背景上看起来是正确的,但它不在任何其他背景上。
You can use the ImageMagick CLI for this. 您可以使用ImageMagick CLI进行此操作。 Here's a one liner:
这是一个班轮:
composite -dissolve 50% -geometry +20+10 -gravity SouthWest watermark.png input.png output.png
This will put watermark.png
on top of input.png
with an opacity of 50%
in the SouthWest
corner with an offset of +20, +10
pixels. 这将使
watermark.png
之上input.png
用的不透明度50%
在SouthWest
角落的偏移+20, +10
像素。 You can tweak it to your liking and use it in PHP with shell_exec
: 您可以根据自己的喜好调整它,并在PHP中使用
shell_exec
:
shell_exec('composite ...');
Reference: http://www.imagemagick.org/script/command-line-options.php 参考: http : //www.imagemagick.org/script/command-line-options.php
Sample image: 示例图片:
Edit: If you also want to extend the original image's height (by 20 pixels, for example), you need to do this first: 编辑:如果您还想扩展原始图像的高度(例如,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.