<?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();
PHP图片无损压缩上传到七牛云
最新推荐文章于 2022-04-26 17:25:13 发布