php GD库的操作

#GD库的操作
生成对应的海报这时候就需要GD库的帮助了

当时写的你们可以直接拿来用

protected $image  = null;
    protected $width  = 0;
    protected $height = 0;

    /**
     * 初始化一个新的图像,可指定宽度、高度及背景色
     *
     * @param int   $width
     * @param int   $height
     * @param array $backColor #这是一个rgb颜色的数组或null值
     */
    public function __construct($width = 500, $height = 500, $backColor = [255, 255, 255])
    {
        $this->width  = $width;
        $this->height = $height;
        $this->image  = imagecreatetruecolor($width, $height);
        $color        = imagecolorallocate($this->image, $backColor[0], $backColor[1], $backColor[2]);
        imagefill($this->image, 0, 0, $color);
    }

    /**
     * 在图像上贴一张图片
     *
     * @param      $maskImage
     * @param int  $_x
     * @param int  $_y
     * @param null $width
     * @param null $height
     *
     * @return $this
     */
    public function addImage($maskImage, $_x = 0, $_y = 0, $width = null, $height = null)
    {
        if (gettype($maskImage) != 'resource') {
            if (stripos($maskImage, 'qlogo.cn') !== false) {
                $maskImage = Helper::getHttpImg($maskImage);
            } else {
                $maskImage = imagecreatefromstring(file_get_contents($maskImage));
            }
        }
        $maskImageWidth  = imagesx($maskImage);
        $maskImageHeight = imagesy($maskImage);
        $width           = $width == null ? $maskImageWidth : $width;
        $height          = $height == null ? $maskImageHeight : $height;
        imagecopyresampled($this->image, $maskImage, $_x, $_y, 0, 0, $width, $height, $maskImageWidth,
            $maskImageHeight);
        return $this;
    }

    /**
     *  照片类型
     * @param $maskImage
     * @param $degrees
     *
     * @return bool|resource
     */
    public function rotateImage($maskImage,$degrees)
    {

        $original = getimagesize($maskImage);
        switch ($original[2]) {
            case '1':
                $source = imagecreatefromgif($maskImage);
                break;
            case '2':
                $source = imagecreatefromjpeg($maskImage);
                break;
            case '3':
                $source = imagecreatefrompng($maskImage);
                break;
        }
        if (empty($source)) return false;
        $rotate = imagerotate($source,$degrees,0);
        return $rotate;
    }

    /**
     * 在图像上增加文字(就是普通的字体)
     *
     * @param       $text
     * @param       $fontSize
     * @param array $fontColor #这是一个rgb颜色的数组
     * @param int   $_x
     * @param int   $_y
     * @param bool  $bold
     *
     * @return $this
     */
    public function addText($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = $bold ? storage_path('font/msyhbd.ttf') : storage_path('font/msyh.ttf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }
   /**
     * 在图像上增加文字(就是普通的字体)
     *
     * @param       $text
     * @param       $fontSize
     * @param array $fontColor #这是一个rgb颜色的数组
     * @param int   $_x
     * @param int   $_y
     * @param bool  $bold
     *
     * @return $this
     */
    public function addTextHb($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = storage_path('font/hb.ttf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }

    public function addTextLgf($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = storage_path('font/lgf.ttf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }

    public function addTextHlg($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = storage_path('font/Alibaba-PuHuiTi-Heavy.otf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }

    //读书日活动字体
    public function addText423($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = storage_path('font/XianErTi-2.ttf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }

    public function addTextRubbish($text, $fontSize, $fontColor = [0, 0, 0], $_x = 0, $_y = 0, $bold = false)
    {
        $im    = imagecreatetruecolor(1, 1);
        $color = imagecolorexact($im, $fontColor[0], $fontColor[1], $fontColor[2]);
        $font  = storage_path('font/Alibaba-PuHuiTi-Heavy.otf');
        imagefttext($this->image, $fontSize, 0, $_x, $_y, $color, $font, $text);
        return $this;
    }

    /**
     * 输出图像
     */
    public function render()
    {
        header('Content-type: image/png');
        imagepng($this->image);
        imagedestroy($this->image);
    }

    /**
     * 将图像转换为圆型
     *
     * @param $srcImage
     *
     * @return resource
     */
    public function roundImage($srcImage)
    {
        if (gettype($srcImage) != 'resource') {
            $srcImage = imagecreatefromstring(file_get_contents($srcImage));
        }
        $w   = imagesx($srcImage);
        $h   = imagesy($srcImage);
        $img = imagecreatetruecolor($w, $h);
        imagesavealpha($img, true);
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $w / 2; //圆半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($srcImage, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
        return $img;
    }

对于一些变态的需求就是给你文字让你自动换行啥的可以用

             $len = strlen($content); //utf-8 mb_strlen(也可以用这个)
                $str = '';
                for ($i = 0; $i <= $len; $i += 30) {
                    $str = substr($content, $i, 30); // mb_substr(也可以用这个)
                    $image->addText($str, 27, [255, 255, 255], 175, 760 + $i * 2);//签的字
                }

剩下的变态的东西就自己调吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值