PHP图片压缩代码(按质量、等比例压缩)

发布于 2019-12-18  16 次阅读


<?php


class Img_compress{

    private $img_input;
    private $img_output;
    private $img_src;
    private $format;
    private $quality = 80;
    private $x_input;
    private $y_input;
    private $x_output;
    private $y_output;
    private $resize;
    /*
     * 处理上传的文件保存至路径
     * @param {string} $tmp_file 上传的临时文件位置
     * @param {string} $name_file 上传的文件的原名
     * @param {string} $path 要保存的位置和名字(不带扩展名)
     */
    public function img_move($tmp_file,$name_file,$path){
        $temp = explode(".", $name_file);
        $temp =end($temp);//取数组最后一个值为扩展名
        $path = $path . '.' .$temp;
        $move = move_uploaded_file($tmp_file,$path);
        if (!$move){
            return false;
        }
        $data = [
            'path' => $path,
            'ext' => $temp
        ];
        return $data;
    }
    // 设置图片
    public function set_img($img) {
        // 查找格式
        $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
        // JPEG image
        if (is_file($img) && ($ext == "JPG" OR $ext == "JPEG")) {
            $this->format = $ext;
            $this->img_input = ImageCreateFromJPEG($img);
            $this->img_src = $img;
        } // PNG image
        elseif (is_file($img) && $ext == "PNG") {
            $this->format = $ext;
            $this->img_input = ImageCreateFromPNG($img);
            $this->img_src = $img;
        } // GIF image
        elseif (is_file($img) && $ext == "GIF") {
            $this->format = $ext;
            $this->img_input = ImageCreateFromGIF($img);
            $this->img_src = $img;
        }
        // 获取尺寸
        $this->x_input = imagesx($this->img_input);
        $this->y_input = imagesy($this->img_input);
    }

    // 设置最大图像大小 (像素)
    public function set_size($size = 100) {
        // 调整大小
        $this->x_output = $this->x_input * $size;
        $this->y_output = $this->y_input * $size;
        $this->resize = TRUE;
    }

    // 设置图像质量 (JPEG only)
    public function set_quality($quality) {
        if (is_int($quality)) {
            $this->quality = $quality;
        }
    }

    // 保存图片
    public function save_img($path) {
        // 调整大小
        if ($this->resize) {
            $this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
            ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);
        }

        // Save JPEG
        if ($this->format == "JPG" OR $this->format == "JPEG") {
            if ($this->resize) {
                imageJPEG($this->img_output, $path, $this->quality);
            } else {
                copy($this->img_src, $path);
            }
        } // Save PNG
        elseif ($this->format == "PNG") {
            if ($this->resize) {
                imagePNG($this->img_output, $path);
            } else {
                copy($this->img_src, $path);
            }
        } // Save GIF
        elseif ($this->format == "GIF") {
            if ($this->resize) {
                imageGIF($this->img_output, $path);
            } else {
                copy($this->img_src, $path);
            }
        }
    }

    // 获取宽度
    public function get_width() {
        return $this->x_input;
    }

    // 获取高度
    public function get_height() {
        return $this->y_input;
    }

    // 清除图片缓存
    public function clear_cache() {
        @ImageDestroy($this->img_input);
        @ImageDestroy($this->img_output);
    }
}

$img_compress = new Img_compress();
$head_path = $img_compress->img_move($head_temp, $head_tempName, $IdImg_path . $headImg_filename);//将图片保存至路径,并返回带路径的文件名['path']和扩展名['ext']
$img_compress->set_img($head_path['path']);//临时将图片转为jpg
$img_compress->set_quality(70);//设置图片质量
$img_compress->set_size(0.6);//设置缩放比例
$img_compress->save_img($head_path['path']);//保存图片
$img_compress->clear_cache();//清除缓存
clearstatcache();//清除文件缓存

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。