阿里云OSS对象存储

  1. 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的有效期是3600v
         */

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值