作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1938
<?php
/**
* @remark :
* @name :CropImage.php
* @author :lyh
* @method :post
* @time :2025/5/8 9:19
*/
namespace App\Console\Commands\CropImage;
use App\Models\Domain\DomainInfo;
use App\Models\WebSetting\AggregationSetting;
use App\Models\WebSetting\WebSettingImage;
use App\Services\CosService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CropImage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crop_image {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '裁剪图片';
public function handle(){
echo '测试裁剪->cs-crop:'.PHP_EOL;
$images ='https://ecdn6.globalso.com/upload/p/1/image_other/2025-05/1746675561075.jpg';
$image = str_replace_url($images);
$cosService = new CosService();
$new_url = $cosService->cropAndUploadFromCos($image);
return $new_url;
// $project_id = $this->argument('project_id');
// ProjectServer::useProject($project_id);
// $this->_action();
// DB::disconnect('custom_mysql');
}
/**
* @remark :执行的方法
* @name :_action
* @author :lyh
* @method :post
* @time :2025/5/8 9:21
*/
public function _action($project_id){
$data = $this->getKeywordImage($project_id);
if(!empty($data)){
$cosService = new CosService();
foreach ($data as $val){
//处理图片为相对路径
$image = str_replace_url($val);
$new_url = $cosService->cropAndUploadFromCos($image);
echo '返回的图片路径:'.$new_url.PHP_EOL;
}
}
return true;
}
/**
* @remark :获取aiBlog图片
* @name :getAiBlogImage
* @author :lyh
* @method :post
* @time :2025/5/8 10:42
*/
public function getAiBlogImage($project_id,$keywordImage){
$image = '';
// AI博客banner type:1:产品,2:博客,3:新闻,4:AIBlog
$webSettingImageModel = new WebSettingImage();
$aiBlogInfo = $webSettingImageModel->read(['project_id' => $project_id, 'type' => 4],['image']);
if($aiBlogInfo !== false && !empty($aiBlogInfo['image'])){
$image = getImageUrl($aiBlogInfo['image']);
}
if(empty($image) && !empty($keywordImage)){
$image = $keywordImage;
}
return $image;
}
/**
* @remark :获取聚合页图片
* @name :getImage
* @author :lyh
* @method :post
* @time :2025/5/8 9:21
*/
public function getKeywordImage($project_id){
$data = [];
// 聚合页banner
$aggregationSettingModel = new AggregationSetting();
$aggregationSettingInfo = $aggregationSettingModel->read(['project_id' => $project_id],['id','top_banner']);
if($aggregationSettingInfo !== false && !empty($aggregationSettingInfo['top_banner'])){
foreach ($aggregationSettingInfo['top_banner'] as $val){
if($val != 'jpg' && $val != 'png' && $val != 'webp'){
$data[] = $val;
}
}
}
if(empty($data)){
//重页面上获取首页banner
$data = $this->getDomImage($project_id);
}
return $data;
}
/**
* @remark :页面上获取图片
* @name :getDomImage
* @author :lyh
* @method :post
* @time :2025/5/8 10:32
*/
public function getDomImage($project_id){
$data = [];
echo '获取首页banner:' . $project_id . PHP_EOL;
$domainModel = new DomainInfo();
$domainInfo = $domainModel->read(['project_id' => $project_id, 'status' => 1]);
if ($domainInfo !== false) {
$dom = @file_get_html('https://' . $domainInfo['domain'] . '/');
if (empty($dom)) {
$this->output('获取HTML失败: ' . $project_id);
}else{
$banner_dom = $dom->find('main .section-banner-wrap-block img', 0);
$data[] = $banner_dom ? $banner_dom->src : '';
$dom->clear();
unset($dom);
$data[] = $banner_dom ? $banner_dom->src : '';
}
}else{
$this->output('域名不存在: ' . $project_id);
}
return $data;
}
/**
* @remark :记录日志
* @name :output
* @author :lyh
* @method :post
* @time :2025/5/8 9:57
*/
public function output($message, $log_file = 'logs/crop_image.log')
{
$message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
echo $message;
file_put_contents(storage_path($log_file), $message, FILE_APPEND);
return true;
}
}
... ...
... ... @@ -82,8 +82,8 @@ class AiBlogController extends BaseController
* @method :post
* @time :2023/7/5 14:33
*/
public function save(AiBlogRequest $aiBlogRequest,AiBlogLogic $aiBlogLogic){
$aiBlogRequest->validated();
public function save(AiBlogLogic $aiBlogLogic){
// $aiBlogRequest->validated();
$aiBlogLogic->blogSave();
$this->response('success');
}
... ...
... ... @@ -312,4 +312,83 @@ class CosService
'HelveticaNeue.dfont'
];
}
/**
* @remark :获取cos图片高度
* @name :cropAndUploadFromCos
* @author :lyh
* @method :post
* @time :2025/5/8 10:58
* @param :pathUrl->存储桶路径
*/
public function cropAndUploadFromCos($pathUrl,$maxHeight = 300){
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
try {
$result = $cosClient->HeadObject([
'Bucket' => $cos['bucket'],
'Key' => $pathUrl,
'CIProcess' => 'image/info',
]);
$info = simplexml_load_string($result['Body']);
$height = (int) $info->ImageHeight;
if($height > $maxHeight){
return $this->cropCosImage($pathUrl);
}
}catch (\Exception $e){
@file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
}
return true;
}
/**
* @remark :裁剪图片
* @name :cropCosImage
* @author :lyh
* @method :post
* @time :2025/5/8 11:06
*/
public function cropCosImage($cosUrl,$height = 200)
{
$cos = config('filesystems.disks.cos');
// 初始化 COS 客户端
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
// 定义 Pic-Operations JSON,用于裁剪并覆盖原图
$pathInfo = pathinfo($cosUrl);
$newKey = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-'. time() . $pathInfo['extension'];
$operations = [
'is_pic_info' => 0,
'rules' => [
[
// 注意 fileid 要 base64 编码,并与 Key 相同才能覆盖
'fileid' => base64_encode($newKey),
'rule' => 'imageMogr2/crop/x'.$height.'/gravity/center'
]
]
];
try {
// 执行裁剪并覆盖
$cosClient->ImageProcess([
'Bucket' => $cos['bucket'],
'Key' => $cosUrl, // 要处理的对象路径
'PicOperations' => json_encode($operations),
]);
return $newKey;
} catch (\Exception $e) {
@file_put_contents(storage_path('logs/crop_image_error.log'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
return '';
}
}
}
... ...