PHP實(shí)現(xiàn)文字寫(xiě)入圖片功能
本文實(shí)例為大家分享了PHP實(shí)現(xiàn)文字寫(xiě)入圖片的具體代碼,供大家參考,具體內(nèi)容如下
/**
* PHP實(shí)現(xiàn)文字寫(xiě)入圖片
*/
class wordsOnImg {
public $config = null;
/**
* @param $config 傳入?yún)?shù)
* @param $config['file'] 圖片文件
* @param $config['size'] 文字大小
* @param $config['angle'] 文字的水平角度
* @param $config['fontfile'] 字體文件路徑
* @param $config['width'] 預(yù)先設(shè)置的寬度
* @param $config['x'] 開(kāi)始寫(xiě)入時(shí)的橫坐標(biāo)
* @param $config['y'] 開(kāi)始寫(xiě)入時(shí)的縱坐標(biāo)
*/
public function __construct($config=null){
if(empty($config)){
return 'must be config';
}
$fileArr = explode(".",$config['file']);
$config['file_name'] = $fileArr[0];
$config['file_ext'] = $fileArr[1];
$this->config = $config;
}
/**
* PHP實(shí)現(xiàn)圖片上寫(xiě)入實(shí)現(xiàn)文字自動(dòng)換行
* @param $fontsize 字體大小
* @param $angle 角度
* @param $font 字體路徑
* @param $string 要寫(xiě)在圖片上的文字
* @param $width 預(yù)先設(shè)置圖片上文字的寬度
* @param $flag 換行時(shí)單詞不折行
*/
public function wordWrap($fontsize,$angle,$font,$string,$width,$flag=true) {
$content = "";
if($flag){
$words = explode(" ",$string);
foreach ($words as $key=>$value) {
$teststr = $content." ".$value;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
//判斷拼接后的字符串是否超過(guò)預(yù)設(shè)的寬度
if(($testbox[2] > $width)) {
$content .= "\n";
}
$content .= $value." ";
}
}else{
//將字符串拆分成一個(gè)個(gè)單字 保存到數(shù)組 letter 中
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
// 判斷拼接后的字符串是否超過(guò)預(yù)設(shè)的寬度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
}
return $content;
}
/**
* 實(shí)現(xiàn)寫(xiě)入圖片
* @param $text 要寫(xiě)入的文字
* @param $flag 是否直接輸出到瀏覽器,默認(rèn)是
*/
public function writeWordsToImg($text,$flag=true){
if(empty($this->config)){
return 'must be config';
}
//獲取圖片大小
$img_pathWH = getimagesize($this->config['file']);
//打開(kāi)指定的圖片文件
$im = imagecreatefrompng($this->config['file']);
#設(shè)置水印字體顏色
$color = imagecolorallocatealpha($im,0, 0, 255, 75);//藍(lán)色
$have = false;
if(stripos($text,"<br/>")!== false){
$have = true;
}
if($have){
$words_text = explode("<br/>",$text);
$words_text[0] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[0], $this->config['width']); //自動(dòng)換行處理
$words_text[1] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[1], $this->config['width']); //自動(dòng)換行處理
$words_text[2] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[2], $this->config['width']); //自動(dòng)換行處理
imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y'], $color, $this->config['fontfile'], $words_text[0]);
imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y']+30, $color, $this->config['fontfile'], " ".$words_text[1]);
imagettftext($im, $this->config['size'], $this->config['angle'], $img_pathWH[0]/2+70, $img_pathWH[1]-80, $color, $this->config['fontfile'], $words_text[2]);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
imagedestroy($im);
}
$words_text = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $text, $this->config['width']); //自動(dòng)換行處理
imagettftext($im, $this->config['size'], $this->config['angle'], $this->config['x'], $this->config['y'], $color, $this->config['fontfile'], $words_text);
if($flag){
header("content-type:image/png");
imagepng($im);
imagedestroy($im);
}
imagepng($im,$this->config['file_name'].'_1.'.$this->config['file_ext']);
imagedestroy($im);
}
}
$text = "Dear Kang<br/>If you can hold something up and put it down, it is called weight lifting;if you can hold something up but can never put it down, it's called bueden bearing. Pitifully, most of people are bearing heavy burdens when they are in love.\n\nBeing nice to someone you dislike doesn't mean you're a hypocritical people. It means you're mature enough to tolerate your dislike towards them.<br/>Mr. Kang";
$data = array(
'file'=>'20171226152410.png',
'size'=>12,
'angle'=>0,
'fontfile'=>'./Font/ChalkboardSE.ttc',
'width'=>270,
'x'=>20,
'y'=>70
);
//使用
$wordsOnImgObj = new wordsOnImg($data);
$wordsOnImgObj->writeWordsToImg($text);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php實(shí)現(xiàn)簡(jiǎn)單的MVC框架實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)簡(jiǎn)單的MVC框架,較為詳細(xì)的分析了php實(shí)現(xiàn)MVC框架的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
PHP同時(shí)連接多個(gè)mysql數(shù)據(jù)庫(kù)示例代碼
這篇文章主要介紹了PHP同時(shí)連接多個(gè)mysql數(shù)據(jù)庫(kù)的具體實(shí)現(xiàn),需要的朋友可以參考下2014-03-03
THINKPHP在添加數(shù)據(jù)的時(shí)候獲取主鍵id的值方法
下面小編就為大家?guī)?lái)一篇THINKPHP在添加數(shù)據(jù)的時(shí)候獲取主鍵id的值方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
PHP實(shí)現(xiàn)將瀏覽歷史頁(yè)面網(wǎng)址保存到cookie的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)將瀏覽歷史頁(yè)面網(wǎng)址保存到cookie的方法,涉及php對(duì)cookie的讀取、字符串轉(zhuǎn)化及保存等技巧,需要的朋友可以參考下2015-01-01
PHP簡(jiǎn)單實(shí)現(xiàn)解析xml為數(shù)組的方法
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)解析xml為數(shù)組的方法,涉及php文件讀取、xml解析相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
php fsockopen中多線(xiàn)程問(wèn)題的解決辦法[翻譯]
最近研究php多線(xiàn)程的問(wèn)題,發(fā)現(xiàn)中文資源少的可憐,僅有的幾篇文章被轉(zhuǎn)了又轉(zhuǎn),但文中內(nèi)容價(jià)值有限。搜索過(guò)程中發(fā)現(xiàn)國(guó)外很多網(wǎng)站引用的一篇文章寫(xiě)的不錯(cuò),所以翻譯過(guò)來(lái)2011-11-11
php類(lèi)中private屬性繼承問(wèn)題分析
首先 這個(gè)題目就有點(diǎn)問(wèn)題 因?yàn)閜rivate屬性是不能被繼承的2012-11-11

