给图片添加水印的类

gmsgms 2010-11-20 10:25:18
从书上摘取的代码

<?php //imagelogo.class.php ($filename为源图片的名称;$img_name为图片文件名,需指定保存路径名)
class imagelogo
{
var $input_image_file = ""; //输入图片的文件名
var $output_image_file = ""; //生成文件的名称
var $logo_image_name = ""; //包含存放路径的水印图片的文件名
var $logo_text = ""; //水印文件名
var $logo_text_size = 18; //水印文字大小
var $logo_text_angle = 4; //水印文字角度
var $logo_text_pos = 6; //水印文字放置位置
var $logo_text_font = ""; //水印文字字体
var $logo_text_color = "#ffffff"; //水印文字颜色
var $logo_image_pos = 6; //水印图片放置位置
// 0 = 随机位置 1 = 顶部居左 2 = 顶部居中 3 = 顶部居右 4 = 底部居左 5 = 底部居中 6 = 底部居右 7 = 中间居左 8 = 居中 9 = 中间居右
var $logo_image_transition = 25; //水印图片与原图片的融合度(1-100)
var $jpeg_quality = 75; //JPEG图片的质量

//生成水印图片
function create($filename = "")
{
if ($filename)
$this->input_image_file = strtolower(trim($filename));
$src_image_type = $this->get_type($this->input_image_file);
$src_image = $this->createImage($src_image_type, $this->input_image_file);
if (!$src_image)
return;
$src_image_w = imagesx($src_image);
$src_image_h = imagesy($src_image);

//开始处理水印Logo图片信息,将两个图片合成为一个图片
if ($this->logo_image_name)
{
$this->logo_image_name = strtolower(trim($this->logo_image_name));
$logo_image_type = $this->get_type($this->logo_image_name);
$logo_image = $this->createImage($logo_image_type, $this->logo_image_name);
$logo_image_w = imagesx($logo_image);
$logo_image_h = imagesy($logo_image);
$temp_logo_image = $this->getPos($src_image_w, $src_image_h, $this->logo_image_pos, $logo_image);
$logo_image_x = $temp_logo_image["dest_x"];
$logo_image_y = $temp_logo_image["dest_y"];
imagecopymerge($src_image, $logo_image, $logo_image_x, $logo_image_y, 0, 0, $logo_image_w, $logo_image_h, $this->logo_image_transition);
}

//水印为纯文本
if ($this->logo_text)
{
$this->logo_text = iconv('GB2312','UTF-8', $this->logo_text);
$temp_logo_text = $this->getPos($src_image_w, $src_image_h, $this->logo_text_pos);
$logo_text_x = $temp_logo_text["dest_x"];
$logo_text_y = $temp_logo_text["dest_y"];
if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->logo_text_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
$logo_text_color = imagecolorallocate($src_image, $red, $green, $blue);
}
else
{
$logo_text_color = imagecolorallocate($src_image, 255, 255, 255);
}
//用TrueType字体向图像写入文本
imagettftext($src_image, $this->logo_text_size, $this->logo_angle, $logo_text_x, $logo_text_y, $logo_text_color, $this->logo_text_font, $this->logo_text);
}

//保存生成的图片为新的文件
if ($this->output_image_file)
{ //若程序运行此分支时正常
switch ($this->get_type($this->output_image_file))
{
case 'gif':
$src_img = imagegif($src_image, $this->output_image_file);
break;
case 'jpg':
$src_img = imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality);
break;
case 'png':
$src_img = imagepng($src_image, $this->output_image_file);
break;
default:
$src_img = imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality);
break;
}
}
else //在原来图片的基础上生成新的合成图片
{ //若程序运行此分支时不正常,不能在浏览器显示水印图片,只显示红叉标记
if ($src_image_type == "jpg")
$src_image_type = "jpeg";
header("content-type:image/{$src_image_type}");
switch ($src_image_type)
{
case 'gif':
$src_img = imagegif($src_image);
break;
case 'jpg':
$src_img = imagejpeg($src_image, "", $this->jpeg_quality);
break;
case 'png':
$src_img = imagepng($src_image);
break;
default:
$src_img = imagejpeg($src_image, "", $this->jpeg_quality);
break;
}
}
imagedestroy($src_image);
}

//根据文件名和类型创建图片
function createImage($type, $img_name)
{
if (!$type)
$type = $this->get_type($img_name);
switch ($type)
{
case 'gif':
if (function_exists('imagecreatefromgif'))
$tmp_img = @imagecreatefromgif($img_name);
break;
case 'jpg':
$tmp_img = imagecreatefromjpeg($img_name);
break;
case 'png':
$tmp_img = imagecreatefrompng($img_name);
break;
default:
$tmp_img = imagecreatefromstring($img_name);
break;
}
return $tmp_img;
}

