<?php


namespace App\Utility;

use Es3\Call\Curl;
use Es3\Exception\WaringException;

class ImageUtil
{
    /**
     * @param string $image
     * @return string|null
     * @throws \EasySwoole\HttpClient\Exception\InvalidUrl
     * Created by PhpStorm
     * User:jill
     * Date:2022/10/27
     * Time:16:33
     */
    public static function down(string $image): ?string
    {
        $arr = explode(".", $image);
        $file = "down_" . time() . '.' . $arr[count($arr) - 1];
        $tempDir = config("TEMP_DIR");
        $curl = new Curl($image);
        $curl->download($tempDir . $file);
        return $tempDir . $file;
    }

    /**
     * 图片添加水印
     * $file = 'test.png';   //待加水印的图片地址
     * $water = 'logo.png';  //水印图片的地址
     * watermark($file, $water);
     * @param $img
     * @param $watermark
     * @param int $district
     * @param int $watermarkquality
     * @return string|null Created by PhpStorm
     * Created by PhpStorm
     * User:jill
     * Date:2022/10/27
     * Time:15:22
     * @throws \Es3\Exception\WaringException
     */
    public static function watermark($img, $watermark, $district = 0, $watermarkquality = 95): ?string
    {
        $imginfo = @getimagesize($img);

        $watermarkinfo = @getimagesize($watermark);

        $img_w = $imginfo[0];

        $img_h = $imginfo[1];

        $watermark_w = $watermarkinfo[0];
        $watermark_h = $watermarkinfo[1];

        if ($district == 0) {
            $district = rand(1, 9);
        }

        if (!is_int($district) or 1 > $district or $district > 9) {
            $district = 9;
        }

        if (($img_w - $watermark_w) < 10 || ($img_h - $watermark_h) < 10) {
            unlink($img);
            unlink($watermark);
            throw new WaringException(1010, "两个图片相差太小");
        }

        switch ($district) {

            case 1:
                $x = +5;
                $y = +5;
                break;

            case 2:
                $x = ($img_w - $watermark_w) / 2;
                $y = +5;
                break;

            case 3:
                $x = $img_w - $watermark_w - 5;
                $y = +5;
                break;

            case 4:

                $x = +5;
                $y = ($img_h - $watermark_h) / 2;
                break;

            case 5:

                $x = ($img_w - $watermark_w) / 2;
                $y = ($img_h - $watermark_h) / 2;
                break;

            case 6:
                $x = $img_w - $watermark_w;
                $y = ($img_h - $watermark_h) / 2;
                break;

            case 7:
                $x = +5;
                $y = $img_h - $watermark_h - 5;
                break;
            case 8:
                $x = ($img_w - $watermark_w) / 2;
                $y = $img_h - $watermark_h - 5;
                break;
            case 9:
                $x = $img_w - $watermark_w - 30;
                $y = $img_h - $watermark_h - 30;
                break;

        }

        switch ($imginfo[2]) {
            case 1:
                $im = imagecreatefromgif($img);
                break;
            case 2:
                $im = imagecreatefromjpeg($img);
                break;
            case 3:
                $im = imagecreatefrompng($img);
                break;

        }

        switch ($watermarkinfo[2]) {
            case 1:
                $watermark_logo = imagecreatefromgif($watermark);
                break;
            case 2:
                $watermark_logo = imagecreatefromjpeg($watermark);
                break;
            case 3:
                $watermark_logo = imagecreatefrompng($watermark);
                break;

        }

        if (!$im or !$watermark_logo) {
            return false;
        }

        $dim = imagecreatetruecolor($img_w, $img_h);

        if (imagecopy($dim, $im, 0, 0, 0, 0, $img_w, $img_h)) {
            imagecopy($dim, $watermark_logo, $x, $y, 0, 0, $watermark_w, $watermark_h);
        }

        $file = dirname($img) . '/w' . basename($img);
        imagejpeg($dim, $file, $watermarkquality);

        imagedestroy($watermark_logo);

        imagedestroy($dim);

        imagedestroy($im);

        return $file;
    }


