首页 > 代码库 > ThinkPHP使用Imagick给图片加文字

ThinkPHP使用Imagick给图片加文字

在PHP处理文字的过程中,imagettftext是一个给图片添加水印的方式,可以动态指定字体、文字、大小,用起来比较方便;

在ThinkPHP中,可以方便地使用Imagick来完成相应的效果ImagickDraw.annotateImage,但是二者共同的问题是文字不能自动根据宽度换行;

 

解决的办法就是计算文字的宽度,并且重构字符串在一些地方加入\n符号

于是经过在网上的搜寻,整合出以下代码:

技术分享
    /**
     * 返回一个字符的数组
     *
     * @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给图片加文字的任务~

 

ThinkPHP使用Imagick给图片加文字