连续复制
一键复制
一键打包
class Img
{
    private static $instance;

    /**
     * 初始化
     * @access public
     * @param array $options 参数
     * @return Img
     */
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($options);
        }

        return self::$instance;
    }

    /**
     * 调整 头像大小
     * @param $imgSource
     * @param $maxwidth
     * @param $maxheight
     * @return resource
     */
    public function resizeImage(&$imgSource, $maxwidth, $maxheight)
    {
        $width = imagesx($imgSource);
        $height = imagesx($imgSource);

        $bgSource = imagecreatetruecolor($maxwidth, $maxheight); // 创建一个真彩色图像 我把它理解为创建了一个画布

        imagecopyresampled($bgSource, $imgSource, 0, 0, 0, 0, $maxwidth, $maxheight, $width, $height);
        return $bgSource;
    }

    /**
     * 获取图片资源
     * @param $imgPath
     * return resource images
     */
    public function getSource($imgPath)
    {
        $ext = pathinfo($imgPath);
        switch ($ext['extension']) {
            case 'jpg':
                $imgSource = imagecreatefromjpeg($imgPath);
                break;
            case 'png':
                $imgSource = imagecreatefrompng($imgPath);
                break;
            case 'jpeg':
                $imgSource = imagecreatefromjpeg($imgPath);
                break;
            case 'wbmp':
                $imgSource = imagecreatefromwbmp($imgPath);
                break;
            case 'gif':
                $imgSource = imagecreatefromgif($imgPath);
                break;
        }


        return $imgSource;
    }

    /**
     * 处理成圆图片,如果图片不是正方形就取最小边的圆半径,从左边开始剪切成圆形
     *
     * @param $imgSource
     * @param null $width
     * @param null $height
     * @param bool $return
     * @return resource
     */
    public function circular(&$imgSource)
    {

        $w = imagesx($imgSource);
        $h = imagesy($imgSource);
        $w = min($w, $h);
        $h = $w;
        $returnSource = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($returnSource, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($returnSource, 255, 255, 255, 127);
        imagefill($returnSource, 0, 0, $bg);
        $r = $w / 2; //圆半径
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($imgSource, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($returnSource, $x, $y, $rgbColor);
                }
            }
        }

        return $returnSource;
    }

    /**
     * 合并图片.
     * @param string $bgSource
     * @param string $smSource
     * @param int $imgX
     * @param int $imgY
     * @return string
     */
    public function mergeImages(&$bgSource = '', &$smSource = '', $imgX = 0, $imgY = 0)
    {
        // 获取点图片的宽高
        $point_w = imagesx($smSource);
        $point_h = imagesy($smSource);

        // 重点:png透明用这个函数
        imagecopy($bgSource, $smSource, $imgX, $imgY, 0, 0, $point_w, $point_h);
        return $bgSource;
    }

    /**
     * 合并文字
     * @param string $bgSource
     * @param int $fontInt
     * @param $text
     * @param int $fontSize
     * @param string $textX
     * @param int $textY
     * @return string
     */
    public function mergeText(&$bgSource = '', $fontInt = 1, $text, $fontSize = 40, $textX = '', $textY = 0)
    {
        // 背景宽度
        $width = imagesx($bgSource);
        $font = ROOT_PATH . ($fontInt == 1 ? 'public/assets/fonts/AktivGroteskCNBold.ttf' : 'public/assets/fonts/Dengb.ttf');//字体

        $fontBox = imagettfbbox($fontSize, 0, $font, $text);//文字水平居中实质

        $white = imagecolorallocate($bgSource, 51, 51, 51);//字体颜色 RGB
        $textX = $textX ?: ceil(($width - $fontBox[2]) / 2);
        $circleSize = 0; //旋转角度
        imagefttext($bgSource, $fontSize, $circleSize, $textX, $textY, $white, $font, $text);
        return $bgSource;
    }
}