    /**
     * 图片等比例压缩
     * desription 压缩图片
     * @param string $imgSrc 图片路径
     * @param int $compressWidth
     * @return string|null
     */
    public static function compressedImage(string $imgSrc, int $compressWidth): ?string
    {
        $info = getimagesize($imgSrc);
        $width = $info[0];
        $height = $info[1];
        $type = $info[2];


        $arr = explode(".", $imgSrc);
        $file = "compressedImage_" . time() . '.' . $arr[count($arr) - 1];
        $tempDir = config("TEMP_DIR");
        $imgDst = $tempDir . $file;

        $new_width = $width;//压缩后的图片宽
        $new_height = $height;//压缩后的图片高

        if ($width >= $compressWidth) {
            $per = $compressWidth / $width;//计算比例
            $new_width = $width * $per;
            $new_height = $height * $per;
        }

        switch ($type) {
            case 1:
                $giftype = check_gifcartoon($imgSrc);
                if ($giftype) {
                    $image_wp = imagecreatetruecolor($new_width, $new_height);
                    $image = imagecreatefromgif($imgSrc);
                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //90代表的是质量、压缩图片容量大小
                    imagejpeg($image_wp, $imgDst, 90);
                    imagedestroy($image_wp);
                    imagedestroy($image);
                }
                break;
            case 2:

                $image_wp = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($imgSrc);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                //90代表的是质量、压缩图片容量大小
                imagejpeg($image_wp, $imgDst, 90);
                imagedestroy($image_wp);
                imagedestroy($image);
                break;
            case 3:

                $image_wp = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefrompng($imgSrc);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                //90代表的是质量、压缩图片容量大小
                imagejpeg($image_wp, $imgDst, 90);
                imagedestroy($image_wp);
                imagedestroy($image);
                break;
        }
        return $imgDst;
    }

    /**
     * 背景变透明
     * $o_pic = '1.jpg';
     * $name = pngMerge($o_pic,'aaaa.png');
     * print_r($name);
     * @param string $oPic
     * @return mixed
     * Created by PhpStorm
     * User:jill
     * Date:2022/10/28
     * Time:14:52
     */
    public static function pngMerge(string $oPic): ?string
    {
        $arr = explode(".", $oPic);
        $file = "pngMerge_" . time() . '.' . $arr[count($arr) - 1];
        $tempDir = config("TEMP_DIR");
        $outPic = $tempDir . $file;

        $begin_r = 255;
        $begin_g = 250;
        $begin_b = 250;
        $info = getimagesize($oPic);// 获取原图像信息 宽高

        $src_w = $info[0];
        $src_h = $info[1];

        $src_im = imagecreatefromjpeg($oPic); //读取png图片

        //imagesavealpha($src_im,true);//这里很重要 意思是不要丢了$src_im图像的透明色
        $src_white = imagecolorallocatealpha($src_im, 255, 255, 255, 127); // 创建一副白色透明的画布
        for ($x = 0; $x < $src_w; $x++) {
            for ($y = 0; $y < $src_h; $y++) {
                $rgb = imagecolorat($src_im, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                if ($r == 255 && $g == 255 && $b == 255) {
                    imagefill($src_im, $x, $y, $src_white); //填充某个点的颜色
                    imagecolortransparent($src_im, $src_white); //将原图颜色替换为透明色
                }
                if (!($r <= $begin_r && $g <= $begin_g && $b <= $begin_b)) {
                    imagefill($src_im, $x, $y, $src_white);//替换成白色
                    imagecolortransparent($src_im, $src_white); //将原图颜色替换为透明色
                }
            }
        }


        $target_im = imagecreatetruecolor($src_w, $src_h);//新图

        imagealphablending($target_im, false);//这里很重要,意思是不合并颜色,直接用$target_im图像颜色替换,包括透明色;
        imagesavealpha($target_im, true);//这里很重要,意思是不要丢了$target_im图像的透明色;
        $tag_white = imagecolorallocatealpha($target_im, 255, 255, 255, 127);//把生成新图的白色改为透明色 存为tag_white
        imagefill($target_im, 0, 0, $tag_white);//在目标新图填充空白色
        imagecolortransparent($target_im, $tag_white);//替换成透明色
        imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);//合并原图和新生成的透明图
        imagepng($target_im, $outPic);

        return $outPic;
    }
}
最后修改:2022 年 11 月 02 日
如果觉得我的文章对你有用,请随意赞赏