- 论坛徽章:
- 0
|
PHP给图片增加水印得类
Php代码- 1.<?php
- 2./*
- 3.+--------------------------------------------------------------------------
- 4.| 生成加水印的图片类 (支持水印为图片或者文字)
- 5.| ============================
- 6.| by JackMing (感谢Dash和其他不知道姓名的朋友支持,本类在这些朋友作品的基础上创建)
- 7.+--------------------------------------------------------------------------
- 8.*/
- 9.
- 10.Class Gimage{
- 11.var $src_image_name = ""; //输入图片的文件名(必须包含路径名)
- 12.var $jpeg_quality = 90; //jpeg图片质量
- 13.var $save_image_file = ''; //输出文件名
- 14.var $wm_image_name = ""; //水印图片的文件名(必须包含路径名)
- 15.var $wm_image_pos = 1; //水印图片放置的位置
- 16.// 0 = middle
- 17.// 1 = top left
- 18.// 2 = top right
- 19.// 3 = bottom right
- 20.// 4 = bottom left
- 21.// 5 = top middle
- 22.// 6 = middle right
- 23.// 7 = bottom middle
- 24.// 8 = middle left
- 25.//other = 3
- 26.var $wm_image_transition = 20; //水印图片与原图片的融合度 (1=100)
- 27.
- 28.var $wm_text = ""; //水印文字(支持中英文以及带有\r\n的跨行文字)
- 29.var $wm_text_size = 20; //水印文字大小
- 30.var $wm_text_angle = 4; //水印文字角度,这个值尽量不要更改
- 31.var $wm_text_pos = 3; //水印文字放置位置
- 32.var $wm_text_font = ""; //水印文字的字体
- 33.var $wm_text_color = "#cccccc"; //水印字体的颜色值
- 34.
- 35.function create($filename="")
- 36.{
- 37.if ($filename) $this->src_image_name = strtolower(trim($filename));
- 38.
- 39.$src_image_type = $this->get_type($this->src_image_name);
- 40.$src_image = $this->createImage($src_image_type,$this->src_image_name);
- 41.if (!$src_image) return;
- 42.$src_image_w=ImageSX($src_image);
- 43.$src_image_h=ImageSY($src_image);
- 44.
- 45.
- 46.if ($this->wm_image_name){
- 47.$this->wm_image_name = strtolower(trim($this->wm_image_name));
- 48.$wm_image_type = $this->get_type($this->wm_image_name);
- 49.$wm_image = $this->createImage($wm_image_type,$this->wm_image_name);
- 50.$wm_image_w=ImageSX($wm_image);
- 51.$wm_image_h=ImageSY($wm_image);
- 52.$temp_wm_image = $this->getPos($src_image_w,$src_image_h,$this->wm_image_pos,$wm_image);
- 53.$wm_image_x = $temp_wm_image["dest_x"];
- 54.$wm_image_y = $temp_wm_image["dest_y"];
- 55.imageCopyMerge($src_image, $wm_image,$wm_image_x,$wm_image_y,0,0,$wm_image_w,$wm_image_h,$this->wm_image_transition);
- 56.}
- 57.
- 58.if ($this->wm_text){
- 59.$this->wm_text = $this->gb2utf8($this->wm_text);
- 60.$temp_wm_text = $this->getPos($src_image_w,$src_image_h,$this->wm_text_pos);
- 61.$wm_text_x = $temp_wm_text["dest_x"];
- 62.$wm_text_y = $temp_wm_text["dest_y"];
- 63.if(preg_match("/([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])/i", $this->wm_text_color, $color))
- 64.{
- 65.$red = hexdec($color[1]);
- 66.$green = hexdec($color[2]);
- 67.$blue = hexdec($color[3]);
- 68.$wm_text_color = imagecolorallocate($src_image, $red,$green,$blue);
- 69.}else{
- 70.$wm_text_color = imagecolorallocate($src_image, 255,255,255);
- 71.}
- 72.
- 73.imagettftext($src_image, $this->wm_text_size, $this->wm_angle, $wm_text_x, $wm_text_y, $wm_text_color,$this->wm_text_font,$this->wm_text);
- 74.}
- 75.
- 76.if ($this->save_file)
- 77.{
- 78.switch ($this->output_type){
- 79.case 'gif':$src_img=ImagePNG($src_image, $this->save_file); break;
- 80.case 'jpeg':$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
- 81.case 'png':$src_img=ImagePNG($src_image, $this->save_file); break;
- 82.default:$src_img=ImageJPEG($src_image, $this->save_file, $this->jpeg_quality); break;
- 83.}
- 84.}
- 85.else
- 86.{
- 87.if ($src_image_type = "jpg") $src_image_type="jpeg";
- 88.header("Content-type: image/{$src_image_type}");
- 89.switch ($src_image_type){
- 90.case 'gif':$src_img=ImagePNG($src_image); break;
- 91.case 'jpg':$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
- 92.case 'png':$src_img=ImagePNG($src_image);break;
- 93.default:$src_img=ImageJPEG($src_image, "", $this->jpeg_quality);break;
- 94.}
- 95.}
- 96.imagedestroy($src_image);
- 97.}
- 98.
- 99./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 100./*
- 101.createImage 根据文件名和类型创建图片
- 102.内部函数
- 103.
- 104.$type: 图片的类型,包括gif,jpg,png
- 105.$img_name:图片文件名,包括路径名,例如 " ./mouse.jpg"
- 106.*/
- 107.function createImage($type,$img_name){
- 108.if (!$type){
- 109.$type = $this->get_type($img_name);
- 110.}
- 111.
- 112.switch ($type){
- 113.case 'gif':
- 114.if (function_exists('imagecreatefromgif'))
- 115.$tmp_img=@ImageCreateFromGIF($img_name);
- 116.break;
- 117.case 'jpg':
- 118.$tmp_img=ImageCreateFromJPEG($img_name);
- 119.break;
- 120.case 'png':
- 121.$tmp_img=ImageCreateFromPNG($img_name);
- 122.break;
- 123.default:
- 124.$tmp_img=ImageCreateFromString($img_name);
- 125.break;
- 126.}
- 127.return $tmp_img;
- 128.}
- 129.
- 130./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 131.getPos 根据源图像的长、宽,位置代码,水印图片id来生成把水印放置到源图像中的位置
- 132.内部函数
- 133.
- 134.$sourcefile_width: 源图像的宽
- 135.$sourcefile_height: 原图像的高
- 136.$pos: 位置代码
- 137.// 0 = middle
- 138.// 1 = top left
- 139.// 2 = top right
- 140.// 3 = bottom right
- 141.// 4 = bottom left
- 142.// 5 = top middle
- 143.// 6 = middle right
- 144.// 7 = bottom middle
- 145.// 8 = middle left
- 146.$wm_image: 水印图片ID
- 147.*/
- 148.function getPos($sourcefile_width,$sourcefile_height,$pos,$wm_image=""){
- 149.if($wm_image){
- 150.$insertfile_width = ImageSx($wm_image);
- 151.$insertfile_height = ImageSy($wm_image);
- 152.}else {
- 153.$lineCount = explode("\r\n",$this->wm_text);
- 154.$fontSize = imagettfbbox($this->wm_text_size,$this->wm_text_angle,$this->wm_text_font,$this->wm_text);
- 155.$insertfile_width = $fontSize[2] - $fontSize[0];
- 156.$insertfile_height = count($lineCount)*($fontSize[1] - $fontSize[3]);
- 157.}
- 158.
- 159.switch ($pos){
- 160.case 0:
- 161.$dest_x = ( $sourcefile_width / 2 ) - ( $insertfile_width / 2 );
- 162.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
- 163.break;
- 164.
- 165.case 1:
- 166.$dest_x = 0;
- 167.if ($this->wm_text){
- 168.$dest_y = $insertfile_height;
- 169.}else{
- 170.$dest_y = 0;
- 171.}
- 172.break;
- 173.
- 174.case 2:
- 175.$dest_x = $sourcefile_width - $insertfile_width;
- 176.if ($this->wm_text){
- 177.$dest_y = $insertfile_height;
- 178.}else{
- 179.$dest_y = 0;
- 180.}
- 181.break;
- 182.
- 183.case 3:
- 184.$dest_x = $sourcefile_width - $insertfile_width;
- 185.$dest_y = $sourcefile_height - $insertfile_height;
- 186.break;
- 187.
- 188.case 4:
- 189.$dest_x = 0;
- 190.$dest_y = $sourcefile_height - $insertfile_height;
- 191.break;
- 192.
- 193.case 5:
- 194.$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
- 195.if ($this->wm_text){
- 196.$dest_y = $insertfile_height;
- 197.}else{
- 198.$dest_y = 0;
- 199.}
- 200.break;
- 201.
- 202.case 6:
- 203.$dest_x = $sourcefile_width - $insertfile_width;
- 204.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
- 205.break;
- 206.
- 207.case 7:
- 208.$dest_x = ( ( $sourcefile_width - $insertfile_width ) / 2 );
- 209.$dest_y = $sourcefile_height - $insertfile_height;
- 210.break;
- 211.
- 212.case 8:
- 213.$dest_x = 0;
- 214.$dest_y = ( $sourcefile_height / 2 ) - ( $insertfile_height / 2 );
- 215.break;
- 216.
- 217.default:
- 218.$dest_x = $sourcefile_width - $insertfile_width;
- 219.$dest_y = $sourcefile_height - $insertfile_height;
- 220.break;
- 221.}
- 222.return array("dest_x"=>$dest_x,"dest_y"=>$dest_y);
- 223.}
- 224.
- 225./*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 226.gb2utf8 指定的文字转换为UTF-8格式,包括中英文混合
- 227.内部函数
- 228.*/
- 229.function gb2utf8($gb)
- 230.{
- 231.if(!trim($gb))
- 232.return $gb;
- 233.$filename="./gb2312.txt";
- 234.$tmp=file($filename);
- 235.$codetable=array();
- 236.while(list($key,$value)=each($tmp))
- 237.$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
- 238.
- 239.$utf8="";
- 240.while($gb)
- 241.{
- 242.if (ord(substr($gb,0,1))>127)
- 243.{
- 244.$tthis=substr($gb,0,2);
- 245.$gb=substr($gb,2,strlen($gb)-2);
- 246.$utf8.=$this->u2utf8(hexdec($codetable[hexdec(bin2hex($tthis))-0x8080]));
- 247.}
- 248.else
- 249.{
- 250.$tthis=substr($gb,0,1);
- 251.$gb=substr($gb,1,strlen($gb)-1);
- 252.$utf8.=$this->u2utf8($tthis);
- 253.}
- 254.}
- 255.
- 256.return $utf8;
- 257.}
- 258.
- 259.function u2utf8($c)
- 260.{
- 261.$str="";
- 262.if ($c < 0x80)
- 263.{
- 264.$str.=$c;
- 265.}
- 266.else if ($c < 0x800)
- 267.{
- 268.$str.=chr(0xC0 | $c>>6);
- 269.$str.=chr(0x80 | $c & 0x3F);
- 270.}
- 271.else if ($c < 0x10000)
- 272.{
- 273.$str.=chr(0xE0 | $c>>12);
- 274.$str.=chr(0x80 | $c>>6 & 0x3F);
- 275.$str.=chr(0x80 | $c & 0x3F);
- 276.}
- 277.else if ($c < 0x200000)
- 278.{
- 279.$str.=chr(0xF0 | $c>>18);
- 280.$str.=chr(0x80 | $c>>12 & 0x3F);
- 281.$str.=chr(0x80 | $c>>6 & 0x3F);
- 282.$str.=chr(0x80 | $c & 0x3F);
- 283.}
- 284.return $str;
- 285.}
- 286.
- 287./*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 288.get_type 获得图片的格式,包括jpg,png,gif
- 289.内部函数
- 290.
- 291.$img_name: 图片文件名,可以包括路径名
- 292.*/
- 293.function get_type($img_name)//获取图像文件类型
- 294.{
- 295.$name_array = explode(".",$img_name);
- 296.if (preg_match("/\.(jpg|jpeg|gif|png)$/", $img_name, $matches))
- 297.{
- 298.$type = strtolower($matches[1]);
- 299.}
- 300.else
- 301.{
- 302.$type = "string";
- 303.}
- 304.return $type;
- 305.}
- 306.
- 307.}
- 308.
- 309.?>
-
复制代码 使用方法:- $img = new Gimage();
- $img->wm_text = "www.discuz.com";
- $img->wm_text_font = "./STXINWEI.TTF";
- $img->create("./mouse.jpg");
复制代码 就可以了,其中
mouse.jpg是你要在其上添加水印的图片名称,注意包含路径名
STXINWEI.TTF是字体文件的路径名+文件名
这就是一个简单的测试。如果要调整更复杂的显示效果,只要修改一下类中的属性就可以了,例如把字体放大就可以
$img->wm_text_size = 20;
增加水印图片就可以
$img->wm_image_name="文件名"; |
|