>백엔드 개발 >PHP 튜토리얼 >thinkphp 프레임워크 GD 라이브러리를 사용하여 사진에 텍스트를 쓰는 방법(코드 예)

thinkphp 프레임워크 GD 라이브러리를 사용하여 사진에 텍스트를 쓰는 방법(코드 예)

little bottle
little bottle앞으로
2019-04-23 11:58:283123검색

이 기사의 주요 내용은 GD 라이브러리를 사용하여 사진에 텍스트를 쓰는 것에 관한 것입니다. 관심 있는 친구들이 이에 대해 배울 수 있기를 바랍니다.

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 */
namespace Home\Event;

use \Think\Image;
use \Think\Upload;
class ImgEvent
{

    private $THINK_IMAGE = null;
    private $THINK_UPLOAD = null;

    public function __construct()
    {
        $this->THINK_IMAGE =  new Image();
        $this->THINK_UPLOAD =  new Upload();
    }

    /*
     * 保存base64文件
     * $img    string    base64类型的文件
     * $type   string    保存的文件类型
     *      app_user_head_img   用户头像
     *
     *
     */
    public function saveImg_base64($img = null , $type = null)
    {
        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
//        dump($imgConfig_savePath[$type]);
//        dump($imgConfig_size);

        if (preg_match(&#39;/^(data:\s*image\/(\w+);base64,)/&#39;, $img, $result) && $imgConfig_savePath[$type])
        {

            $img_ext                = $result[2]; //图片后缀
            $img_header             = $result[1];//图片头信息
            $new_file_name          = date(&#39;Ymd&#39;).&#39;/&#39;.uniqid().&#39;.&#39;.$img_ext;
            $origin_img_path        = &#39;&#39;;//原图的保存路径
            $origin_img_save_flag   = true;//
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date(&#39;Ymd&#39;)))
                {
                    mkdir($v.date(&#39;Ymd&#39;),0777,true);
                }

                if ($k == &#39;origin&#39;)
                {
                    //先保存一份原图,然后其他尺寸的保存直接调用原图路径origin_img_path.
                    $origin_res = file_put_contents($v.$new_file_name, base64_decode(str_replace($img_header, &#39;&#39;, $img)));
                    if (!$origin_res)
                    {
                        $origin_img_save_flag = false;
                        break;
                    }
                    else
                    {
                        $saveFlag = $new_file_name;
                        $origin_img_path = $v.$new_file_name;
                        $this->THINK_IMAGE->open($origin_img_path);
                    }
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k][&#39;w&#39;];
                        $height = $imgConfig_size[$type][$k][&#39;h&#39;];
                        $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$new_file_name);
                    }

                }
            }

        }
        return $saveFlag;
    }


    /*
     *  保存file类型文件
     *
     */
    public function saveImg_file($img = null , $type = null)
    {

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;

        if ($img && $imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date(&#39;Ymd&#39;)))
                {
                    mkdir($v.date(&#39;Ymd&#39;),0777,true);
                }

                if ($k == &#39;origin&#39;)
                {
                    $this->THINK_UPLOAD->rootPath = $v;
                    $this->THINK_UPLOAD->subName = date(&#39;Ymd&#39;);
                    $this->THINK_UPLOAD->saveName = array(&#39;uniqid&#39;,&#39;&#39;);
                    $saveRes = $this->THINK_UPLOAD->uploadOne($img);
                    if (!$saveRes)
                    {
                        $origin_img_save_flag = false;
                    }
                    else
                    {
                        $saveFlag = true;
                        $origin_img_path = $v.$saveRes[&#39;savepath&#39;].$saveRes[&#39;savename&#39;];
                        $this->THINK_IMAGE->open($origin_img_path);
                        $fileName = $saveRes[&#39;savepath&#39;].$saveRes[&#39;savename&#39;];
                    }
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k][&#39;w&#39;];
                        $height = $imgConfig_size[$type][$k][&#39;h&#39;];
                        $compress_img_path=$v.$fileName;
                        $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                        if (!$saveFlag)
                        {
                            $saveFlag = false;
                            $origin_img_save_flag = false;
                        }

                    }

                }


            }

            if($saveFlag)
            {
                #$saveFlag = $fileName;
                $saveFlag = array(&#39;origin&#39;=>get_current_Host().&#39;/&#39;.$origin_img_path,&#39;compress&#39;=>get_current_Host().&#39;/&#39;.$compress_img_path);
            }

        }
        return $saveFlag;
    }

    public function save_file($img = null , $type = null)
    {

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $saveFlag = false;

        if ($img && $imgConfig_savePath[$type]) {


                if (!is_dir($imgConfig_savePath[$type] . date(&#39;Ymd&#39;))) {
                    mkdir($imgConfig_savePath[$type] . date(&#39;Ymd&#39;), 0777, true);
                }
                $this->THINK_UPLOAD->rootPath = $imgConfig_savePath[$type];
                $this->THINK_UPLOAD->subName = date(&#39;Ymd&#39;);
                $this->THINK_UPLOAD->saveName = array(&#39;uniqid&#39;, &#39;&#39;);
                $saveRes = $this->THINK_UPLOAD->uploadOne($img);
                if ($saveRes) {
                    $saveFlag = true;
                    $origin_img_path = $imgConfig_savePath[$type] . $saveRes[&#39;savepath&#39;] . $saveRes[&#39;savename&#39;];

                    //$fileName = $saveRes[&#39;savepath&#39;] . $saveRes[&#39;savename&#39;];
                }
            }


            if($saveFlag)
            {
                $saveFlag = $origin_img_path;
                return get_current_Host().&#39;/&#39;.$saveFlag;
            }else
            {
                return $saveFlag;
            }


    }




    /*
     *  保存file类型文件,多文件
     *
     */
    public function saveImgs_files($imgs = null , $type = null)
    {

        //获取保存图片配置s
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
        $imgResArr = array();//保存图片路径
        $origin_img_path = array();//原图路径

        if ($imgs && $imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date(&#39;Ymd&#39;)))
                {
                    mkdir($v.date(&#39;Ymd&#39;),0777,true);
                }

                if ($k == &#39;origin&#39;)
                {
                    $this->THINK_UPLOAD->rootPath = $v;
                    $this->THINK_UPLOAD->subName = date(&#39;Ymd&#39;);
                    $this->THINK_UPLOAD->saveName = array(&#39;uniqid&#39;,&#39;&#39;);
                    $saveRes = $this->THINK_UPLOAD->upload($imgs);

                    if ($saveRes)
                    {
                        $saveFlag = true;
                        foreach ($saveRes as $srK => $srV)
                        {
                            $origin_img_path[] = $v.$saveRes[$srK][&#39;savepath&#39;].$saveRes[$srK][&#39;savename&#39;];
                            $fileName = $saveRes[$srK][&#39;savepath&#39;].$saveRes[$srK][&#39;savename&#39;];
                            $imgResArr[] = $fileName;
                        }


                    }
                }
                else
                {
                    foreach($origin_img_path as $oipK => $oipV)
                    {
                        if ($saveFlag)
                        {
                            $width = $imgConfig_size[$type][$k][&#39;w&#39;];
                            $height = $imgConfig_size[$type][$k][&#39;h&#39;];
                            $this->THINK_IMAGE->open($oipV);
                            $saveThumb = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$imgResArr[$oipK]);
                            if (!$saveThumb)
                            {
                                $saveFlag = false;
                                break;
                            }
                        }
                    }

                }

            }

            if ($saveFlag)
            {
                $saveFlag = $imgResArr;
            }
        }
        return $saveFlag;
    }

    /*
     * 保存微信头像 - url
     *
     */
    public function save_WxImg($picUrl = &#39;&#39;,$type = null)
    {

        $ch = curl_init($picUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpInfo = curl_getinfo($ch);
        curl_close($ch);
        $media = array_merge(array(&#39;mediaBody&#39; => $package), $httpInfo);

        //求出文件格式
        preg_match(&#39;/\w\/(\w+)/i&#39;, $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];

        $saveName = md5(microtime(true)).".{$fileExt}";

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;

        if ($imgConfig_savePath[$type])
        {

            $origin_img_save_flag   = true;
            foreach($imgConfig_savePath[$type] as $k => $v)
            {

                if(!is_dir($v.date(&#39;Ymd&#39;)))
                {
                    mkdir($v.date(&#39;Ymd&#39;),0777,true);
                }

                if ($k == &#39;origin&#39;)
                {
                    file_put_contents($v.&#39;/&#39;.date(&#39;Ymd&#39;).&#39;/&#39;.$saveName,$media[&#39;mediaBody&#39;]);

                    $origin_img_path = $v.&#39;/&#39;.date(&#39;Ymd&#39;).&#39;/&#39;.$saveName;
                    $this->THINK_IMAGE->open($origin_img_path);
                    $fileName = date(&#39;Ymd&#39;).&#39;/&#39;.$saveName;
                }
                else
                {
                    if ($origin_img_save_flag)
                    {
                        $width = $imgConfig_size[$type][$k][&#39;w&#39;];
                        $height = $imgConfig_size[$type][$k][&#39;h&#39;];
                        $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                        if (!$saveFlag)
                        {
                            $saveFlag = false;
                            $origin_img_save_flag = false;
                        }
                        else
                        {
                            $saveFlag = $fileName;
                        }
                    }

                }
            }
        }
        return $saveFlag;

    }

    /*
     *  保存file类型文件
     *
     */
    public function saveImg_Wx($WxImg = array() , $type = null)
    {

        //获取保存图片配置
        $imgConfig_savePath = C("img_save.save_path");
        $imgConfig_size     = C("img_save.size");
        $saveFlag = false;
        $imgResArr = array();
        
        if ($WxImg && $imgConfig_savePath[$type])
        {


            foreach ($WxImg as $wiK => $wiV)
            {

                $wxImg = $this->getWxImg($wiV);

                $origin_img_save_flag   = true;
                foreach($imgConfig_savePath[$type] as $k => $v)
                {

                    if(!is_dir($v.date(&#39;Ymd&#39;)))
                    {
                        mkdir($v.date(&#39;Ymd&#39;),0777,true);
                    }

                    if ($k == &#39;origin&#39;)
                    {
                        file_put_contents($v.&#39;/&#39;.date(&#39;Ymd&#39;).&#39;/&#39;.$wxImg[&#39;saveName&#39;],$wxImg[&#39;imgMedia&#39;][&#39;mediaBody&#39;]);

                        $origin_img_path = $v.&#39;/&#39;.date(&#39;Ymd&#39;).&#39;/&#39;.$wxImg[&#39;saveName&#39;];
                        $this->THINK_IMAGE->open($origin_img_path);
                        $fileName = date(&#39;Ymd&#39;).&#39;/&#39;.$wxImg[&#39;saveName&#39;];
                        $imgResArr[] = $fileName;
                    }
                    else
                    {
                        if ($origin_img_save_flag)
                        {
                            $width = $imgConfig_size[$type][$k][&#39;w&#39;];
                            $height = $imgConfig_size[$type][$k][&#39;h&#39;];
                            $saveFlag = $this->THINK_IMAGE->thumb($width, $height,3)->save($v.$fileName);

                            if (!$saveFlag)
                            {
                                $saveFlag = false;
                                $origin_img_save_flag = false;
                            }
                            else
                            {
                                $saveFlag = $fileName;
                            }
                        }

                    }
                }
            }


            if($saveFlag)
            {
                $saveFlag = $imgResArr;
            }

        }
        return $saveFlag;
    }
  

    /*
     * 保存用户的推广图片   在图片上写字!!!!
     */
    public function save_userShareImg($bg_img_path = &#39;&#39; , $head_img_path = &#39;&#39; , $qrCode_img_path = &#39;&#39; , $saveFile = &#39;&#39; , $userName = &#39;&#39; , $userDscp = &#39;&#39; , $font_path = &#39;&#39;)
    {

        
        //设置头像图片为圆角
        $head_img_radius = $this->radius_img($head_img_path,240);
        //合并到背景图中
        $this->mergeImage($bg_img_path,$head_img_radius,$saveFile,array(&#39;left&#39; => 200, &#39;top&#39; => 55, &#39;width&#39; => 480, &#39;height&#39; => 480));
        //设置名称
        $this->writeText($saveFile, $saveFile, $userName,255,255,255,1010,32,$font_path, array());
        //设置二维码内容
        $this->mergeImage($saveFile,$qrCode_img_path,$saveFile,array(&#39;left&#39; => 255, &#39;top&#39; => 684, &#39;width&#39; => 250, &#39;height&#39; => 245));

        return $saveFile;
    }

    /*
     * 合并图片
     */
    private function mergeImage($bg_img, $main_img, $saveFile, $param)
    {

        extract($param);
//        list($bgWidth, $bgHeight) = getimagesize($bg_img);
        $bgImg = $this->imagecreate($bg_img);
        if (!is_resource($main_img))
        {
            list($mainWidth, $mainHeight) = getimagesize($main_img);
            $mainImg = $this->imagecreate($main_img);
        }
        else
        {
            $mainImg = $main_img;

            $mainWidth = $width;
            $mainHeight = $height;
        }
        imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $mainWidth, $mainHeight);
//        imagecopyresized($bgImg, $mainImg, $left, $top, 0, 0, $width, $height, $width,$height);
        ob_start();
        // output jpeg (or any other chosen) format & quality
        imagejpeg($bgImg, NULL, 100);
        $contents = ob_get_contents();
        ob_end_clean();
        imagedestroy($bgImg);
        imagedestroy($mainImg);
        $fh = fopen($saveFile, "w+");
        fwrite($fh, $contents);
        fclose($fh);
    }

    /*
     * 图片写文字
     * $bg_img 背景图片
     * $saveFile 保存图片路径
     * $text 文字
     * $fontColor 字体颜色
     * $top 图片距离顶端高度
     * $fontSize 字体大小
     * $font 字体路径
     * $param 传递的参数
     */
    private function writeText($bg_img, $saveFile, $text, $colorR,$colorG,$colorB , $top , $fontSize , $font , $param = array())
    {
        list($bgWidth, $bgHeight) = getimagesize($bg_img);
        $im = imagecreatefromjpeg($bg_img);

        $fontColor = imagecolorallocate($im, $colorR, $colorG, $colorB);//字的RGB颜色
        $str = mb_convert_encoding($text, "html-entities", "utf-8");;//解决乱码问题

        $fontBox = imagettfbbox($fontSize, 0, $font, $str);//文字水平居中实质
        $width = imagesx($im);
        imagettftext($im, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $top, $fontColor, $font, $str);


        ob_start();
        // output jpeg (or any other chosen) format & quality
        imagejpeg($im, NULL, 100);
        $contents = ob_get_contents();
        ob_end_clean();
        imagedestroy($im);
        $fh = fopen($saveFile, "w+");
        fwrite($fh, $contents);
        fclose($fh);
    }

    /**
     * 处理圆角图片
     * @param  string $imgPath 源图片路径
     * @param  integer $radius 圆角半径长度默认为15,处理成圆型
     * @return [type]           [description]
     */
    public function radius_img($imgPath = &#39;&#39;, $radius = 65)
    {
        $ext = pathinfo($imgPath);
        $src_img = null;
        switch ($ext[&#39;extension&#39;]) {
            case &#39;jpg&#39;:
                $src_img = imagecreatefromjpeg($imgPath);
                break;
            case &#39;png&#39;:
                $src_img = imagecreatefrompng($imgPath);
                break;
            default:
                $src_img = imagecreatefromjpeg($imgPath);
                break;
        }
        $wh = getimagesize($imgPath);
        $w = $wh[0];
        $h = $wh[1];
        // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $radius; //圆 角半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
                    //不在四角的范围内,直接画
                    imagesetpixel($img, $x, $y, $rgbColor);
                } else {
                    //在四角的范围内选择画
                    //上左
                    $y_x = $r; //圆心X坐标
                    $y_y = $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //上右
                    $y_x = $w - $r; //圆心X坐标
                    $y_y = $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //下左
                    $y_x = $r; //圆心X坐标
                    $y_y = $h - $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                    //下右
                    $y_x = $w - $r; //圆心X坐标
                    $y_y = $h - $r; //圆心Y坐标
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                    }
                }
            }
        }
        return $img;
    }

    private function imagecreate($bg)
    {
        $bgImg = @imagecreatefromjpeg($bg);
        if (FALSE == $bgImg) {
            $bgImg = @imagecreatefrompng($bg);
        }
        if (FALSE == $bgImg) {
            $bgImg = @imagecreatefromgif($bg);
        }
        return $bgImg;
    }


    /*
     * 下载微信服务器的图片
     * media_id    微信的媒体id
     * type        保存图片类型
     * */
    public function getWxImg($mediaId)
    {
        if(!class_exists(&#39;\JSSDK&#39;) && !class_exists(&#39;\WxPayConfig&#39;))
        {
            vendor(&#39;wxsdkapi.jssdk&#39;);
            vendor(&#39;wxpayapi.lib.WxPay#Config&#39;);
        }
        $res = new \JSSDK(C(&#39;WEIXINPAY_CONFIG.APPID&#39;),C(&#39;WEIXINPAY_CONFIG.APPSECRET&#39;));
        $accessToken = $res->getAccessToken();
    
//    $mediaId = &#39;cVqPIhDPPThUtayzS27GK6RN4-dseA6AZnTJxcg7_VaeuAaKcL53X32hmKxhgiSD&#39;;
//    $accessToken = &#39;KHaopJY444kJ06LcH9ESj-73pkEfFTgIr3L45RURuUxv1KTjgXyFepJ-cTIF_oUyAKyYb5dK2r9L_wC-WDw2USqGQBMobC6DzPImuhPqxB8jxZ41CuLDtJiaEZk8Vf5kWSSjAAAAWW&#39;;
        $picUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=$mediaId";

        $ch = curl_init($picUrl);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpInfo = curl_getinfo($ch);
        curl_close($ch);
        $media = array_merge(array(&#39;mediaBody&#39; => $package), $httpInfo);

        //求出文件格式
        preg_match(&#39;/\w\/(\w+)/i&#39;, $media["content_type"], $extmatches);
        $fileExt = $extmatches[1];

        $saveName = md5(microtime(true)).".{$fileExt}";

        $img = array(&#39;imgMedia&#39;=>$media,&#39;saveName&#39;=>$saveName);

        return $img;
    }
        /*
     * 下载微信服务器的图片
     * media_id    微信的媒体id
     * type        保存图片类型
     * */
    public function getWxImg2($mediaId,$file)
    {
        if(!class_exists(&#39;\JSSDK&#39;) && !class_exists(&#39;\WxPayConfig&#39;))
        {
            vendor(&#39;wxsdkapi.jssdk&#39;);
            vendor(&#39;wxpayapi.lib.WxPay#Config&#39;);
        }
        $res = new \JSSDK(C(&#39;WEIXINPAY_CONFIG.APPID&#39;),C(&#39;WEIXINPAY_CONFIG.APPSECRET&#39;));
        $accessToken = $res->getAccessToken();
        $picUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$accessToken&media_id=$mediaId";
    
        
       

        return httpcopy($picUrl,$file);
    }

}

호출 방법:

/**
     * 生成图片
     * @param varchar  tid 任务id
     * @httpmethod POST
     * @response 响应数据
     * {
     *   "ResultType": "1000",
     *   "Message": "上传成功",
     *   "AppendData": "图片路径",
     * }
     */
    public function UploadNavImg()
    {
        $tid=Util::getSafeText(I(&#39;post.tid&#39;));
        $imgEven = A(&#39;Gszc/Img&#39;,&#39;Event&#39;);
        //查出公司名称
        $res = M(&#39;gszc_tasks as t&#39;)
            ->join(&#39;app_gszc_userinfo as u on u.tid=t.id&#39;)
            ->where(&#39;t.id=&#39;.$tid)
            ->field(&#39;u.*,t.lastname&#39;)
            ->find();

        //主营业务范围
        $zyywfws = explode(&#39;、&#39;,$res[&#39;zyywfw&#39;]);
        $zyywfw = array_slice($zyywfws, 0,3);  //获取前三个主营业范围
        $zyywfw2 = array_slice($zyywfws, 3,6);  //获取后三个主营业务范围

        $imageName = date("His",time())."_".rand(1111,9999).&#39;.jpg&#39;;
        $dir = date(&#39;Ymd&#39;);
        $path = &#39;uploads/company/&#39;.$dir;
        if (!is_dir($path)){ //判断目录是否存在 不存在就创建
            mkdir($path,0777,true);
        }
        $img = $imgEven->save_userShareImg($_SERVER[&#39;DOCUMENT_ROOT&#39;]."/uploads/test.png",&#39;&#39;,&#39;&#39;,$_SERVER[&#39;DOCUMENT_ROOT&#39;]."/".$path."/". $imageName,$res[&#39;lastname&#39;],$zyywfw, $zyywfw2, &#39;&#39;,$_SERVER[&#39;DOCUMENT_ROOT&#39;]."/uploads/PingFang Bold.ttf");

        if($img){
            //生成照片成功 保存到任务表里
            $tasks = M(&#39;gszc_tasks&#39;)->where(&#39;id=&#39;.$tid)->setField(&#39;lastpic&#39;,$path."/".$imageName);
            if($tasks){
                $lujing = $path."/".$imageName;
                $this->array_return[&#39;errno&#39;]=self::__OK__;
                $this->array_return[&#39;errmsg&#39;]=&#39;生成成功!&#39;;
                $this->array_return[&#39;data&#39;]= $lujing ;
                $this->ajaxReturn($this->array_return);
            }
        }else{
            $this->array_return[&#39;errno&#39;]=self::__ERROR__;
            $this->array_return[&#39;errmsg&#39;]=&#39;生成失败!&#39;;
            $this->array_return[&#39;data&#39;]=[];
            $this->ajaxReturn($this->array_return);
        }
    }

관련 튜토리얼: PHP 비디오 튜토리얼

위 내용은 thinkphp 프레임워크 GD 라이브러리를 사용하여 사진에 텍스트를 쓰는 방법(코드 예)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제