ThinkPHP使用Imagick給圖片加文字


在PHP處理文字的過程中,imagettftext是一個給圖片添加水印的方式,可以動態指定字體、文字、大小,用起來比較方便;

在ThinkPHP中,可以方便地使用Imagick來完成相應的效果ImagickDraw.annotateImage,但是二者共同的問題是文字不能自動根據寬度換行;

 

解決的辦法就是計算文字的寬度,利用imagettfbox計算文字寬度,並且重構字符串在一些地方加入\n符號(http://php.net/manual/en/function.imagettfbbox.php#68518)

於是經過在網上的搜尋,整合出以下代碼:

    /**
     * 返回一個字符的數組
     *
     * @param $str      文字
     * @param $charset  字符編碼
     * @return $match   返回一個字符的數組
     */
    function charArray($str,$charset="utf-8"){
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);

        return $match;

    }



    /**
     * 返回一個字符串在圖片中所占的寬度
     * @param $fontsize  字體大小
     * @param $fontangle 角度
     * @param $ttfpath   字體文件
     * @param $char      字符
     * @return $width
     */
    function charwidth($fontsize,$fontangle,$ttfpath,$char){
        $box = imagettfbbox($fontsize,$fontangle,$ttfpath,$char);
        $width = max($box[2], $box[4]) - min($box[0], $box[6]);

        return $width;
    }

    /**
     * 根據預設寬度讓文字自動換行
     * @param $fontsize   字體大小
     * @param $ttfpath    字體名稱
     * @param $str    字符串
     * @param $width    預設寬度
     * @param $fontangle  角度
     * @param $charset    編碼
     * @return $_string  字符串
     */
    function autowrap($fontsize,$ttfpath,$str,$width,$fontangle=0,$charset='utf-8'){
        $_string = "";
        $_width  = 0;
        $temp    = $this->chararray($str);
        foreach ($temp[0] as $v){
            $w = $this->charwidth($fontsize,$fontangle,$ttfpath,$v);
            $_width += intval($w);
            if (($_width > $width) && ($v !== "")){
                $_string .= PHP_EOL;
                $_width = 0;
            }
            $_string .= $v;
        }

        return $_string;

    }
View Code

 

我使用的是ThinkPHP,所以會有一些特殊的已經定義過的符號;

NOTE!!!

非常重要的一點,PHP里使用的文字單位不是像素,而是磅,所以會造成位置的偏差;

這個問題還沒有找到好的解決辦法,網上似乎也有二者的轉換方法,但是需要結合圖片分辨率吧,我記得我用了效果也不是很好;

/**
 * 實現php后台里,指定文字大小時,單位轉換
 * @param $px
 * @return int
 */
function px2dp($px){
    $map=array(
        0,4,
        5,
        7,
        8,
        9,
        10,
        11,
        12,
        14,
        15,
        16,
        17,
        18,
        19,
        21,
        22,
        23,
        25,
        26,
        27,
        28,
        29,
        30,
        32,
        33,
        34,
        35,
        36,
        38,
        39,
        40,
        41,
        43,
        44,
        46,
        47,
        48,
        48,
        50,
        51);

    //遍歷數組
    for($i=1;$i<count($map);$i++){
        //恰好有匹配的磅值
        if($map[$i]==$px){
            return $i;
        }

        //如果當前像素值恰好在兩個磅值之間
        if($map[$i]<$px && $map[$i+1]>$px){
            return $i+0.5;
        }
    }
}

