圖片加水印
阿新 • • 發佈:2018-12-10
imagecreatefromgd ---- 從GD檔案或URL新建一影象
imagecreatefromgd2 ----從GD2檔案或URL新建一影象
imagecreatefromgif ----由檔案或URL建立一個新影象
圖片加水印
1.將圖片檔案讀取出來,存入到一個變數中
<?php
//載入圖片
$imgfile = "xxb.jpg";
$str = file_get_contents("$imgfile");
2.建立影象
$img = imagecreatefromstring($str); if($img === false){ die('建立影象失敗'); }
3.輸出影象
header('content-type:image/png');
imagepng($img);
imagedestroy($img);
輸出的圖片如下:
4.加水印(即在影象上輸出字元)引入字型庫
$font = "STCAIYUN.TTF"; if(!file_exists($font)){ die('字型庫檔案不存在'); } $text = "XXX版權所有"; $color = imagecolorallocate($img, 255, 255, 255); imagettftext($img,18,0,25,25,$color,$font,$text); // header('content-type:image/png'); imagepng($img,'new'.$imgfile); imagedestroy($img); echo '生成影象檔案成功';
5.生成一個新影象(在資料夾下)會在名字前面加new
輸出加水印的圖片如下:
整體程式碼如下:
<?php //載入圖片 $imgfile = "xxb.jpg"; //將圖片檔案讀取出來,存入到一個變數中 $str = file_get_contents("$imgfile"); //字串編碼gb2312->utf8 // $str = iconv('gb2312','utf-8',$str); // $str = mb_convert_encoding($str,'utf-8','gb2312'); // $str = nl2br($str);//將換行符轉換成br標籤進行輸出 //建立影象 $img = imagecreatefromstring($str); if($img === false){ die('建立影象失敗'); } //加水印(即在影象上輸出字元) $font = "STCAIYUN.TTF"; if(!file_exists($font)){ die('字型庫檔案不存在'); } $text = "XXX版權所有"; $color = imagecolorallocate($img, 255, 255, 255); imagettftext($img,18,0,25,25,$color,$font,$text); // header('content-type:image/png'); imagepng($img,'new'.$imgfile); imagedestroy($img); echo '生成影象檔案成功';