作者 lyh

gx脚本demo

... ... @@ -328,15 +328,9 @@ class LoginController extends BaseController
public function ceshi()
{
$cos = new CosService();
$cdnUrl = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com/upload/p/2000/image_product/2024-07/aero-y50-introduction-3.jpg';
$cdnUrl = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com/upload/p/1/image_other/2023-11/655d9c70b692e10129.png';
$data = [
'characters' => '这是水印',
'position' => 'gravity/NorthWest',
'dx' => 'dx/20',
'dy' => 'dy/20',
'font' => 'font/5b6u6L2v6buR', // 黑体
'fontsize' => 'fontsize/30',
'fill' => 'fill/I0RGRkZGRg==', // 白色
'text' => '这是水印',
];
return $cos->addFieldImage($cdnUrl, $data);
}
... ...
... ... @@ -22,7 +22,7 @@ class CosService
* @method :post
* @time :2023/7/19 15:28
*/
public function uploadFile(&$files,$path,$filename, $binary = false)
public function uploadFile(&$files,$path,$filename, $binary = false,$watermarkOptions = null)
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
... ... @@ -34,11 +34,25 @@ class CosService
]);
$key = $path.'/'.$filename;
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
$cosClient->putObject([
$options = [
'Bucket' => $cos['bucket'],
'Key' => $key,
'Body' => $Body,
]);
];
//水印
if ($watermarkOptions) {
$options['Pic-Operations'] = json_encode([
'is_pic_info' => 1,
'rules' => [
[
'fileid' => $filename, // 使用相同的文件名保存
'rule' => $watermarkOptions,
]
]
]);
}
// 上传文件
$cosClient->putObject($options);
return $key;
}
... ... @@ -169,28 +183,54 @@ class CosService
SouthEast:右下角
*/
public function addFieldImage($cdnUrl = '',$data = [],$is_image = false){
$domain = 'http://globalso-v6-1309677403.cos.ap-hongkong.myqcloud.com';//cos域名
$url = $domain . $cdnUrl;
if($is_image){
$data['image'] = $domain . $data['image'];
$param = [
'image/'.$this->urlSafeBase64Encode($data['image']),//文字水印名称
$data['position'] ?? 'gravity/SouthEast',
$data['dx'] ?? 'dx/10/dy/10',
$data['font'] ?? 'font/5bCP6aOe',//默认宋体
$data['fontsize'] ?? 'fontsize/24',//大小
$data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色
'image/'.$this->urlSafeBase64Encode($data['image'] ?? ''),//文字水印名称
'gravity/'.$this->urlSafeBase64Encode($data['gravity'] ?? 'SouthEast'),
'dx/'.$data['dx'] ?? 10,
'dy/'. $data['dy'] ?? 10,
'batch/'.$data['batch'] ?? 0,//平铺水印功能
'dissolve/'.$data['dissolve'] ?? 50,//透明度
'degree/'.$data['degree'] ?? 0,//文字水印的旋转角度设置,取值范围为0 - 360,默认0
];
$cdnUrl = $cdnUrl.'?imageMogr2/watermark/1/'.implode('/',$param);
$url = $url.'?watermark/1/'.implode('/',$param);
}else{
$param = [
'text/'.$this->urlSafeBase64Encode($data['characters']),//文字水印名称
$data['position'] ?? 'gravity/SouthEast',
$data['dx'] ?? 'dx/10/dy/10',
$data['font'] ?? 'font/5bCP6aOe',//默认宋体
$data['fontsize'] ?? 'fontsize/24',//大小
$data['fill'] ?? 'fill/I0ZGRkZGRg==',//颜色
'text/'.$this->urlSafeBase64Encode($data['text'] ?? ''),//文字水印名称
'gravity/'.$this->urlSafeBase64Encode($data['gravity'] ?? 'SouthEast'),
'dx/'.$data['dx'] ?? 10,
'dy/'. $data['dy'] ?? 10,
'font/'.$this->urlSafeBase64Encode($data['font'] ?? 'tahoma.ttf'),//默认宋体
$data['fontsize'] ?? 'fontsize/24',//水印文字字体大小,单位为磅,缺省值13
'fill/'.$this->urlSafeBase64Encode($data['fill'] ?? '#3D3D3D'),//颜色
'dissolve/'.$data['dissolve'] ?? 50,//透明度
'degree/'.$data['degree'] ?? 0,//文字水印的旋转角度设置,取值范围为0 - 360,默认0
'batch/'.$data['batch'] ?? 0,//平铺水印功能
'shadow/'.$data['shadow'] ?? 0,//文字阴影效果,有效值为[0,100],默认为0,表示无阴影
];
$cdnUrl = $cdnUrl.'?watermark/2/'.implode('/',$param);
$url = $url.'?watermark/2/'.implode('/',$param);
}
return $cdnUrl;
// 获取水印后的图片内容
// $imageContent = file_get_contents($url);
// // 使用 COS SDK 将图片重新上传并覆盖原图
// $cos = config('filesystems.disks.cos');
// $cosClient = new \Qcloud\Cos\Client([
// 'region' => $cos['region'],
// 'credentials' => [
// 'secretId' => $cos['credentials']['secretId'],
// 'secretKey' => $cos['credentials']['secretKey'],
// ],
// ]);
// // 上传并覆盖原图
// $cosClient->putObject([
// 'Bucket' => $cos['bucket'],
// 'Key' => $cdnUrl, // 去掉域名部分,得到存储桶内的路径
// 'Body' => $imageContent,
// ]);
return $url;
}
/**
... ... @@ -200,7 +240,10 @@ class CosService
* @method :post
* @time :2024/8/19 14:21
*/
public function urlSafeBase64Encode($data) {
public function urlSafeBase64Encode($data = '') {
if(empty($data)){
return $data;
}
// 1. 使用标准的 BASE64 编码
$base64 = base64_encode($data);
// 2. 将加号(+)替换成连接号(-)
... ... @@ -211,4 +254,50 @@ class CosService
$base64 = rtrim($base64, '=');
return $base64;
}
/**
* @remark :处理9宫格数据
* @name :getPosition
* @author :lyh
* @method :post
* @time :2024/8/19 15:16
*/
public function getPosition(){
return [
1=>'gravity/NorthWest',
2=>'gravity/North',
3=>'gravity/NorthEast',
4=>'gravity/West',
5=>'gravity/Center',
6=>'gravity/East',
7=>'gravity/SouthWest',
8=>'gravity/South',
9=>'gravity/SouthEast',
];
}
/**
* @remark :字体
* @name :getFont
* @author :lyh
* @method :post
* @time :2024/8/19 15:47
*/
public function getFont(){
return [
'simfang仿宋.ttf',
'tahoma.ttf',
'simhei黑体.ttf',
'simkai楷体.ttf',
'simsun宋体.ttc',
'STHeiti Light华文黑体.ttc',
'STHeiti Medium.ttc',
'幼圆.TTF',
'ahronbd.ttf',
'arial.ttf',
'ariblk.ttf',
'Helvetica.dfont',
'HelveticaNeue.dfont'
];
}
}
... ...