PHP生成海报/PHP合并图片/PHP图片处理

外部类生成圆角图片下载地址 https://mywebmymail.com/easyphpthumbnail/

使用该功能必须有GD库支持,文字水印还需要 freetype 库支持

一、图片处理类

<?php
/**
 * Created by PhpStorm.
 * User: panxp
 * Date: 2018-08-24
 * Time: 9:50
 */

namespace Utility;

use Lib;

class ImageLib
{

    //生成新图片的地址
    public $newImage;

    public function __construct($newImage = '')
    {

        if ($newImage) {
            $this->newImage = $newImage;
        }

    }

   /** 合并图片可以多次使用 **/

    public function mergeImage($bgImgPath, $thumbImagePath, $dstx = 290, $dsty = 600)
    {


        $bigImg = imagecreatefromstring(file_get_contents($bgImgPath));
        $qCodeImg = imagecreatefromstring(file_get_contents($thumbImagePath));

        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($thumbImagePath);
// imagecopymerge使用注解
        imagecopymerge($bigImg, $qCodeImg, $dstx, $dsty, 0, 0, $qCodeWidth, $qCodeHight, 100);

        // list($bigWidth, $bigHight, $bigType) = getimagesize($bgImgPath);

        $bigType = 2;
        if ($this->newImage) {
            imagejpeg($bigImg, $this->newImage);
        } else {
            switch ($bigType) {
                case 2: //jpg
                    header('Content-Type:image/jpg');
                    imagejpeg($bigImg);
                    break;
                default:
                    # code...
                    break;
            }

        }

        imagedestroy($bigImg);
        imagedestroy($qCodeImg);

    }

/** 生成文水印 **/
    public function waterMark($text, $left, $top, $bgDir = '')
    {
        if ($this->newImage) {
            $bgDir = $this->newImage;
        }
        $img = imagecreatefromstring(file_get_contents($bgDir));
        $font = realpath('.') . DS . 'font' . DS . 'fangzhenghuali_GBK.ttf';//字体

        $black = imagecolorallocate($img, 51, 51, 51);//字体颜色 RGB

        $fontSize = 33;   //字体大小
        $circleSize = 0; //旋转角度

        imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, $text);
        // list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
        imagejpeg($img, $this->newImage);

        imagedestroy($img);
    }


    /**
     * 缩小图片
     * @param $filename
     */
    public function thumbImage($filename, $newFileName, $n_w = 160, $n_h = 160)
    {
        list($width, $height) = getimagesize($filename);
        //缩放比例
        if ($width > $n_w) {
            $new = imagecreatetruecolor($n_w, $n_h);
            $img = imagecreatefromjpeg($filename);
            //copy部分图像并调整
            imagecopyresized($new, $img, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
            //图像输出新图片、另存为
            imagejpeg($new, $newFileName);
            imagedestroy($new);
            imagedestroy($img);
            return array('width' => $n_w, 'height' => $n_h);
        } else {
            return array('width' => $width, 'height' => $height);
        }

    }

    /**
     * 生成圆角图片
     */
    public function roundCornerImage($thumbAvatarDir,$roundCornerAvatar)
    {
        include_once APP . '/Lib/ThumbImage.php';
        $thumb = new Lib\ThumbImage();
        $thumb->Backgroundcolor = '#0000FF';
        //$thumb->Thumblocation = '/data/www/cake/zhly_api/topicPoiQrcode/';
        //$thumb->Thumbprefix = 'roundCornerAvatar';
        $thumb->ThumbFileDir = $roundCornerAvatar;
        $thumb->Clipcorner = array(2, 50, 0, 1, 1, 1, 1);
        $thumb->Maketransparent = array(1, 1, '#0000FF', 30);
        $thumb->Createthumb($thumbAvatarDir, 'file');

    }
}

二、使用示例(使用项目中,没有拷贝全部代码)

        $image = new Utility\ImageLib($newImage);
        if (!file_exists($roundCornerImageAvatarDir)) {
            $http = new HttpSocket(array(
                'ssl_verify_host' => false
            ));
            //把远程头像保存到本地
            file_put_contents($avatarDir, $http->get($avatar));
            //缩小头像到160x160
            $thumbAvatar = $image->thumbImage($avatarDir, $thumbAvatarDir, $thumbAvatarWidth, $thumbAvatarHeight);

            //头像缩小
            if ($thumbAvatar['width'] == $thumbAvatarWidth) {
                //生成圆角头像
                $image->roundCornerImage($thumbAvatarDir, $roundCornerImageAvatarDir);
            } else {
                //头像原图小于160
                //生成圆角头像
                $mergeAvatarDistX = (750 - $thumbAvatar['width']) / 2;
                $mergeAvatarDistY = $mergeAvatarDistY + ($mergeAvatarDistY - $thumbAvatar['height']) / 2.8;
                $image->roundCornerImage($avatarDir, $roundCornerImageAvatarDir);
            }
        } else {
            return true;
        }


        //合并二维码
        $image->mergeImage($bgImage, $qrcodeImage, 270, 1065);

        //生成文字水印
        $image->waterMark($text, $txtMarkLeft, 440);
        //生成文字水印
        $image->waterMark($textExt, 295, 510);
        //合并头像
        $image->mergeImage($newImage, $roundCornerImageAvatarDir, $mergeAvatarDistX, $mergeAvatarDistY);

//        header('Content-Type:image/jpg');
//        imagejpeg(imagecreatefromjpeg($newImage));

         return true;

猜你喜欢

转载自blog.csdn.net/coolpan123/article/details/82221559