阿里云 OSS 对象存储 OSS 图片加文字水印

实际开发需先阅读 阿里云 OSS快速入门文档

在这里插入图片描述

开通服务无实际操作难度,PASS

在这里插入图片描述

创建存储空间

在这里插入图片描述

获取 Access Key

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述在这里插入图片描述

若开通编程访问,请及时保存AccessKey 信息,页面关闭后将无法再次获取信息。

这两个东西要复制出来 要用的

在这里插入图片描述

搞好之后就 下载SDK 开始开发

在这里插入图片描述

将文件放入项目目录

文件已给好命名空间

在这里插入图片描述

编写公共函数 方便直接调用

在这里插入图片描述

以下代码均手动编写,可根据自己的实际情况重新编写(参考 官方文档 即可)
OSS 公共配置
	if (!function_exists('file_oss_ini')) {
	    /***
	     * OSS配置
	     * @author  tangshuai
	     * @date    2019.5.29
	     * @return OssClient
	     * @throws OssException
	     */
	    function file_oss_ini(){
	        $accessKeyId = "<yourAccessKeyId>" ;
	        $accessKeySecret = "<yourAccessKeySecret>";
	        // Endpoint以杭州为例,其它Region请按实际情况填写。
	        $endpoint = "oss-cn-hangzhou.aliyuncs.com";
	        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	        return $ossClient;
	    }
	}
OSS 报错抛出
	if (!function_exists('oss_error')) {
	    /***
	     * @author  tangshuai
	     * @date    2019.5.30
	     * @param string $function  必传  文件名
	     * @param string $cont      必传  错误信息
	     * @param string $bucket    选填  储存空间 默认 shjzsj
	     * @throws OssException     异常抛出
	     */
	    function oss_error($function='', $cont='', $bucket='shjzsj'){
	        $ini = file_oss_ini();
	        $file_path = date('Ymd');
	        $file_name = date('His');
	        $log = 'log/'.$file_path.'/'.$file_name.'.log';
	        $content = $function . ' => '. $cont;
	        $ini->putObject($bucket, $log, $content);
	    }
	}
OSS 上传
	if (!function_exists('oss_upload')) {
	    /***
	     * OSS上传
	     * @author  tangshuai
	     * @date    2019.5.29
	     * @param string $file      必传  文件名         从 upload/ 之后开始写
	     * @param string $type_     必传  文件分类        ['user', 'data', 'goods', 'shop']
	     * @param string $path      选填  前后端文件归总   默认前端 ['home', 'admin']
	     * @param string $bucket    选填  储存空间        默认 shjzsj
	     * @return string           返回  JSON
	     * @throws OssException     异常抛出
	     */
	    function oss_upload($file='', $type_, $path='home', $bucket="shjzsj"){
	        $path_arr = ['home', 'admin'];
	        $type_arr = ['user', 'data', 'goods', 'shop'];
	        if ( !in_array($path, $path_arr) ){
	            return returnJson(0, '路径参数 异常!', 'error');
	        }
	
	        if ( is_array($type_) ) {
	            $type = $type_['0'];
	            $file_path = $type_['1'];
	        }else{
	            $type = $type_;
	            $file_path = date('Ymd');
	        }
	
	        if ( !in_array($type, $type_arr) ){
	            return returnJson(0, '分类参数 异常!', 'error');
	        }
	
	        if ($file == ''){
	            return returnJson(0, '文件参数 异常!', 'error');
	        }
	
	        $suffix =  substr($file,strrpos($file,'.'));
	        // 重命名
	        $file_name = date('His').rand(0000, 9999).md5(rand(0000, 9999)).$suffix;
	        $filePath = WEB_ROOT . "upload/" . $file;
	        $object = $path . '/' . $type . '/' . $file_path . '/' . $file_name;
	
	        $ini = file_oss_ini();
	        try {
	            $res = $ini->uploadFile($bucket, $object, $filePath);
	            if ($res['info']['http_code'] == 200 ){
	                // 返回成功
	                unlink($filePath);
	                return returnJson(1, $res['info']['url'], 'success');
	            }else{
	                oss_error('UPLOAD', 'http_code : '.$res['info']['http_code']);
	                return returnJson(0, 'http_code: '. $res['info']['http_code'], 'error');
	            }
	        } catch (OssException $e) {
	            oss_error('UPLOAD', $e->getMessage());
	            return returnJson(0, $e->getMessage(), 'error');
	        }
	    }
	}
上传成功文件都在此目录下

在这里插入图片描述

OSS 删除文件
	if (!function_exists('oss_delete')) {
	    /***
	     * OSS删除
	     * @author  tangshuai
	     * @date    2019.5.29
	     * @param string $file      必传  文件名
	     * @param string $bucket    选填  储存空间 默认 shjzsj
	     * @return string           返回  JSON
	     * @throws OssException     异常抛出
	     */
	    function oss_delete($file='', $bucket="shjzsj"){
	        if ($file == ''){
	            return returnJson(0, '文件不存在!', 'error');
	        }
	        $left = 'http:\/\/shjzsj.oss-cn-hangzhou.aliyuncs.com\/';
	        $file = str_replace($left,"", $file);
	
	        $ini = file_oss_ini();
	        try{
	            $res = $ini->deleteObject($bucket, $file);
	            $code = $res['info']['http_code'];
	            if ($code == 200){
	                return returnJson(1, 'success', 'success');
	            }else{
	                oss_error('DELETE', 'http_code : '.$code);
	                return returnJson(0, 'http_code : '.$code, 'error');
	            }
	        } catch(OssException $e) {
	            oss_error('DELETE', $e->getMessage());
	            return returnJson(0, $e->getMessage(), 'error');
	        }
	    }
	}
OSS 图片加文字水印
	if (!function_exists('oss_watermark')) {
	    /***
	     * OSS 图片添加 文字水印
	     * @author  tangshuai
	     * @date    2019.5.29
	     * @param string $img   必传  文件名
	     * @param string $type  必传  文件分类        ['user', 'data', 'goods', 'shop']
	     * @param string $path  选填  前后端文件归总   默认前端 ['home', 'admin']
	     * @param string $text  选填  水印文字        默认前端 'www.jzsj.com 建筑设计网'
	     * @return string       返回  JSON
	     * @throws OssException 异常抛出
	     */
	    function oss_watermark($img='', $type='', $path='home', $text='www.jzsj.com 建筑设计网'){
	        if ($img == ''){
	            return returnJson(0, '参1不可为空!', 'error');
	        }
	        if ($type == ''){
	            return returnJson(0, '参2不可为空!', 'error');
	        }
	        // 表示文字水印的文字内容(必须编码)
	        $str = base64_encode($text);
	
	        // 表示文字水印的文字类型(必须编码)
	        $font_type = 'ZmFuZ3poZW5naGVpdGk=';
	
	        $param = '?x-oss-process=image/watermark,type_'.$font_type.
	            ',text_'.$str.',t_70,color_ffffff,size_30';
	
	        $suffix = substr($img,strrpos($img,'.'));
	
	        $image = $img.$param;
	
	        $get = file_get_contents($image);
	        file_put_contents(WEB_ROOT . "upload/tmp".$suffix, $get);
	
	        $img_name = "tmp".$suffix;
	        return oss_upload($img_name, $type, $path);
	    }
	}

总结 : OSS存储(基础)没什么技术难点 ,我们项目只需要用到文件的简单存、读、删,图片的文字水印 。参照 官方文档 开发即可;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值