php給圖片加文字水印

<?php
/*給圖片加文字水印的方法*/
$dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg';
$dst = imagecreatefromstring(file_get_contents($dst_path));
/*imagecreatefromstring()--從字符串中的圖像流新建一個圖像,返回一個圖像標示符,其表達了從給定字符串得來的圖像
圖像格式將自動監測,只要php支持jpeg,png,gif,wbmp,gd2.*/
 
$font = './t1.ttf';
$black = imagecolorallocate($dst, 0, 0, 0);
imagefttext($dst, 20, 0, 10, 30, $black, $font, 'Hello world!');
/*imagefttext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
$img由圖像建立函數返回的圖像資源
size要使用的水印的字體大小
angle(角度)文字的傾斜角度,若是是0度表明文字從左往右,若是是90度表明從上往下
x,y水印文字的第一個文字的起始位置
color是水印文字的顏色
fontfile,你但願使用truetype字體的路徑*/
list($dst_w,$dst_h,$dst_type) = getimagesize($dst_path);
/*list(mixed $varname[,mixed $......])--把數組中的值賦給一些變量
像array()同樣,這不是真正的函數,而是語言結構,List()用一步操做給一組變量進行賦值*/
/*getimagesize()能獲取到什麼信息?
getimagesize函數會返回圖像的全部信息,包括大小,類型等等*/
switch($dst_type){
    case 1://GIF
        header("content-type:image/gif");
        imagegif($dst);
        break;
    case 2://JPG
        header("content-type:image/jpeg");
        imagejpeg($dst);
        break;
    case 3://PNG
        header("content-type:image/png");
        imagepng($dst);
        break;
    default:
        break;
    /*imagepng--以PNG格式將圖像輸出到瀏覽器或文件
    imagepng()將GD圖像流(image)以png格式輸出到標註輸出(一般爲瀏覽器),或者若是用filename給出了文件名則將其輸出到文件*/
}
imagedestroy($dst);
?>
相關文章
相關標籤/搜索