PHP使用imagecreatefromstring(), imagecolorallocate() imagettftext() 給圖片添加文字描述


轉自:https://www.jianshu.com/p/3145b9427e35

 

首先了解這三個php圖片處理函數的作用及用法

1、imagecreatefromstring() -- 從字符串中的圖像流新建一圖像
說明 : resource imagecreatefromstring ( string image )
imagecreatefromstring() 返回一個圖像標識符,其表達了從給定字符串得來的圖像。圖像格式將自動檢測,只要 PHP 支持:JPEG,PNG,GIF,WBMP 和 GD2。
返回值: 成功則返回圖像資源,如果圖像格式不支持,數據不是認可的格式,或者圖像已損壞則返回 FALSE。
示例:


  imagecreatefromstring(file_get_contents('./img.jpg')); //從當前目錄下img.jpg為准新建一圖像

2、imagecolorallocate()   int imagecolorallocate(resource image,intred,int green,intblue) //為一幅圖分配顏色
示例:

$white = imagecolorallocate($im,255,255,255);//返回由十進制整數設置為白色的標識符
$black = imagecolorallocate($im,0,0,0);//返回由十進制參數設置為黑色的標識符

3、imagettftext()
說明:array imagettftext ( resource image , floatsize , float angle , intx , int y , intcolor , string fontfile , stringtext )
參數:


image 由圖象創建函數(例如imagecreatetruecolor())返回的圖象資源。
size 字體的尺寸。根據 GD 的版本,為像素尺寸(GD1)或點(磅)尺寸(GD2)。
angle 角度制表示的角度,0 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。
x 由 xy 所表示的坐標定義了第一個字符的基本點(大概是字符的左下角)。這和 imagestring() 不同,其 xy 定義了第一個字符的左上角。例如 "top left" 為 0, 0。
y Y 坐標。它設定了字體基線的位置,不是字符的最底端。
color 顏色索引。使用負的顏色索引值具有關閉防鋸齒的效果。見 imagecolorallocate()
fontfile 是想要使用的 TrueType 字體的路徑。

根據 PHP 所使用的 GD 庫的不同,當 fontfile 沒有以 / 開頭時則 .ttf 將被加到文件名之后並且會在庫定義字體路徑中嘗試搜索該文件名。

當使用的 GD 庫版本低於 2.0.18 時,一個空格字符 而不是分號將被用來作為不同字體文件的“路徑分隔符”。不小心使用了此特性將會導致一條警告信息:Warning: Could not find/open font。對受影響的版本來說唯一解決方案就是將字體移動到不包含空格的路徑中去。

很多情況下字體都放在腳本的同一個目錄下。下面的小技巧可以減輕包含的問題。

<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

text :

UTF-8 編碼的文本字符串。

可以包含十進制數字化字符表示(形式為:€)來訪問字體中超過位置 127 的字符。UTF-8 編碼的字符串可以直接傳遞。

命名實體,比如 © 是不支持的。可以考慮使用 html_entity_decode() 來解碼命名實體為 UTF-8 字符。 (自 PHP 5.0.0 開始 html_entity_decode() 開始支持)

如果字符串中使用的某個字符不被字體支持,一個空心矩形將替換該字符。

返回值: 返回一個含有 8 個單元的數組表示了文本外框的四個角,順序為坐下角,右下角,右上角,左上角。這些點是相對於文本的而和角度無關,因此“左上角”指的是以水平方向看文字時其左上角。

以上就是php向圖片添加文字必須三個函數,下面看代碼:

原圖


 

 方法

/**
     * 給圖片添加文字
     * 
     * @param string $strImg 圖片文件名
     * @param string $strText 字符串內容
     * @param array $arrOpt ['r','g','b','x','y','fontsize','angle','savepath','ttf']
     */
public static function addText($strImg, $strText, $arrOpt = array()){
        
        $intR = isset($arrOpt['r']) ? $arrOpt['r'] : 255;
        $intG = isset($arrOpt['g']) ? $arrOpt['g'] : 255;
        $intB = isset($arrOpt['b']) ? $arrOpt['b'] : 255;
        $intX = isset($arrOpt['x']) ? $arrOpt['x'] : 24;
        $intY = isset($arrOpt['y']) ? $arrOpt['y'] : 24;
        $intSize = isset($arrOpt['fontsize']) ? $arrOpt['fontsize'] : 14;
        $intAngle = isset($arrOpt['angle']) ? $arrOpt['angle'] : 0;
        $strSave = isset($arrOpt['savepath']) ? $arrOpt['savepath'] : false;
        $strFont = isset($arrOpt['ttf']) ? $arrOpt['ttf'] : './simsun.ttf';
        
        $im = imagecreatefromstring(file_get_contents($strImg));

        $color = imagecolorallocate($im, $intR, $intG, $intB);
        imagettftext($im, $intSize, $intAngle, $intX, $intY, $color, $strFont, $strText);
        
        if (empty($strSave)) {
            header("Content-type: image/jpeg");
            imagejpeg($im);
        } else {
            imagejpeg($im, $strSave);
        }
        
        imagedestroy($im);
    }
    

效果

 

 改變字體顏色大小再來

 

 

還可以添加垂直文字示例:

    /**
     * 給圖片添加垂直文字
     *
     * @param string $strImg 圖片文件名
     * @param string $strText 字符串內容
     * @param array $arrOpt ['r','g','b','x','y','fontsize','savepath','ttf']
     */
    public static function addVerticalText($strImg, $strText, $arrOpt = array()){
        
        $intR = isset($arrOpt['r']) ? $arrOpt['r'] : 255;
        $intG = isset($arrOpt['g']) ? $arrOpt['g'] : 255;
        $intB = isset($arrOpt['b']) ? $arrOpt['b'] : 255;
        $intX = isset($arrOpt['x']) ? $arrOpt['x'] : 24;
        $intY = isset($arrOpt['y']) ? $arrOpt['y'] : 24;
        $intSize = isset($arrOpt['fontsize']) ? $arrOpt['fontsize'] : 14;
        $intAngle = 0;
        $strSave = isset($arrOpt['savepath']) ? $arrOpt['savepath'] : false;
        $strFont = isset($arrOpt['ttf']) ? $arrOpt['ttf'] : './simsun.ttf';
        
        $im = imagecreatefromstring(file_get_contents($strImg));
        $color = imagecolorallocate($im, $intR, $intG, $intB);
        
        preg_match_all('/./u', $strText, $m);
        foreach ($m[0] as $text) {
            if (empty($text)) {
                continue;
            }
            imagettftext($im, $intSize, $intAngle, $intX, $intY, $color, $strFont, $text);
            $intY += intval($intSize * 1.5);
        }
        
        if (empty($strSave)) {
            header("Content-type: image/jpeg");
            imagejpeg($im);
        } else {
            imagejpeg($im, $strSave);
        }
        
        imagedestroy($im);
    }

效果:

 

 

作者:北漂這八年
鏈接:https://www.jianshu.com/p/3145b9427e35
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM