HandleNewsText.php
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* @remark :
* @name :HandleNewsText.php
* @author :lyh
* @method :post
* @time :2024/11/8 9:14
*/
namespace App\Console\Commands\Test;
use App\Models\News\News;
use App\Services\CosService;
use Illuminate\Console\Command;
use App\Models\Project\Project;
use App\Models\Template\BTemplateCom;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\DB;
class HandleNewsText extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'news_text';
/**
* The console command description.
*
* @var string
*/
protected $description = 'news_text';
/**
* Execute the job.
*
* @return void
*/
protected $project_id = 0;
public function handle()
{
$projectModel = new Project();
$list = $projectModel->list(['delete_status'=>0,'type'=>['!=',0]]);
$data = [];
foreach ($list as $v){
$this->project_id = $v['id'];
echo date('Y-m-d H:i:s') . 'project_id:'.$v['id'] . PHP_EOL;
ProjectServer::useProject($v['id']);
DB::connection('custom_mysql')->statement('DROP TABLE IF EXISTS gl_news_copy');
DB::connection('custom_mysql')->statement('CREATE TABLE gl_news_copy LIKE gl_news');
DB::connection('custom_mysql')->statement('INSERT INTO gl_news_copy SELECT * FROM gl_news');
$newsModel = new News();
$news_list = $newsModel->list(['status'=>['!=',2]],'id',['id','text']);
if(!empty($news_list)){
foreach ($news_list as $key => $values){
echo date('Y-m-d H:i:s') . '处理的数据id:'.$values['id'] . PHP_EOL;
$text = $this->handleText($values['text']);
$newsModel->edit(['text'=>$text],['id'=>$values['id']]);
}
}
DB::disconnect('custom_mysql');
}
echo date('Y-m-d H:i:s') . 'end' . PHP_EOL;
}
/**
* @remark :处理text
* @name :handleText
* @author :lyh
* @method :post
* @time :2024/11/7 17:20
*/
public function handleText($text){
$pattern = '/<img\s+[^>]*src=["\']([^"\']+)["\']/i';
$matches = [];
preg_match_all($pattern, $text, $matches);
$text = $this->saveBase64Images($matches[1],$text);
return ($text);
}
/**
* @remark :解码图片
* @name :saveBase64Images
* @author :lyh
* @method :post
* @time :2024/11/7 16:52
*/
public function saveBase64Images($imageSources,&$text)
{
foreach ($imageSources as $src) {
if (preg_match('/^data:image\/(png|jpg|jpeg|gif);base64,/', $src, $match)) {
echo date('Y-m-d H:i:s') . '当前数据包含base64图片流。' . PHP_EOL;
$imageType = $match[1]; // Image type (png, jpg, etc.)
$imageUrl = $this->manager_uploads($src,$imageType);
$text = str_replace($src, $imageUrl, $text);
}
}
return ($text);
}
/**
* @remark :自调用
* @name :manager_uploads
* @author :lyh
* @method :post
* @time :2024/11/7 11:00
*/
public function manager_uploads($files,$type = 'png'){
$this->uploads = config('upload.default_image');
$path = $this->uploads['path_b'].'/'.($this->project_id).'/image_news/'.date('Y-m');
$cosService = new CosService();
$fileName = md5(uniqid() . '_' . time() . '.' . ($this->project_id).rand(1,10000)) . '.' .$type;
return getImageUrl($cosService->uploadFile($files,$path,$fileName));
}
}