1 <?php
2
3 /**
4 * 测试 of test
5 * php生成水印类(生成缩略图.缩略图水印), 网上其它的代码都PNG不透明的问题..这个全透明.包括gif的操作, 包括生成的缩略图 ,支持图片水印,文字水印,水印透明
6 * @author wc1217
7 */
8 class test extends spController{
9
10 function index(){
11 // $d = spClass('encryptClass')->encode(json_encode(array('wc1217', 'wcwc')));
12 // echo $d . '<br>';
13 // echo spClass('encryptClass')->decode($d);
14 echo filesize('E:\\WEB\\chuangyiwang\\views\\bottom.tpl');
15 }
16
17 function waterMarkAndThumb(){
18 $file_path = 'E://WEB//chuangyiwang//themes//images//main.png'; //原文件名
19 $makeThumb = true; //是否创建缩略图
20 $thumbMark = true; //缩略图是否要水印
21 $waterText = false; //是否启用文字水印,启用直接使用数组:如下:
22 //array('waterText' => "创意革命网 | www.chuangyiwang.com", 'waterTextColor' => "#000000", 'waterTextSize' => '10', 'waterTextFont' => OA_LIB_PATH . '/simsun.ttc')
23 $imgWaterPos = 9; //水印位置
24 $transparent = 50; //图片水印透明度
25 $bigFile = 10; //超过多少M不操作(视php限制内存而定,0为不准定)
26 $thumbnail = array(
27 'upload_dir' => 'nail',
28 'max_width' => 150,
29 'max_height' => 400,
30 'jpeg_quality' => 80,
31 'png_quality' => 9,
32 ); //缩略图参数
33
34 if($bigFile && filesize($file_path) > $bigFile * 1024 * 1024)
35 return false;
36 //后缀
37 $extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION));
38
39 //得到原始文件宽高
40 list($img_width, $img_height) = getimagesize($file_path);
41 if(!$img_width || !$img_height){
42 return false;
43 }
44
45 //创建等尺寸的真彩色画布
46 $new_img = $this->getImageTrueColor($img_width, $img_height, $extension);
47 //生成图片参数
48 switch($extension){
49 case 'jpg':
50 case 'jpeg':
51 $src_img = imagecreatefromjpeg($file_path);
52 $write_image = 'imagejpeg';
53 $image_quality = $thumbnail['jpeg_quality'];
54 break;
55 case 'bmp':
56 $src_img = imagecreatefromwbmp($file_path);
57 $write_image = 'imagewbmp';
58 $image_quality = null;
59 break;
60 case 'gif':
61 $src_img = imagecreatefromgif($file_path);
62 $write_image = 'imagegif';
63 $image_quality = null;
64 break;
65 case 'png':
66 $src_img = imagecreatefrompng($file_path);
67 $write_image = 'imagepng';
68 $image_quality = $thumbnail['png_quality'];
69 break;
70 default:
71 return false;
72 }
73 //将图载入画布
74 imagecopyresampled($new_img, $src_img, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
75
76 if(is_array($waterText)){
77 //得到文字大小信息
78 $waterTextInfo = imagettfbbox(ceil($waterText['waterTextSize'] * 1.2), 0, $waterText['waterTextFont'], $waterText['waterText']);
79 $maskWidth = $waterTextInfo[4] - $waterTextInfo[6];
80 $maskHeight = $waterTextInfo[1] - $waterTextInfo[7];
81 unset($waterTextInfo);
82 $waterPos = $this->setWaterPos($imgWaterPos, $img_width, $img_height, $maskWidth, $maskHeight);
83 //使用文字水印
84 $new_img && imagettftext($new_img, $waterText['waterTextSize'], 0, $waterPos[0], $maskHeight + $waterPos[1]
85 , imagecolorallocate($new_img, hexdec(substr($waterText['waterTextColor'], 1, 2)), hexdec(substr($waterText['waterTextColor'], 3, 2))
86 , hexdec(substr($waterText['waterTextColor'], 5, 2))), $waterText['waterTextFont'], $waterText['waterText']);
87 }else{
88 //以下为固定水印图片
89 $maskWidth = 226;
90 $maskHeight = 22;
91 $maskHandle = imagecreatefromgif(OA_LIB_PATH . '/waterMask.gif');
92 //设置水印位置
93 $waterPos = $this->setWaterPos($imgWaterPos, $img_width, $img_height, $maskWidth, $maskHeight);
94 //拷贝水印图片
95 $new_img && imagecopymerge($new_img, $maskHandle, $waterPos[0], $waterPos[1], 0, 0, $maskWidth, $maskHeight, $transparent);
96 }
97 //写入文件,原图直接覆盖
98 $succeed = $write_image($new_img, $file_path);
99
100 if($makeThumb){
101 //创建文件相关
102 $new_file_path = 'E:/WEB/test/watermark/'; //dirname($file_path) . DIRECTORY_SEPARATOR . $thumbnail['upload_dir'] . DIRECTORY_SEPARATOR;
103 if(!file_exists($new_file_path))
104 mkdir($new_file_path, 0777, true);
105 $new_file_path .= basename($file_path);
106
107 //生成缩略图比例,缩略图不要水印的话不能直接复制
108 $scale = min($thumbnail['max_width'] / $img_width, $thumbnail['max_height'] / $img_height);
109 if($thumbMark && $scale >= 1){
110 if($file_path !== $new_file_path){
111 return copy($file_path, $new_file_path);
112 }
113 return true;
114 }
115 //生成新的宽高
116 $new_width = $img_width * $scale;
117 $new_height = $img_height * $scale;
118 //创建新的宽高真彩色画布
119 $thumb_img = $this->getImageTrueColor($new_width, $new_height, $extension);
120 //将图载入画布
121 imagecopyresampled($thumb_img, ($thumbMark ? $new_img : $src_img), 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
122 //写入文件
123 $write_image($thumb_img, $new_file_path, $image_quality);
124 imagedestroy($thumb_img);
125 }
126 //销毁对象
127 imagedestroy($src_img);
128 imagedestroy($new_img);
129 return $succeed;
130 }
131
132 /**
133 * 生成一块真彩色画布
134 * @param int $img_width
135 * @param int $img_height
136 * @param string $img_type
137 * @return boolean or imageHandle
138 */
139 private function getImageTrueColor($img_width, $img_height, $img_type){
140 $new_img = imagecreatetruecolor($img_width, $img_height);
141 //生成图片参数
142 switch($img_type){
143 case 'gif':
144 imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0));
145 break;
146 case 'png':
147 imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0));
148 imagealphablending($new_img, false);
149 imagesavealpha($new_img, true);
150 break;
151 default:
152 return false;
153 }
154 return $new_img;
155 }
156
157 /**
158 * 设置水印位置
159 * @param int $waterPosType 位置
160 * @param int $groundImageWidth 原图宽
161 * @param int $groundImageHeight 原图高
162 * @param int $waterWidth 水印宽
163 * @param int $waterHeight 水印高
164 * @return array include x,y
165 */
166 function setWaterPos($waterPosType, $groundImageWidth, $groundImageHeight, $waterWidth, $waterHeight){
167 switch($waterPosType){
168 case 1://1为顶端居左
169 $waterPosX = 0;
170 $waterPosY = 0;
171 break;
172 case 2://2为顶端居中
173 $waterPosX = ($groundImageWidth - $waterWidth) / 2;
174 $waterPosY = 0;
175 break;
176 case 3://3为顶端居右
177 $waterPosX = $groundImageWidth - $waterWidth;
178 $waterPosY = 0;
179 break;
180 case 4://4为中部居左
181 $waterPosX = 0;
182 $waterPosY = ($groundImageHeight - $waterHeight) / 2;
183 break;
184 case 5://5为中部居中
185 $waterPosX = ($groundImageWidth - $waterWidth) / 2;
186 $waterPosY = ($groundImageHeight - $waterHeight) / 2;
187 break;
188 case 6://6为中部居右
189 $waterPosX = $groundImageWidth - $waterWidth;
190 $waterPosY = ($groundImageHeight - $waterHeight) / 2;
191 break;
192 case 7://7为底端居左
193 $waterPosX = 0;
194 $waterPosY = $groundImageHeight - $waterHeight * rand(115, 125) / 100;
195 break;
196 case 8://8为底端居中
197 $waterPosX = ($groundImageWidth - $waterWidth) / 2;
198 $waterPosY = $groundImageHeight - $waterHeight * rand(115, 125) / 100;
199 break;
200 case 9://9为底端居右
201 $waterPosX = $groundImageWidth - $waterWidth;
202 $waterPosY = $groundImageHeight - $waterHeight * rand(115, 120) / 100;
203 break;
204 default://随机
205 $waterPosX = rand(0, ($groundImageWidth - $waterWidth));
206 $waterPosY = rand(0, ($groundImageHeight - $waterHeight));
207 }
208 return array($waterPosX, $waterPosY);
209 }
210
211 }
212
213 ?>