//根据图像的长、宽、位置代码和水印ID将生成的水印放置到源图像的位置
function getPos($sourcefile_width, $sourcefile_height, $pos, $logo_image = "")
{
if ($logo_image)
{
$insertfile_width = imagesx($logo_image);
$insertfile_height = imagesy($logo_image);
}
else
{
$lineCount = explode("\r\n", $this->logo_text);
$fontSize = imagettfbbox($this->logo_text_size, $this->logo_text_angle, $this->logo_text_font, $this->logo_text);
$insertfile_width = $fontSize[2] - $fontSize[0];
$insertfile_height = ($fontSize[3] - $fontSize[5]);
}
switch ($pos)
{
case 1:
$dest_x = 0;
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 2:
$dest_x = (($sourcefile_width - $insertfile_width)/2);
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 3:
$dest_x = $sourcefile_width - $insertfile_width;
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 4:
$dest_x = 0;
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 5:
$dest_x = (($sourcefile_width - $insertfile_width)/2);
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 6:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 7:
$dest_x = 0;
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
case 8:
$dest_x = ($sourcefile_width/2) - ($insertfile_width/2);
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
case 9:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
default:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;
}
return array("dest_x" => $dest_x, "dest_y" => $dest_y);
}

//获取图片的格式
function get_type($img_name)
{
$name_array = explode(".", $img_name);
if (preg_match("/.(gif|jpg|jpeg|png)$/", $img_name, $matches))
$type = strtolower($matches[1]);
else
$type = "string";
return $type;
}
}

$imglogo = new imagelogo();
$imglogo->input_image_file = "./pic.jpg";
$imglogo->logo_image_name = "./logo.jpg";
$imglogo->logo_image_pos = 6;
$imglogo->logo_image_transition = 75;
$imglogo->jpeg_quality = 75;
$imglogo->output_image_file = "./newpic.jpg"; //指定输出图片文件名
$imglogo->create();
?>

若指定输出图片文件名,程序能正确生成水印图片,但如果不指定输出图片文件名,水印图片却不能在浏览器显示,只显示带红叉的标记。
请高手指点一下,问题在哪里。
...全文
83 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gmsgms 2010-11-24
  • 打赏
  • 举报
回复
把代码重新写了一遍就好了,估计是录入时出错,把正确代码贴上。

<?php
class imagelogo
{
var $input_image_file = ""; //输入图片的文件名
var $output_image_file = ""; //生成文件的名称
var $logo_image_name = ""; //包含存放路径的水印图片的文件名
var $logo_text = ""; //水印文件名
var $logo_text_size = 18; //水印文字大小
var $logo_text_angle = 4; //水印文字角度
var $logo_text_pos = 6; //水印文字放置位置
var $logo_text_font = ""; //水印文字字体
var $logo_text_color = "#ffffff"; //水印文字颜色
var $logo_image_pos = 6; //水印图片放置位置
// 0 = 随机位置 1 = 顶部居左 2 = 顶部居中 3 = 顶部居右 4 = 底部居左 5 = 底部居中 6 = 底部居右 7 = 中间居左 8 = 居中 9 = 中间居右
var $logo_image_transition = 25; //水印图片与原图片的融合度(1-100)
var $jpeg_quality = 75; //JPEG图片的质量

//生成水印图片
function create($filename = "")
{
if ($filename)
$this->input_image_file = strtolower(trim($filename));
$src_image_type = $this->get_type($this->input_image_file);
$src_image = $this->createImage($src_image_type, $this->input_image_file);
if (!$src_image)
return;
$src_image_w = imagesx($src_image);
$src_image_h = imagesy($src_image);

//开始处理水印Logo图片信息,将两个图片合成为一个图片
if ($this->logo_image_name)
{
$this->logo_image_name = strtolower(trim($this->logo_image_name));
$logo_image_type = $this->get_type($this->logo_image_name);
$logo_image = $this->createImage($logo_image_type, $this->logo_image_name);
$logo_image_w = imagesx($logo_image);
$logo_image_h = imagesy($logo_image);
$temp_logo_image = $this->getPos($src_image_w, $src_image_h, $this->logo_image_pos, $logo_image);
$logo_image_x = $temp_logo_image["dest_x"];
$logo_image_y = $temp_logo_image["dest_y"];
imagecopymerge($src_image, $logo_image, $logo_image_x, $logo_image_y, 0, 0, $logo_image_w, $logo_image_h, $this->logo_image_transition);
}

//水印为纯文本
if ($this->logo_text)
{
$this->logo_text = iconv('GB2312','UTF-8', $this->logo_text);
$temp_logo_text = $this->getPos($src_image_w, $src_image_h, $this->logo_text_pos);
$logo_text_x = $temp_logo_text["dest_x"];
$logo_text_y = $temp_logo_text["dest_y"];
if (preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->logo_text_color, $color))
{
$red = hexdec($color[1]);
$green = hexdec($color[2]);
$blue = hexdec($color[3]);
$logo_text_color = imagecolorallocate($src_image, $red, $green, $blue);
}
else
{
$logo_text_color = imagecolorallocate($src_image, 255, 255, 255);
}
//用TrueType字体向图像写入文本
imagettftext($src_image, $this->logo_text_size, $this->logo_angle, $logo_text_x, $logo_text_y, $logo_text_color, $this->logo_text_font, $this->logo_text);
}


if ($this->output_image_file) //保存生成的图片为新的文件
{
switch ($this->get_type($this->output_image_file))
{
case 'gif':
$src_img = imagegif($src_image, $this->output_image_file);
break;
case 'jpg':
$src_img = imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality);
break;
case 'png':
$src_img = imagepng($src_image, $this->output_image_file);
break;
default:
$src_img = imagejpeg($src_image, $this->output_image_file, $this->jpeg_quality);
break;
}
}
else //在原来图片的基础上生成新的合成图片
{
if ($src_image_type == "jpg")
$src_image_type = "jpeg";
header("content-type:image/{$src_image_type}");
switch ($src_image_type)
{
case 'gif':
$src_img = imagegif($src_image);
break;
case 'jpg':
$src_img = imagejpeg($src_image, "", $this->jpeg_quality);
break;
case 'png':
$src_img = imagepng($src_image);
break;
default:
$src_img = imagejpeg($src_image, "", $this->jpeg_quality);
break;
}
}
imagedestroy($src_image);
}

