PHP在圖片上添加文字的方法如下:
//向不同格式的圖片中間畫一個字符串(也是文字水印)
private function image($filename,$string){
//獲取圖片的屬性,第一個寬度,第二個高度,類型1=>gif,2=>jpeg,3=>png
list($width,$height,$type) = getimagesize($filename);
//可以處理的圖片類型
$types = array(1=>"gif",2=>"jpeg",3=>"png",);
//通過圖片類型去組合,可以創建對應圖片格式的,創建圖片資源的GD庫函數
$createfrom = "imagecreatefrom".$types[$type];
//通過“變量函數”去打對應的函數去創建圖片的資源
$image = $createfrom($filename);
//設置居中字體的X軸坐標位置
$x = ($width-imagefontwidth(5)*strlen($string))/2;
//設置居中字體的Y軸坐標位置
$y = ($height-imagefontheight(5))/1.18;
//設置字體的顏色為紅色
$textcolor = imagecolorallocate($image, 255, 0, 0);
//向圖片畫一個指定的字符串
// imagestring($image, 5, $x, $y, $string, $textcolor);
$font = "./Fonts/ACaslonPro-Bold.otf"; //字體在服務器上的絕對路徑
imagefttext($image, 105, 0, $x, $y, $textcolor, $font, $string);
//通過圖片類型去組合保存對應格式的圖片函數
$output = "image".$types[$type];
//通過變量函數去保存對應格式的圖片
$output($image,$filename);
imagedestroy($image);
}