php压缩图片大小尺寸

压缩图片大小以及分辨率

class ImgCompressor {

    /**
     * 可供压缩的类型
     */
    private $setting = [
        'file_type' => [
            'image/jpeg',
            'image/png',
            'image/gif'
        ]
    ];
    private $imagePath;

    private $imageCompressPath;

    /**
     * [
     *      "0": 879,
     *      "1": 623,
     *      "2": 2,
     *      "3": "width=\"879\" height=\"623\"",
     *      "bits": 8,
     *      "channels": 3,
     *      "mime": "image/jpeg"
     *  ]
     */
    private $imageInfo;

    private $res = [
        'code' => 0,
        'original' => [
            'name' => 'oldName',
            'type' => 'imageType',
            'size' => 'imageSize'
        ],
        'compressed' => [
            'name' => 'newName',
            'type' => 'imageType',
            'size' => 'imageSize'
        ]
    ];

    function __construct($fileType = false) {
        if ($fileType)
            $this->setting['file_type'] = $fileType;
    }

    public function compress(  $level = 0) {
        if ($level < 0 || $level > 9)
            throw new \Exception(__METHOD__ . 'Compression level: [0, 9]');

        $compressImageName = $this->imageCompressPath;

        $type = $this->imageInfo['mime'];

        $image = ( 'imagecreatefrom' . basename($type) )($this->imagePath);

        if($type == 'image/jpeg'){
            imagejpeg($image, $compressImageName, (100 - ($level * 10)) );
        } else if ($type == 'image/gif') {
            if($this->ifTransparent($image)) { // 保留图片透明状态
                imageAlphaBlending($image, true);
                imageSaveAlpha($image, true);
                imagegif($image, $compressImageName);
            } else
                imagegif($image, $compressImageName);

        } else if($type == 'image/png'){
            if($this->ifTransparent($image)) {
                imageAlphaBlending($image, true);
                imageSaveAlpha($image, true);
                imagepng($image, $compressImageName, $level);
            } else
                imagepng($image, $compressImageName, $level);
        }

        // 销毁图片
        imagedestroy($image);

        $this->res['compressed']['size'] = filesize($compressImageName);

        return $this;
    }

    private function ifTransparent($image) {
        for($x = 0; $x < imagesx($image); $x++)
            for($y = 0; $y < imagesy($image); $y++)
                if((imagecolorat($image, $x, $y) & 0x7F000000) >> 24) return true;
        return false;
    }


    public function set($image, $compressImageName)
    {
        try {
            $this->imageInfo = getImageSize($image);
        } catch (\Exception $e) {
            throw new \Exception('不是图片类型');
        }

        $this->imagePath = $image;
        $this->imageCompressPath = $compressImageName;

        $this->res['original'] = [
            'name' => $this->imagePath,
            'type' => $this->imageInfo['mime'],
            'size' => filesize($this->imagePath)
        ];

        $this->res['compressed'] = [
            'name' => $this->imageCompressPath,
            'type' => $this->imageInfo['mime'],
            'size' => ''
        ];

        if( in_array($this->imageInfo['mime'], $this->setting['file_type']) )
            return $this;

        throw new \Exception(__METHOD__);
    }

    function resize($width, $height) {

        if($width == 0 && $height > 0) {
            $width = ( $height / $this->imageInfo['1'] ) * $this->imageInfo['0'] ;
        } else if ($width > 0 && $height == 0) {
            $height = ( $width / $this->imageInfo['0'] ) * $this->imageInfo['1'] ;
        } else if ($width <= 0 && $height <= 0) {
            throw new \Exception('illegal size!');
        }

        $imageSrc = ( 'imagecreatefrom' . basename($this->imageInfo['mime']) )($this->imagePath);

        $image = imagecreatetruecolor($width, $height); //创建一个彩色的底图
        imagecopyresampled($image, $imageSrc, 0, 0, 0, 0,$width, $height, $this->imageInfo[0], $this->imageInfo[1]);

        ( 'image' . basename($this->imageInfo['mime']) )($image, $this->imageCompressPath);

        $this->imagePath = $this->imageCompressPath;

        $this->res['compressed']['size'] = filesize($this->imageCompressPath);

        imagedestroy($image);
        imagedestroy($imageSrc);

        return $this;
    }

    /**
     * 获取结果
     * 
     * @return array
     * @author 19/1/17 CLZ.
     */
    public function get()
    {
        return $this->res;
    }
}

使用

$ImgCompressor = new ImgCompressor();

# 仅压缩
$result = $ImgCompressor->set('/vim.png', '/compressOnly.png')->compress(5)->get();
# 仅改变尺寸
$result = $ImgCompressor->set('/vim.jpg', '/resizeOnly.jpg')->resize(500, 500)->get();
# 压缩且改变尺寸
$result = $ImgCompressor->set('/vim.png', '/resizeAndCompress.png')->resize(0, 500)->compress(5)->get();

原文链接https://blog.csdn.net/changanyi/article/details/86523322

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值