//根据文件名和类型创建图片
function createImage($type, $img_name)
{
if (!$type)
$type = $this->get_type($img_name);
switch ($type)
{
case 'gif':
if (function_exists('imagecreatefromgif'))
$tmp_img = @imagecreatefromgif($img_name);
break;
case 'jpg':
$tmp_img = imagecreatefromjpeg($img_name);
break;
case 'png':
$tmp_img = imagecreatefrompng($img_name);
break;
default:
$tmp_img = imagecreatefromstring($img_name);
break;
}
return $tmp_img;
}

//获取图片的格式
function get_type($img_name)
{
$name_array = explode(".", $img_name);
if (preg_match("/.(gif|jpg|jpeg|png)$/", $img_name, $matches))
$type = strtolower($matches[1]);
else
$type = "string";
return $type;
}

//根据图像的长、宽、位置代码和水印ID将生成的水印放置到源图像的位置
function getPos($sourcefile_width, $sourcefile_height, $pos, $logo_image = "")
{
if ($logo_image)
{
$insertfile_width = imagesx($logo_image);
$insertfile_height = imagesy($logo_image);
}
else
{
$lineCount = explode("\r\n", $this->logo_text);
$fontSize = imagettfbbox($this->logo_text_size, $this->logo_text_angle, $this->logo_text_font, $this->logo_text);
$insertfile_width = $fontSize[2] - $fontSize[0];
$insertfile_height = ($fontSize[3] - $fontSize[5]);
}
switch ($pos)
{
case 1:
$dest_x = 0;
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 2:
$dest_x = (($sourcefile_width - $insertfile_width)/2);
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 3:
$dest_x = $sourcefile_width - $insertfile_width;
if ($this->logo_text)
$dest_y = $insertfile_height;
else
$dest_y = 0;
break;
case 4:
$dest_x = 0;
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 5:
$dest_x = (($sourcefile_width - $insertfile_width)/2);
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 6:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;
case 7:
$dest_x = 0;
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
case 8:
$dest_x = ($sourcefile_width/2) - ($insertfile_width/2);
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
case 9:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = ($sourcefile_height/2) - ($insertfile_height/2);
break;
default:
$dest_x = $sourcefile_width - $insertfile_width;
$dest_y = $sourcefile_height - $insertfile_height;
break;
}
return array("dest_x" => $dest_x, "dest_y" => $dest_y);
}
}
?>
gmsgms 2010-11-23
  • 打赏
  • 举报
回复
再顶。。。
heyli 2010-11-20
  • 打赏
  • 举报
回复
var $output_image_file = ""; //生成文件的名称 这一个默认不要让他为空
燕昭王 2010-11-20
  • 打赏
  • 举报
回复
学习了
gmsgms 2010-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 heyli 的回复:]
var $output_image_file = ""; //生成文件的名称 这一个默认不要让他为空
[/Quote]这样的话就会一直生成图片文件,不能在浏览器显示水印图片了。我想要可以选择在浏览器显示水印图片或生成图片文件。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