php中要给中文加水印需要作一些简单的处理,一就是要把字符转换成gbk,还有就是要加载中文字体,有了这两个条件在图片加中文水印就简单了,php几个实例代码如下:
-
- <?php
- Header("Content-type: image/png");
- $im = imagecreate(400 , 300);
- $gray = ImageColorAllocate($im , 235 , 235 , 235);
- $pink = ImageColorAllocate($im, 255 , 128 , 255);
-
- $fontfile = "simkai.ttf";
-
-
- $str = iconv('GB2312','UTF-8','中文水印');
- ImageTTFText($im, 30, 0, 100, 200, $pink , $fontfile , $str);
-
- Imagepng($im);
- ImageDestroy($im);
- ?>
-
-
-
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
- function img_text($img, $new_img, $text, $text_size, $text_angle, $text_x, $text_y, $text_font, $r, $g, $b){
-
- $text=iconv("gb2312","UTF-8",$text);
- Header("Content-type: image/gif");
- $im = @imagecreatefromstring(file_get_contents($img)) or die ("打开图片失败!");
- $color = ImageColorAllocate($im, $r,$g,$b);
-
-
-
-
-
-
-
-
-
- ImageTTFText($im, $text_size, $text_angle, $text_x, $text_y, $color, $text_font, $text);
-
- if ($new_img==""):
- ImageGif($im);
- else:
- ImageGif($im,$new_img);
- endif;
-
- ImageDestroy($im);
- }
- ?>
-
-
-
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)
- {
- $isWaterImage = FALSE;
- $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;
-
-
- if(!emptyempty($waterImage) && file_exists($waterImage))
- {
- $isWaterImage = TRUE;
- $water_info = getimagesize($waterImage);
- $water_w = $water_info[0];
- $water_h = $water_info[1];
-
- switch($water_info[2])
- {
- case 1:$water_im = imagecreatefromgif($waterImage);break;
- case 2:$water_im = imagecreatefromjpeg($waterImage);break;
- case 3:$water_im = imagecreatefrompng($waterImage);break;
- default:die($formatMsg);
- }
- }
-
-
- if(!emptyempty($groundImage) && file_exists($groundImage))
- {
- $ground_info = getimagesize($groundImage);
- $ground_w = $ground_info[0];
- $ground_h = $ground_info[1];
-
- switch($ground_info[2])
- {
- case 1:$ground_im = imagecreatefromgif($groundImage);break;
- case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
- case 3:$ground_im = imagecreatefrompng($groundImage);break;
- default:die($formatMsg);
- }
- }
- else
- {
- die(”需要加水印的图片不存在!”);
- }
-
-
- if($isWaterImage)
- {
- $w = $water_w;
- $h = $water_h;
- $label = “图片的”;
- }
- else
- {
- $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);
- $w = $temp[2] - $temp[6];
- $h = $temp[3] - $temp[7];
- unset($temp);
- $label = “文字区域”;
- }
- if( ($ground_w<$w) || ($ground_h<$h) )
- {
- echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;
- return;
- }
- switch($waterPos)
- {
- case 0:
- $posX = rand(0,($ground_w - $w));
- $posY = rand(0,($ground_h - $h));
- break;
- case 1:
- $posX = 0;
- $posY = 0;
- break;
- case 2:
- $posX = ($ground_w - $w) / 2;
- $posY = 0;
- break;
- case 3:
- $posX = $ground_w - $w;
- $posY = 0;
- break;
- case 4:
- $posX = 0;
- $posY = ($ground_h - $h) / 2;
- break;
- case 5:
- $posX = ($ground_w - $w) / 2;
- $posY = ($ground_h - $h) / 2;
- break;
- case 6:
- $posX = $ground_w - $w;
- $posY = ($ground_h - $h) / 2;
- break;
- case 7:
- $posX = 0;
- $posY = $ground_h - $h;
- break;
- case 8:
- $posX = ($ground_w - $w) / 2;
- $posY = $ground_h - $h;
- break;
- case 9:
- $posX = $ground_w - $w;
- $posY = $ground_h - $h;
- break;
- default:
- $posX = rand(0,($ground_w - $w));
- $posY = rand(0,($ground_h - $h));
- break;
- }
-
-
- imagealphablending($ground_im, true);
-
- if($isWaterImage)
- {
- imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);
- }
- else
- {
- if( !emptyempty($textColor) && (strlen($textColor)==7) )
- {
- $R = hexdec(substr($textColor,1,2));
- $G = hexdec(substr($textColor,3,2));
- $B = hexdec(substr($textColor,5));
- }
- else
- {
- die(”水印文字颜色格式不正确!”);
- }
- imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
- }
-
-
- @unlink($groundImage);
- switch($ground_info[2])
- {
- case 1:imagegif($ground_im,$groundImage);break;
- case 2:imagejpeg($ground_im,$groundImage);break;
- case 3:imagepng($ground_im,$groundImage);break;
- default:die($errorMsg);
- }
-
-
- if(isset($water_info)) unset($water_info);
- if(isset($water_im)) imagedestroy($water_im);
- unset($ground_info);
- imagedestroy($ground_im);
- }
-
- $id=$_REQUEST['id'];
- $num = count($_FILES['userfile']['name']);
- print_r($_FILES['userfile']);
- print_r($_FILES['userfile']['name']);
-
- echo $num;
- echo “<bR>”;
- if(isset($id)){
- for($i=0;$i<$id;$i++){
-
- if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
- {
- $uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];
- echo “<br>”;
- echo $uploadfile;
- if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))
- {
- echo “OK<br>”;
-
-
-
-
-
- $waterImage=”logo_ok1.gif”;
- imageWaterMark($uploadfile,9,$waterImage);
-
- echo “<img src=”".$uploadfile.”” border=”0”>”;
- }
- else
- {
- echo “Fail<br>”;
- }
- }
- }
- }
-
- ?>
- <form enctype=”multipart/form-data” method=”POST”>
- <?php
- for($a=0;$a<$id;$a++){
- echo “文件: <input name=”userfile[]” type=”file”><br>”;
-
- }
- ?>
- <input type=”submit” value=”上传”>
- </form>
|