-
Notifications
You must be signed in to change notification settings - Fork 554
/
RefreshLQIP.php
executable file
·89 lines (73 loc) · 2.81 KB
/
RefreshLQIP.php
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
<?php
namespace A17\Twill\Commands;
use A17\Twill\Models\Media;
use A17\Twill\Services\MediaLibrary\Glide;
use A17\Twill\Services\MediaLibrary\ImageService;
use Illuminate\Config\Repository as Config;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Arr;
class RefreshLQIP extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'twill:lqip {--all=0}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Refresh Low Quality Image Placeholders.';
/**
* @var DatabaseManager
*/
protected $db;
/**
* @var Config
*/
protected $config;
protected $cropParamsKeys = [
'crop_x',
'crop_y',
'crop_w',
'crop_h',
];
/**
* @param DatabaseManager $db
* @param Config $config
*/
public function __construct(DatabaseManager $db, Config $config)
{
parent::__construct();
$this->db = $db;
$this->config = $config;
}
// TODO: document this and actually think about moving to queuable job after content type updates
public function handle()
{
$this->db->table(config('twill.mediables_table', 'twill_mediables'))->orderBy('id')->chunk(100, function ($attached_medias) {
foreach ($attached_medias as $attached_media) {
$uuid = Media::withTrashed()->find($attached_media->media_id, ['uuid'])->uuid;
$lqip_width = $this->config->get('lqip.' . $attached_media->mediable_type . '.' . $attached_media->role . '.' . $attached_media->crop, 30);
if ($lqip_width && (!$attached_media->lqip_data || $this->option('all'))) {
$crop_params = Arr::only((array) $attached_media, $this->cropParamsKeys);
$imageService = config('twill.media_library.image_service');
$url = ImageService::getLQIPUrl($uuid, $crop_params + ['w' => $lqip_width]);
if (($imageService === Glide::class) && !config('twill.glide.base_url')) {
$this->error('Cannot generate LQIP. Missing glide base url. Please set GLIDE_BASE_URL in your .env');
return;
}
try {
$data = file_get_contents($url);
$dataUri = 'data:image/gif;base64,' . base64_encode($data);
$this->db->table(config('twill.mediables_table', 'twill_mediables'))->where('id', $attached_media->id)->update(['lqip_data' => $dataUri]);
} catch (\Exception $e) {
$this->info("LQIP was not generated for $uuid because {$e->getMessage()}");
}
}
}
});
}
}