国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實現文字寫入圖片功能

2024-05-05 00:06:50
字體:
來源:轉載
供稿:網友

本文實例為大家分享了PHP實現文字寫入圖片的具體代碼,供大家參考,具體內容如下

/** * PHP實現文字寫入圖片 */class wordsOnImg {   public $config = null;   /**   * @param $config 傳入參數   * @param $config['file'] 圖片文件   * @param $config['size'] 文字大小   * @param $config['angle'] 文字的水平角度   * @param $config['fontfile'] 字體文件路徑   * @param $config['width'] 預先設置的寬度   * @param $config['x'] 開始寫入時的橫坐標   * @param $config['y'] 開始寫入時的縱坐標   */  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實現圖片上寫入實現文字自動換行   * @param $fontsize 字體大小   * @param $angle 角度   * @param $font 字體路徑   * @param $string 要寫在圖片上的文字   * @param $width 預先設置圖片上文字的寬度   * @param $flag  換行時單詞不折行   */  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);        //判斷拼接后的字符串是否超過預設的寬度        if(($testbox[2] > $width)) {          $content .= "/n";        }        $content .= $value." ";      }    }else{      //將字符串拆分成一個個單字 保存到數組 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);        // 判斷拼接后的字符串是否超過預設的寬度        if (($testbox[2] > $width) && ($content !== "")) {          $content .= "/n";        }        $content .= $l;      }    }    return $content;  }   /**   * 實現寫入圖片   * @param $text 要寫入的文字   * @param $flag 是否直接輸出到瀏覽器,默認是   */  public function writeWordsToImg($text,$flag=true){    if(empty($this->config)){      return 'must be config';    }    //獲取圖片大小    $img_pathWH = getimagesize($this->config['file']);    //打開指定的圖片文件    $im = imagecreatefrompng($this->config['file']);    #設置水印字體顏色    $color = imagecolorallocatealpha($im,0, 0, 255, 75);//藍色    $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']); //自動換行處理      $words_text[1] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[1], $this->config['width']); //自動換行處理      $words_text[2] = $this->wordWrap($this->config['size'], $this->config['angle'], $this->config['fontfile'], $words_text[2], $this->config['width']); //自動換行處理      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']); //自動換行處理    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);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 无极县| 陈巴尔虎旗| 双辽市| 龙岩市| 大渡口区| 曲阜市| 永仁县| 莒南县| 中山市| 渭源县| 通渭县| 儋州市| 石城县| 霍林郭勒市| 古田县| 洪洞县| 灌阳县| 穆棱市| 新津县| 新竹市| 孟连| 农安县| 民县| 谷城县| 布拖县| 托里县| 潼关县| 佛冈县| 丹东市| 和顺县| 汕尾市| 峡江县| 冀州市| 柳江县| 吐鲁番市| 吉水县| 南平市| 霍邱县| 五河县| 吴堡县| 颍上县|