濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP實現(xiàn)文字寫入圖片功能

PHP實現(xiàn)文字寫入圖片功能

熱門標(biāo)簽:臨沂智能電銷機(jī)器人軟件 浙江外呼電話系統(tǒng)軟件 銀川ai電話機(jī)器人 十堰ai電話機(jī)器人效果怎么樣 芒果電銷機(jī)器人 電梯外呼線路板維修視頻 安陽自動外呼系統(tǒng)價格是多少 上海公司外呼系統(tǒng)線路 地圖標(biāo)注風(fēng)向標(biāo)

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

/**
 * PHP實現(xiàn)文字寫入圖片
 */
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'] 開始寫入時的橫坐標(biāo)
   * @param $config['y'] 開始寫入時的縱坐標(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實現(xiàn)圖片上寫入實現(xiàn)文字自動換行
   * @param $fontsize 字體大小
   * @param $angle 角度
   * @param $font 字體路徑
   * @param $string 要寫在圖片上的文字
   * @param $width 預(yù)先設(shè)置圖片上文字的寬度
   * @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);
        //判斷拼接后的字符串是否超過預(yù)設(shè)的寬度
        if(($testbox[2] > $width)) {
          $content .= "\n";
        }
        $content .= $value." ";
      }
    }else{
      //將字符串拆分成一個個單字 保存到數(shù)組 letter 中
      for ($i=0;$imb_strlen($string);$i++) {
        $letter[] = mb_substr($string, $i, 1);
      }
      foreach ($letter as $l) {
        $teststr = $content." ".$l;
        $testbox = imagettfbbox($fontsize, $angle, $font, $teststr);
        // 判斷拼接后的字符串是否超過預(yù)設(shè)的寬度
        if (($testbox[2] > $width)  ($content !== "")) {
          $content .= "\n";
        }
        $content .= $l;
      }
    }
    return $content;
  }
 
  /**
   * 實現(xiàn)寫入圖片
   * @param $text 要寫入的文字
   * @param $flag 是否直接輸出到瀏覽器,默認(rèn)是
   */
  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']);
    #設(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']); //自動換行處理
      $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 Kangbr/>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);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP獲取photoshop寫入圖片文字信息的方法

標(biāo)簽:武威 遂寧 徐州 吐魯番 寧夏 荊門 遵義 常州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實現(xiàn)文字寫入圖片功能》,本文關(guān)鍵詞  PHP,實現(xiàn),文字,寫入,圖片,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP實現(xiàn)文字寫入圖片功能》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP實現(xiàn)文字寫入圖片功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    大悟县| 博爱县| 武城县| 庆安县| 淮滨县| 佛冈县| 上杭县| 苏州市| 泽州县| 桦甸市| 化德县| 新平| 翼城县| 庆云县| 沙雅县| 石台县| 河津市| 鹤庆县| 南投市| 太和县| 巨野县| 墨脱县| 策勒县| 积石山| 南和县| 张家口市| 苏尼特左旗| 南安市| 曲阳县| 贵阳市| 固镇县| 宁蒗| 新乡市| 庆安县| 罗定市| 高邑县| 鲁山县| 香港 | 肥东县| 麻栗坡县| 察哈|