| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- // 1. 正确的队列任务类 application/job/ImageJob.php
- namespace app\service;
- use think\Db;
- use think\Queue;
- class ImageService
- {
- public function handleImage($params) {
- // 如果是单条,转为数组
- if(!isset($params["batch"])){
- return false;
- }
- $arr = [];
- $batch = $params["batch"];
- $num = $params["num"];
- //进行数据处理
- $j = 0;
- foreach ($batch as $k => $v) {
- $baseItem = [
- "sourceDir" => $this->sourceDir($v, 1) ?? '',
- "outputDir" => $this->sourceDir($v, 2) ?? '',
- "file_name" => $this->sourceDir($v, 3) ?? '',
- "type" => $params['type'] ?? '',
- "prompt" => $params['prompt'] ?? '',
- "width" => $params['width'] ?? 512,
- "height" => $params['height'] ?? 512
- ];
- // 创建$num个相同的项目并合并到$arr
- $arr = array_merge($arr, array_fill(0, $num, $baseItem));
- }
- // 推送队列(启用时请去掉 die)
- //整体进行推送
- Queue::push('app\job\ImageArrJob', $arr,"arrimage");
- }
- public function sourceDir($filePath,$type){
- $arr = [];
- // 使用正则表达式匹配完整路径
- if (preg_match('/^(.+?)\/Preview\/(\d{8})\/(.+)$/', $filePath, $matches)) {
- $arr = [
- 'basePath' => $matches[1], // uploads/operate/ai
- 'date' => $matches[2], // 20250522
- 'filename' => $matches[3] // 333 (89).jpg
- ];
- }else{
- // 备用方案:如果正则匹配失败
- $pathParts = explode('/', $filePath);
- $filename = array_pop($pathParts);
- $date = '';
- $baseParts = [];
- foreach ($pathParts as $part) {
- if (preg_match('/^\d{8}$/', $part)) {
- $date = $part;
- break;
- }
- $baseParts[] = $part;
- }
- $arr = [
- 'basePath' => implode('/', $baseParts),
- 'date' => $date,
- 'filename' => $filename
- ];
- }
- if($type==1){
- // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
- return $arr["basePath"]."/Preview/".$arr["date"];
- }
- if($type==2){
- // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
- return '/'.$arr["basePath"]."/dall-e/".$arr["date"];
- }
- if($type==3){
- // 使用正则表达式匹配路径中的日期部分(YYYYMMDD)
- return $arr["filename"];
- }
- }
- }
|