//附錄-->磅轉像素表
/*
 * 1磅==>4像素, PPI=288
2磅==>5像素, PPI=180
3磅==>7像素, PPI=168
4磅==>8像素, PPI=144
5磅==>9像素, PPI=129.6
6磅==>10像素, PPI=120
7磅==>11像素, PPI=113.14285714286
8磅==>12像素, PPI=108
9磅==>14像素, PPI=112
10磅==>15像素, PPI=108
11磅==>16像素, PPI=104.72727272727
12磅==>17像素, PPI=102
13磅==>18像素, PPI=99.692307692308
14磅==>19像素, PPI=97.714285714286
15磅==>21像素, PPI=100.8
16磅==>22像素, PPI=99
17磅==>23像素, PPI=97.411764705882
18磅==>25像素, PPI=100
19磅==>26像素, PPI=98.526315789474
20磅==>27像素, PPI=97.2
21磅==>28像素, PPI=96
22磅==>29像素, PPI=94.909090909091
23磅==>30像素, PPI=93.913043478261
24磅==>32像素, PPI=96
25磅==>33像素, PPI=95.04
26磅==>34像素, PPI=94.153846153846
27磅==>35像素, PPI=93.333333333333
28磅==>36像素, PPI=92.571428571429
29磅==>38像素, PPI=94.344827586207
30磅==>39像素, PPI=93.6
31磅==>40像素, PPI=92.903225806452
32磅==>41像素, PPI=92.25
33磅==>43像素, PPI=93.818181818182
34磅==>44像素, PPI=93.176470588235
35磅==>46像素, PPI=94.628571428571
36磅==>47像素, PPI=94
37磅==>48像素, PPI=93.405405405405
38磅==>48像素, PPI=90.947368421053
39磅==>50像素, PPI=92.307692307692
40磅==>51像素, PPI=91.8
41磅==>52像素, PPI=91.317073170732
42磅==>53像素, PPI=90.857142857143
43磅==>55像素, PPI=92.093023255814
44磅==>56像素, PPI=91.636363636364
45磅==>57像素, PPI=91.2
46磅==>58像素, PPI=90.782608695652
47磅==>60像素, PPI=91.914893617021
48磅==>62像素, PPI=93
49磅==>63像素, PPI=92.571428571429
50磅==>63像素, PPI=90.72
51磅==>64像素, PPI=90.352941176471
52磅==>67像素, PPI=92.769230769231
53磅==>68像素, PPI=92.377358490566
54磅==>69像素, PPI=92
55磅==>70像素, PPI=91.636363636364
56磅==>71像素, PPI=91.285714285714
57磅==>72像素, PPI=90.947368421053
58磅==>74像素, PPI=91.862068965517
59磅==>75像素, PPI=91.525423728814
60磅==>76像素, PPI=91.2
61磅==>77像素, PPI=90.885245901639
62磅==>78像素, PPI=90.58064516129
63磅==>79像素, PPI=90.285714285714
64磅==>81像素, PPI=91.125
65磅==>83像素, PPI=91.938461538462
66磅==>84像素, PPI=91.636363636364
67磅==>85像素, PPI=91.34328358209
68磅==>86像素, PPI=91.058823529412
69磅==>86像素, PPI=89.739130434783
70磅==>88像素, PPI=90.514285714286
71磅==>90像素, PPI=91.267605633803
72磅==>91像素, PPI=91
73磅==>92像素, PPI=90.739726027397
74磅==>93像素, PPI=90.486486486486
 * */
View Code

並且我在項目中使用的是Imagick方式生成圖片,這個庫也是公認的PHP下最好的圖片處理庫,完整代碼放在最后;

NOTE2!!!

ThinkPHP 3.3.2(我用的版本)中自帶的Imagick.class.php 在初始化圖片的時候,調用的API有問題,會造成整個圖片的大小改變:

在60行左右,需要做如下改變,才能對圖片正常添加水印

        //設置圖像信息
        $this->info = array(
            'width'  => $info[0],
            'height' => $info[1],
            'type'   => image_type_to_extension($info[2], false),
            'mime'   => $info['mime'],
            //下面自帶的方法獲取長寬會窄一點,造成gif縮放錯誤
//            'width'  => $this->img->getImageWidth(),
//            'height' => $this->img->getImageHeight(),
//            'type'   => strtolower($this->img->getImageFormat()),
//            'mime'   => $this->img->getImageMimeType(),
        );
View Code

對於生成圖片,我封裝了ImagickHelper.class.php,分享如下:

<?php
/**
 * Created by PhpStorm.
 * User: m1881
 * Date: 2016/12/3
 * Time: 14:59
 */

namespace Home\Controller;

class ImagickHelper
{
    private $image = null;
    private $type = null;

    // 構造函數
    public function __construct(){}

    // 析構函數
    public function __destruct()
    {
        if($this->image!==null) $this->image->destroy();
    }

    // 載入圖像
    public function open($path)
    {
        $this->image = new \Imagick( $path );
        if($this->image)
        {
            $this->type = strtolower($this->image->getImageFormat());
        }
        return $this->image;
    }

    public function drawRect($draw){
        $this->image->drawImage($draw);
    }


