亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2019年02月18日 12:10:11   作者:葉少翔  
這篇文章主要為大家詳細介紹了PHP實現(xiàn)文字寫入圖片功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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'] 開始寫入時的橫坐標
   * @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實現(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;$i<mb_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 是否直接輸出到瀏覽器,默認是
   */
  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);//藍色
    $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);

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

相關(guān)文章

  • 關(guān)于url地址傳參數(shù)時字符串有回車造成頁面腳本賦值失敗的解決方法

    關(guān)于url地址傳參數(shù)時字符串有回車造成頁面腳本賦值失敗的解決方法

    本篇文章是對關(guān)于url地址傳參數(shù)時字符串有回車造成頁面腳本賦值失敗的解決方法進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 學(xué)習(xí)php設(shè)計模式 php實現(xiàn)策略模式(strategy)

    學(xué)習(xí)php設(shè)計模式 php實現(xiàn)策略模式(strategy)

    這篇文章主要介紹了php設(shè)計模式中的適配器模式,使用php實現(xiàn)適配器模式,感興趣的小伙伴們可以參考一下
    2015-12-12
  • PHP后期靜態(tài)綁定實例淺析

    PHP后期靜態(tài)綁定實例淺析

    這篇文章主要介紹了PHP后期靜態(tài)綁定,結(jié)合實例形式分析了php后期靜態(tài)綁定相關(guān)概念、原理、使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2018-12-12
  • PHP代碼加密和擴展解密實戰(zhàn)

    PHP代碼加密和擴展解密實戰(zhàn)

    這篇文章主要介紹了PHP代碼加密和擴展解密實戰(zhàn),對加密解密感興趣的同學(xué),可以參考下
    2021-04-04
  • php下保存遠程圖片到本地的辦法

    php下保存遠程圖片到本地的辦法

    有些時候我們看到網(wǎng)絡(luò)上有一些漂亮的圖片,而又不想手動去保存下來,尤其是大量的這樣的圖片,那這個時候我們需要寫一段程序來幫助我們完成這個工作。
    2010-08-08
  • PHP利用ffmpeg獲取音頻、視頻的詳細信息

    PHP利用ffmpeg獲取音頻、視頻的詳細信息

    這篇文章給大家介紹了使用PHP利用ffmpeg獲取音頻、視頻的詳細信息,音視頻總時長、碼率、視頻分辨率、音頻編碼、音頻采樣頻率、實際播放時間、文件大小,文中給出詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下
    2024-01-01
  • PHP中iconv函數(shù)知識匯總

    PHP中iconv函數(shù)知識匯總

    iconv函數(shù)庫能夠完成各種字符集間的轉(zhuǎn)換,是php編程中不可缺少的基礎(chǔ)函數(shù)庫。本文內(nèi)容是參考了網(wǎng)上的其他資源,然后結(jié)合自己的實踐,有需要的小伙伴可以參考下。
    2015-07-07
  • php計算多維數(shù)組中所有值總和的方法

    php計算多維數(shù)組中所有值總和的方法

    這篇文章主要介紹了php計算多維數(shù)組中所有值總和的方法,涉及php針對多維數(shù)組遞歸調(diào)用的技巧
    2015-06-06
  • 從手冊去理解分析PHP session機制

    從手冊去理解分析PHP session機制

    session_start()是session機制的開始,它有一定概率開啟垃圾回收,因為session是存放在文件中,PHP自身的垃圾回收是無效的,SESSION的回收是要刪文件的,這個概率是根據(jù)php.ini的配置決定的
    2011-07-07
  • php基于協(xié)程實現(xiàn)異步的方法分析

    php基于協(xié)程實現(xiàn)異步的方法分析

    這篇文章主要介紹了php基于協(xié)程實現(xiàn)異步的方法,結(jié)合實例形式分析了php基于協(xié)程調(diào)度的異步操作實現(xiàn)方法,需要的朋友可以參考下
    2019-07-07

最新評論