阿里云OSS对象存储
2018-02-25 11:20
375 查看
composer安装方式
composer require aliyuncs/oss-sdk-php
2.tp5中的aliyun配置文件
'aliyun'=>[
'accessKeyId'=> 'LTAIK8hGVmhUlOd5',
'accessKeySecret'=> 'XzNA8EBLMuH9SKQZqwdDa0dyDb3TOG',
'endpoint'=> 'oss-cn-shanghai-internal.aliyuncs.com',
'endpoint_out'=> 'oss-cn-shanghai.aliyuncs.com',
'custom'=> 'guwanimg.tianbaoweipai.com',
'bucket'=> 'onlineantique',
'pre_'=> 'guwan',
'gwpre_'=> 'guwan',
'aliyundomain'=> 'http://onlineantique.oss-cn-shanghai.aliyuncs.com',
'cdntianbao'=> 'http://guwanimg.tianbaoweipai.com'
]3,服务层封装类
<?php
namespace app\admin\collection;
use OSS\Core\OssException;
use OSS\OssClient;
use think\Config;
class AliyunOss
{
private $accessKeyId;
private $accessKeySecret;
private $endpoint;
public $bucket;
private $isCName;
public function __construct($isCName=true)
{
$this->accessKeyId = Config::get('aliyun.accessKeyId');
$this->accessKeySecret = Config::get('aliyun.accessKeySecret');
if($isCName===true){
$this->endpoint = Config::get('aliyun.custom');
}else{
$this->endpoint = Config::get('aliyun.endpoint_out');
}
$this->bucket = Config::get('aliyun.bucket');
$this->isCName = $isCName;
}
public function upload($bucket, $object, $content){
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->isCName);
return $ossClient->putObject($this->bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
}
public function createBucket ($bucket){
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$ossClient->createBucket($bucket);
} catch (OssException $e) {
print $e->getMessage();
}
}
public function getCurlofimg($bucketName,$object,$timeout=3600){
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
/**
* 生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
*/
$waterurl ='logo/guwanwater.png?x-oss-process=image/resize,P_20';
$waterurl = rtrim(strtr(base64_encode($waterurl), '+/', '-_'), '=');
$options = array(
OssClient::OSS_PROCESS => 'image/resize,m_fixed,h_700,w_700/sharpen,100/watermark,image_'.$waterurl.',t_90,g_se,x_10,y_10',
);
$signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);
return $signedUrl;
}
public function nomatergetCurlofimg($bucketName,$object,$timeout=3600){
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
/**
* 生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒v
*/
$options = array(
OssClient::OSS_PROCESS => 'image/resize,m_fill,h_160,w_160/sharpen,100',
);
$signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, 'GET', $options);
return $signedUrl;
}
/*
* 身份证水印图片
*
*/
public function getnomatergetCurlofimgverfity($bucketName,$object,$timeout=3600){
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
/**
* 生成一个带签名的可用于浏览器直接打开的url, URL的有效期是3600秒
*/
$waterurl =Config::get('aliyunwatertextweixin.yongtu');
$waterurl = rtrim(strtr(base64_encode($waterurl), '+/', '-_'), '=');
$fontType ='fangzhengshusong';
$fontType = rtrim(strtr(base64_encode($fontType), '+/', '-_'), '=');
$options = array(
OssClient::OSS_PROCESS => 'image/resize,m_fixed,h_1200,w_1200/sharpen,100/watermark,type_'.$fontType.',size_40,text_'.$waterurl.',color_FFFFFF,size_30,rotate_315,t_90,g_center,x_10,y_10',
);
$signedUrl = $ossClient->signUrl($bucketName, $object, $timeout, "GET", $options);
return $signedUrl;
}
public function uploadFile($bucket, $object, $file_name){
try {
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->isCName);
return $ossClient->uploadFile($bucket, $object, $file_name);
} catch (OssException $e) {
$e->getMessage();
return false;
}
}
}4,微信上传示例
引用aliyun封装类 use app\admin\collection\AliyunOss;
foreach($img_arr as $key=>$v){ //$img = (new Weixin())->downWeixinImage($v); $url = Config::get('weixin_api.get_down_weixinuploadimg_url'); $cacheobj = Cache::store('filetoken'); $ACCESS_TOKEN = (new Weixin())->getAccessToeknCode($cacheobj); $url = str_replace('MEDIA_ID', $v, $url); $url = str_replace('ACCESS_TOKEN', $ACCESS_TOKEN, $url); $img = file_get_contents($url); $objDateTime = new \DateTime(); $currenttime = date('Y-m-d-H-i'); $pre_ = Config::get('aliyun.gwpre_') . '/'; $pre_name = $pre_ . $objDateTime->format('Y-m-d') . '/'; //获取图片上传到阿里云 //先上传图片 $tmp = $pre_name . $currenttime . '_img_' . uniqid(); $flag = (new AliyunOss())->upload(Config::get('aliyun.bucket'), $tmp, $img); if($flag){ $rescourseiddata[] = $tmp; } }5,获取上传图片(带水印)
引用aliyun封装类 use app\admin\collection\AliyunOss;
foreach($goods_images as $k=>$v){
$res_id = $v['image_url'];
$goods_images[$k]['image_url'] = (new AliyunOss(false))->getCurlofimg(Config::get('aliyun.bucket'), $res_id, 31536000);
}
相关文章推荐
- 阿里云OSS对象存储的实现
- YII2.0 接入阿里云的OSS对象存储
- 阿里云对象存储OSS————跨域资源共享(CORS)(m3u8 无法加载m3u8:跨域访问被拒绝)
- Laravel中上传视频至阿里云对象存储OSS中
- delphi调用阿里云的对象存储服务OSS
- C# .net Ueditor实现图片上传到阿里云OSS 对象存储
- 阿里云oss对象存储图片上传
- Maven学习总结(37)——利用GitHub或阿里云OSS对象存储、又拍云、七牛云存储等搭建个人Maven仓库
- thinkPHP实现对象存储oss,将图片上传到阿里云oss
- 上传阿里云oss对象型存储 本地文件和上传流上传
- 阿里云OSS ueditor 直接上传oss对象存储遇到的问题
- Java使用阿里云OSS对象存储上传图片
- 阿里云的OSS对象存储 java
- 阿里云对象存储OSS价格是多少,有什么优惠吗?
- Laravel5.2 集成阿里云--OSS对象存储服务
- 阿里云对象存储 OSS,不使用主账号,使用子账号来访问存储内容
- Java使用阿里云OSS对象存储上传图片
- 浅析Docker私有镜像库与阿里云对象存储 OSS
- java操作阿里云的对象存储OSS
- Java使用阿里云OSS对象存储上传图片