    public function crop($x=0, $y=0, $width=null, $height=null)
    {
        if($width==null) $width = $this->image->getImageWidth()-$x;
        if($height==null) $height = $this->image->getImageHeight()-$y;
        if($width<=0 || $height<=0) return;

        if($this->type=='gif')
        {
            $image = $this->image;
            $canvas = new \Imagick();

            $images = $image->coalesceImages();
            foreach($images as $frame){
                $img = new \Imagick();
                $img->readImageBlob($frame);
                $img->cropImage($width, $height, $x, $y);

                $canvas->addImage( $img );
                $canvas->setImageDelay( $img->getImageDelay() );
                $canvas->setImagePage($width, $height, 0, 0);
            }

            $image->destroy();
            $this->image = $canvas;
        }
        else
        {
            $this->image->cropImage($width, $height, $x, $y);
        }
    }

    /*
    * 更改圖像大小
    $fit: 適應大小方式
    'force': 把圖片強制變形成 $width X $height 大小
    'scale': 按比例在安全框 $width X $height 內縮放圖片, 輸出縮放后圖像大小 不完全等於 $width X $height
    'scale_fill': 按比例在安全框 $width X $height 內縮放圖片,安全框內沒有像素的地方填充色, 使用此參數時可設置背景填充色 $bg_color = array(255,255,255)(紅,綠,藍, 透明度) 透明度(0不透明-127完全透明))
    其它: 智能模能 縮放圖像並載取圖像的中間部分 $width X $height 像素大小
    $fit = 'force','scale','scale_fill' 時: 輸出完整圖像
    $fit = 圖像方位值 時, 輸出指定位置部分圖像
    字母與圖像的對應關系如下:

    north_west   north   north_east

    west         center        east

    south_west   south   south_east
    */
    public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255,255,255,0) )
    {

        switch($fit)
        {
            case 'force':
                if($this->type=='gif')
                {
                    $image = $this->image;
                    $canvas = new \Imagick();

                    $images = $image->coalesceImages();
                    foreach($images as $frame){
                        $img = new \Imagick();
                        $img->readImageBlob($frame);
                        $img->thumbnailImage( $width, $height, false );

                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                    }
                    $image->destroy();
                    $this->image = $canvas;
                }
                else
                {
                    $this->image->thumbnailImage( $width, $height, false );
                }
                break;
            case 'scale':
                if($this->type=='gif')
                {
                    $image = $this->image;
                    $images = $image->coalesceImages();
                    $canvas = new \Imagick();
                    foreach($images as $frame){
                        $img = new \Imagick();
                        $img->readImageBlob($frame);
                        $img->thumbnailImage( $width, $height, true );

                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                    }
                    $image->destroy();
                    $this->image = $canvas;
                }
                else
                {
                    $this->image->thumbnailImage( $width, $height, true );
                }
                break;
            case 'scale_fill':
                $size = $this->image->getImagePage();
                $src_width = $size['width'];
                $src_height = $size['height'];

                $x = 0;
                $y = 0;

                $dst_width = $width;
                $dst_height = $height;

                if($src_width*$height > $src_height*$width)
                {
                    $dst_height = intval($width*$src_height/$src_width);
                    $y = intval( ($height-$dst_height)/2 );
                }
                else
                {
                    $dst_width = intval($height*$src_width/$src_height);
                    $x = intval( ($width-$dst_width)/2 );
                }

                $image = $this->image;
                $canvas = new \Imagick();

                $color = 'rgba('.$fill_color[0].','.$fill_color[1].','.$fill_color[2].','.$fill_color[3].')';
                if($this->type=='gif')
                {
                    $images = $image->coalesceImages();
                    foreach($images as $frame)
                    {
                        $frame->thumbnailImage( $width, $height, true );

                        $draw = new \ImagickDraw();
                        $draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);

                        $img = new \Imagick();
                        $img->newImage($width, $height, $color, 'gif');
                        $img->drawImage($draw);

                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                        $canvas->setImagePage($width, $height, 0, 0);
                    }
                }
                else
                {
                    $image->thumbnailImage( $width, $height, true );

                    $draw = new \ImagickDraw();
                    $draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);

                    $canvas->newImage($width, $height, $color, $this->get_type() );
                    $canvas->drawImage($draw);
                    $canvas->setImagePage($width, $height, 0, 0);
                }
                $image->destroy();
                $this->image = $canvas;
                break;
            default:
                $size = $this->image->getImagePage();
                $src_width = $size['width'];
                $src_height = $size['height'];

                $crop_x = 0;
                $crop_y = 0;

                $crop_w = $src_width;
                $crop_h = $src_height;

                if($src_width*$height > $src_height*$width)
                {
                    $crop_w = intval($src_height*$width/$height);
                }
                else
                {
                    $crop_h = intval($src_width*$height/$width);
                }

                switch($fit)
                {
                    case 'north_west':
                        $crop_x = 0;
                        $crop_y = 0;
                        break;
                    case 'north':
                        $crop_x = intval( ($src_width-$crop_w)/2 );
                        $crop_y = 0;
                        break;
                    case 'north_east':
                        $crop_x = $src_width-$crop_w;
                        $crop_y = 0;
                        break;
                    case 'west':
                        $crop_x = 0;
                        $crop_y = intval( ($src_height-$crop_h)/2 );
                        break;
                    case 'center':
                        $crop_x = intval( ($src_width-$crop_w)/2 );
                        $crop_y = intval( ($src_height-$crop_h)/2 );
                        break;
                    case 'east':
                        $crop_x = $src_width-$crop_w;
                        $crop_y = intval( ($src_height-$crop_h)/2 );
                        break;
                    case 'south_west':
                        $crop_x = 0;
                        $crop_y = $src_height-$crop_h;
                        break;
                    case 'south':
                        $crop_x = intval( ($src_width-$crop_w)/2 );
                        $crop_y = $src_height-$crop_h;
                        break;
                    case 'south_east':
                        $crop_x = $src_width-$crop_w;
                        $crop_y = $src_height-$crop_h;
                        break;
                    default:
                        $crop_x = intval( ($src_width-$crop_w)/2 );
                        $crop_y = intval( ($src_height-$crop_h)/2 );
                }

                $image = $this->image;
                $canvas = new \Imagick();

                if($this->type=='gif')
                {
                    $images = $image->coalesceImages();
                    foreach($images as $frame){
                        $img = new \Imagick();
                        $img->readImageBlob($frame);
                        $img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
                        $img->thumbnailImage( $width, $height, true );

                        $canvas->addImage( $img );
                        $canvas->setImageDelay( $img->getImageDelay() );
                        $canvas->setImagePage($width, $height, 0, 0);
                    }
                }
                else
                {
                    $image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
                    $image->thumbnailImage( $width, $height, true );
                    $canvas->addImage( $image );
                    $canvas->setImagePage($width, $height, 0, 0);
                }
                $image->destroy();
                $this->image = $canvas;
        }

    }

    // 添加水印圖片
    public function add_watermark($path, $x = 0, $y = 0)
    {
        $watermark = new \Imagick($path);
        $draw = new \ImagickDraw();
        $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);

        if($this->type=='gif')
        {
            $image = $this->image;
            $canvas = new \Imagick();
            $images = $image->coalesceImages();
            foreach($image as $frame)
            {
                $img = new \Imagick();
                $img->readImageBlob($frame);
                $img->drawImage($draw);

                $canvas->addImage( $img );
                $canvas->setImageDelay( $img->getImageDelay() );
            }
            $image->destroy();
            $this->image = $canvas;
        }
        else
        {
            $this->image->drawImage($draw);
        }
    }

    // 添加水印文字
    public function add_text($text, $limit_width, $x = 0 , $y = 0, $angle=0, $style=array())
    {
        //$width = $this->image->getImageWidth()-$x;
        //$height = $this->image->getImageHeight()-$y;
        //if($width<=0 || $height<=0) return;
        $draw = new \ImagickDraw();
//        $draw->setgravity(\Imagick::GRAVITY_SOUTHWEST);
        if(isset($style['font'])) $draw->setFont($style['font']);
        if(isset($style['font_size'])) $draw->setFontSize($style['font_size']);  //字體大小
        if(isset($style['fill_color'])) $draw->setFillColor($style['fill_color']); // 字體顏色
//        if(isset($style['under_color'])) $draw->setTextUnderColor($style['under_color']);

        //使文字換行
        $text=$this->autowrap($style['font_size'],$style['font'],$text,$limit_width);

        if($this->type=='gif')
        {
            foreach($this->image as $frame)
            {
                $frame->annotateImage($draw, $x, $y, $angle, $text);
            }
        }
        else
        {
            $this->image->annotateImage($draw, $x, $y, $angle, $text);
        }
    }


    // 保存到指定路徑
    public function save_to( $path )
    {
        if($this->type=='gif')
        {
            $this->image->writeImages($path, true);
        }
        else
        {
            $this->image->writeImage($path);
        }
    }

    // 輸出圖像
    public function output($header = true)
    {
        if($header) header('Content-type: '.$this->type);
        echo $this->image->getImagesBlob();
    }


    public function get_width()
    {
        $size = $this->image->getImagePage();
        return $size['width'];
    }

    public function get_height()
    {
        $size = $this->image->getImagePage();
        return $size['height'];
    }

    // 設置圖像類型, 默認與源類型一致
    public function set_type( $type='png' )
    {
        $this->type = $type;
        $this->image->setImageFormat( $type );
    }

    // 獲取源圖像類型
    public function get_type()
    {
        return $this->type;
    }


    // 當前對象是否為圖片
    public function is_image()
    {
        if( $this->image )
            return true;
        else
            return false;
    }



    public function thumbnail($width = 100, $height = 100, $fit = true){
        $this->image->thumbnailImage( $width, $height, $fit );
    } // 生成縮略圖 $fit為真時將保持比例並在安全框 $width X $height 內生成縮略圖片

    /*
    添加一個邊框
    $width: 左右邊框寬度
    $height: 上下邊框寬度
    $color: 顏色: RGB 顏色 'rgb(255,0,0)' 或 16進制顏色 '#FF0000' 或顏色單詞 'white'/'red'...
    */
    public function border($width, $height, $color='rgb(220, 220, 220)')
    {
        $color=new \ImagickPixel();
        $color->setColor($color);
        $this->image->borderImage($color, $width, $height);
    }

    public function blur($radius, $sigma){
        $this->image->blurImage($radius, $sigma);
    } // 模糊
    public function gaussian_blur($radius, $sigma){
        $this->image->gaussianBlurImage($radius, $sigma);
    } // 高斯模糊
    public function motion_blur($radius, $sigma, $angle){
        $this->image->motionBlurImage($radius, $sigma, $angle);
    } // 運動模糊
    public function radial_blur($radius){
        $this->image->radialBlurImage($radius);
    } // 徑向模糊

    public function add_noise($type=null){
        $this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);
    } // 添加噪點

    public function level($black_point, $gamma, $white_point){$this->image->levelImage($black_point, $gamma, $white_point);} // 調整色階
    public function modulate($brightness, $saturation, $hue){$this->image->modulateImage($brightness, $saturation, $hue);} // 調整亮度、飽和度、色調

    public function charcoal($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描
    public function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油畫效果

    public function flop(){$this->image->flopImage();} // 水平翻轉
    public function flip(){$this->image->flipImage();} // 垂直翻轉


    /**
     * 返回一個字符的數組
     *
     * @param $str      文字
     * @param $charset  字符編碼
     * @return $match   返回一個字符的數組
     */
    function charArray($str,$charset="utf-8"){
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);

        return $match;

    }



    /**
     * 返回一個字符串在圖片中所占的寬度
     * @param $fontsize  字體大小
     * @param $fontangle 角度
     * @param $ttfpath   字體文件
     * @param $char      字符
     * @return $width
     */
    function charwidth($fontsize,$fontangle,$ttfpath,$char){
        $box = imagettfbbox($fontsize,$fontangle,$ttfpath,$char);
        $width = max($box[2], $box[4]) - min($box[0], $box[6]);

        return $width;
    }

    /**
     * 根據預設寬度讓文字自動換行
     * @param $fontsize   字體大小
     * @param $ttfpath    字體名稱
     * @param $str    字符串
     * @param $width    預設寬度
     * @param $fontangle  角度
     * @param $charset    編碼
     * @return $_string  字符串
     */
    function autowrap($fontsize,$ttfpath,$str,$width,$fontangle=0,$charset='utf-8'){
        $_string = "";
        $_width  = 0;
        $temp    = $this->chararray($str);
        foreach ($temp[0] as $v){
            $w = $this->charwidth($fontsize,$fontangle,$ttfpath,$v);
            $_width += intval($w);
            if (($_width > $width) && ($v !== "")){
                $_string .= PHP_EOL;
                $_width = 0;
            }
            $_string .= $v;
        }

        return $_string;

    }
}
View Code

然后調用方式如下:

           try{
                $image=new ImagickHelper();
                $image->open($imgUrl);

                for($i=0;$i<count($width);$i++){
                    $this->addTexttoImg($image,
                        $width[$i],$left[$i],$top[$i]+$font_size[$i],
                        $text[$i],$color[$i],
                        $font_family[$i],$font_size[$i]);

                }

                //加水印之后地址
                $image->save_to($tumbUrl);
            }catch (Exception $e){
                var_dump($e."Open save error");

                $this->ajaxReturn(array(
                    'info' => '制作失敗',
                    'code' => 0
                ));
            }
View Code

於是,以此方式可以完成ThinkPHP下Imagick給圖片加文字的任務~

 


免責聲明!

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



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