给图片添加水印的类 本帖最后由 gmsgms 于 2010-11-20 10:28:27 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var $output_image_file = ""; //生成文件的名称 这一个默认不要让他为空 把代码重新写了一遍就好了,估计是录入时出错,把正确代码贴上。<?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); }}?> mysql php登录验证时的一个问题 一个有关数组的问题(自我感觉有点难) 如何给上传文件加限制 什么时候PHP也能真正OO一下啊! 请问:为什么我的PHP文件在浏览器中显示出来的是空白啊? 菜鸟请教phplib中的block问题 按一周显示的日历 什么情况下用到redis数据库?什么样的数据库适合放在redis里尼? php修改数据库数据 哪位大哥能准确描述strip_tags()的行为作用? 页面调过来后,怎样接受数据,非form形式跳转过来的
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);
}
}
?>