阅读背景:

阿里云OSS上传图片,并使用图片服务裁切

来源:互联网 
<?php
use OSS\OssClient;
require_once './autoload.php';

// test
$bucket = "在阿里云设置的bucket名字(这里举例叫testbucket)";
$accessKeyId = "阿里云Access Key ID"; ;
$accessKeySecret = "阿里云Access Key Secret";
// <您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com>
// 如果需要用图片处理功能那么就用图片数据中心域名,且需要在bucket里面设置开启图片服务
// oss-cn-hongkong.aliyuncs.com
$endpoint = "img-cn-hongkong.aliyuncs.com";

try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

// $object = "te";
// $content = "Hello, OSS!"; // 上传的文件内容
// $ossClient->putObject($bucket, $object, $content);

// $object = "test.png";
// $filePath = 'D:/phpStudy/WWW/oss/' . $object;
// $ossClient->uploadFile($bucket, $object, $filePath);

$object = 'message/' . rand(10000, 99999) . '.png';
$content = file_get_contents('./test.png');
$ossClient->putObject($bucket, $object, $content);

// URL: https://adtuu-test.oss-cn-hongkong.aliyuncs.com/message/67142.png
// 图片访问:
// 比如想按照等比缩放到原来的一半大小,那么在URL后面加上@50p
// URL: https://testbucket.img-cn-hongkong.aliyuncs.com/message/67142.png@50p
echo 'URL: https://testbucket.img-cn-hongkong.aliyuncs.com/' . $object, '<BR />';
echo 'URL: https://testbucket.oss-cn-hongkong.aliyuncs.com/' . $object;


} catch (OssException $e) {
print $e->getMessage();
}

分享到: