之前有個功能需要把文字寫入圖檔,當時我就想該怎麼弄,我第一時間想到就是添加水印,現在給大家分享個方法
<?php
//給圖檔添加水印
public function addImgWatermark(){
// //指定圖檔路徑
// $src = '../public/static/test.png';
// $font = '../public/static/dinotcondmedium.ttf';
header("Content-Type:text/html; charset=utf-8");
header('Content-type: image/png');// 告訴浏覽器,這個檔案,是一個png圖檔
$size = 20;
//字型類型,本例為黑體
$font = "../public/static/dinotcondmedium.ttf";
//顯示的文字
$text = "123456";
$text1 = "abcdef";
//建立一個長為500高為80的空白圖檔
// $img = imagecreate(500, 80);
$img = imagecreatefrompng ("../public/static/test.png");// 加載已有圖像
//給圖檔配置設定顔色
// imagecolorallocate($img, 0xff, 0xcc, 0xcc);
//設定字型顔色
$black = imagecolorallocate($img, 0, 0, 0);
//将ttf文字寫到圖檔中
imagettftext($img, $size, 0, 180, 176, $black, $font, $text);
imagettftext($img, $size, 0, 1, 300, $black, $font, $text1);
//發送頭資訊
header('Content-Type: image/png');
//輸出圖檔
// ImagePNG($img);
//儲存圖檔至指定路徑
ImagePNG($img, "../public/static/test.png");
imagedestroy($img);
}
?>