PHP图片无损压缩上传到七牛云

<?php
// 引入鉴权类
use Qiniu\Auth;

// 引入上传类
use Qiniu\Storage\UploadManager;

require_once("../qiniu_php_sdk740/autoload.php");

/**
 * PHP图片无损压缩上传到七牛云
 * @ApiParams   $upload string 必须 文件名称
 */
class imgcompress
{

    private $image;         //重绘图片
    private $imageinfo;     //重绘图片的具体信息(array)
    private $percent = 1;   //压缩比例,1为原像素比例

    //上传接口--接收前端传递过来的图片文件
    public function Upload()
    {
        $file = $_FILES['upload'];
        if (!$file) exit(json_encode(array('code' => -2, 'msg' => '没有图片文件')));
        // 要上传图片的本地路径
        $filePath = $file['tmp_name'];
        // 对图片压缩重绘
        $this->_openImage($filePath);
        // 将重绘后的图片流赋值使用
        ob_start(); // 开始输出缓存.

        $fileName = $file['name'];
        $ext = pathinfo($fileName, PATHINFO_EXTENSION); // 后缀
        if ($ext == 'jpg' || $ext == 'jpeg') {
            imagejpeg($this->image); // imagejpeg()通常会输出图像, 由于ob_start(), 所以不会.
        }
        if ($ext == 'png') {
            imagepng($this->image); // imagepng()通常会输出图像, 由于ob_start(), 所以不会.
        }

        $contents = ob_get_contents(); // 把上面的输出保存为 $content
        ob_end_clean(); // 结束输出缓存.
        imagedestroy($this->image);

        if (!$contents) exit(json_encode(array('code' => -3, 'msg' => '压缩失败')));
        // 上传文件流到七牛云
        return $this->QiniuUpload($fileName, $contents);
    }

    // 上传图片到七牛
    private function QiniuUpload($fileName, $result)
    {
        $accessKey = '你的AK'; // AK
        $secretKey = '你的SK'; // SK
        $bucket = "你的空间名称"; // 空间名称
        $domain = '你的外链域名'; // 外链域名

        $ext = pathinfo($fileName, PATHINFO_EXTENSION);  //后缀
        $key = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . '.' . $ext;

        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        //        echo $key;
        // 调用 UploadManager 的 putFile 方法进行文件的上传--直接上传图片文件
        //list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        //图片文件流
        list($ret, $err) = $uploadMgr->put($token, $key, $result);

        if ($err !== null) {
            $data['code'] = -1;
            $data['msg'] = '上传失败' . $err;
            exit(json_encode($data));
        } else {
            $data['code'] = 1;
            $data['msg'] = '上传成功';
            $data['url'] = $domain . $ret['key'];
            exit(json_encode($data));
        }

    }

    /**
     * 内部:打开图片
     */
    private function _openImage($src)
    {
        //        list($width, $height, $type, $attr) = getimagesize($this->src);
        list($width, $height, $type, $attr) = getimagesize($src);
        $this->imageinfo = array(
            'width' => $width,
            'height' => $height,
            'type' => image_type_to_extension($type, false),
            'attr' => $attr
        );
        $fun = "imagecreatefrom" . $this->imageinfo['type'];
        //        $this->image = $fun($this->src);
        $this->image = $fun($src);
        $this->_thumpImage();
    }

    /**
     * 内部:操作图片
     */
    private function _thumpImage()
    {
        $new_width = $this->imageinfo['width'] * $this->percent;
        $new_height = $this->imageinfo['height'] * $this->percent;
        $image_thump = imagecreatetruecolor($new_width, $new_height);

        //绘制图片透明底色
        imagesavealpha($image_thump, true);
        $black = imagecolorallocate($image_thump, 0, 0, 0);
        imagefilledrectangle($image_thump, 0, 0, 150, 25, $black);
        $trans_colour = imagecolorallocatealpha($image_thump, 0, 0, 0, 127);
        imagefill($image_thump, 0, 0, $trans_colour);

        //将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
        imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']);
        imagedestroy($this->image);
        $this->image = $image_thump;
    }
}

$image = (new imgcompress())->Upload();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值