本文實例講述了PHP圖像處理 imagestring添加圖片水印與文字水印操作。分享給大家供大家參考,具體如下:
imagestring添加圖片水印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php header( "Content-Type: text/html;charset=utf-8" ); //指定圖片路徑 $src = '001.png' ; //獲取圖片信息 $info = getimagesize ( $src ); //獲取圖片擴展名 $type = image_type_to_extension( $info [2],false); //動態的把圖片導入內存中 $fun = "imagecreatefrom{$type}" ; $image = $fun ( '001.png' ); //指定字體顏色 $col = imagecolorallocatealpha( $image ,0,0,0,0); //R,G,B,透明度 //指定字體內容 $content = 'zhangsan' ; //給圖片添加文字 imagestring( $image ,5,190,255, $content , $col ); //指定字體內容 $content = '123456789' ; //給圖片添加文字 imagestring( $image ,5,190,285, $content , $col ); //指定字體內容 $content = '98.6' ; //給圖片添加文字 imagestring( $image ,5,190,320, $content , $col ); //指定輸入類型 header( 'Content-type:' . $info [ 'mime' ]); //動態的輸出圖片到瀏覽器中 $func = "image{$type}" ; $func ( $image ); //銷毀圖片 imagedestroy( $image ); ?> |
這里我們使用了imagestring方法來添加文字,但是imagestring并不支持中文字符,添加中文可以使用imagettftext來添加。
效果圖:
imagettftext添加中文水印
前面寫了PHP圖像處理 imagestring添加圖片水印,但是imagestring方法不能添加中文,所以現在使用imagettftext這個方法來添加中文。相比imagestring,imagettftext需要指定字體文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php //1. 打開要加水印的圖片 $image = imagecreatefromjpeg( "001.jpg" ); //2. 在畫布中繪制圖像 $bai = imagecolorallocate( $image , 255, 255, 255); //3. 設置水印文字 $text = 'abc我是水印123,。、 !@#dasdasda1231'; //使用指定的字體文件繪制文字 //參數2:字體大小 //參數3:字體傾斜的角度 //參數4、5:文字的x、y坐標 //參數6:文字的顏色 //參數7:字體文件 //參數8:繪制的文字 imagettftext( $image , 50, 0, 280, 1000, $bai , 'STXINGKA.TTF' , $text ); //4. 在瀏覽器直接輸出圖像資源 header( "Content-Type:image/jpeg" ); imagejpeg( $image ); //5. 銷毀圖像資源 imagedestroy( $image ); ?> |
效果圖:
希望本文所述對大家PHP程序設計有所幫助。
原文鏈接:https://blog.csdn.net/qq_17497931/article/details/86712977