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); }