1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image Optimize plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\console\controllers; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Field; |
15
|
|
|
use craft\helpers\App; |
16
|
|
|
use craft\queue\QueueInterface; |
17
|
|
|
use nystudio107\imageoptimize\ImageOptimize; |
18
|
|
|
use yii\base\InvalidConfigException; |
19
|
|
|
use yii\console\Controller; |
20
|
|
|
use yii\queue\redis\Queue as RedisQueue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Optimize Command |
24
|
|
|
* |
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package ImageOptimize |
|
|
|
|
27
|
|
|
* @since 1.2.0 |
|
|
|
|
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class OptimizeController extends Controller |
30
|
|
|
{ |
31
|
|
|
// Public Properties |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @var bool Whether image variants should be forced to recreated, even if they already exist on disk |
|
|
|
|
36
|
|
|
* @since 1.6.18 |
37
|
|
|
*/ |
38
|
|
|
public bool $force = false; |
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var string|null Only re-save image variants associated with this field handle |
|
|
|
|
42
|
|
|
* @since 1.6.18 |
43
|
|
|
*/ |
44
|
|
|
public ?string $field = null; |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* @var bool Should the image generation simply be queued, rather than run immediately? |
48
|
|
|
*/ |
49
|
|
|
public bool $queue = false; |
50
|
|
|
|
51
|
|
|
// Public Methods |
52
|
|
|
// ========================================================================= |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
public function options($actionID): array |
58
|
|
|
{ |
59
|
|
|
$options = parent::options($actionID); |
60
|
|
|
return array_merge($options, [ |
|
|
|
|
61
|
|
|
'force', |
62
|
|
|
'field', |
63
|
|
|
'queue', |
64
|
|
|
]); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Create all the OptimizedImages Field variants by creating all the responsive image variant transforms |
69
|
|
|
* |
70
|
|
|
* @param ?string $volumeHandle |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @throws InvalidConfigException |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public function actionCreate(?string $volumeHandle = null): void |
75
|
|
|
{ |
76
|
|
|
echo 'Creating optimized image variants' . PHP_EOL; |
77
|
|
|
if ($this->force) { |
78
|
|
|
echo 'Forcing optimized image variants creation via --force' . PHP_EOL; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$fieldId = null; |
82
|
|
|
if ($this->field !== null) { |
83
|
|
|
/** @var ?Field $field */ |
|
|
|
|
84
|
|
|
$field = Craft::$app->getFields()->getFieldByHandle($this->field); |
85
|
|
|
$fieldId = $field?->id; |
86
|
|
|
} |
87
|
|
|
if ($volumeHandle === null) { |
88
|
|
|
// Re-save all the optimized image variants in all volumes |
89
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets($fieldId, $this->force); |
90
|
|
|
} else { |
91
|
|
|
// Re-save all the optimized image variants in a specific volume |
92
|
|
|
$volumes = Craft::$app->getVolumes(); |
93
|
|
|
$volume = $volumes->getVolumeByHandle($volumeHandle); |
94
|
|
|
if ($volume) { |
95
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume, $fieldId, $this->force); |
96
|
|
|
} else { |
97
|
|
|
echo 'Unknown Asset Volume handle: ' . $volumeHandle . PHP_EOL; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
if (!$this->queue) { |
101
|
|
|
$this->runCraftQueue(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create a single OptimizedImage for the passed in Asset ID |
107
|
|
|
* |
108
|
|
|
* @param ?int $id |
|
|
|
|
109
|
|
|
*/ |
|
|
|
|
110
|
|
|
public function actionCreateAsset(?int $id = null): void |
111
|
|
|
{ |
112
|
|
|
echo 'Creating optimized image variants' . PHP_EOL; |
113
|
|
|
|
114
|
|
|
if ($id === null) { |
115
|
|
|
echo 'No Asset ID specified' . PHP_EOL; |
116
|
|
|
} else { |
117
|
|
|
// Re-save a single Asset ID |
118
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAsset($id, $this->force); |
119
|
|
|
} |
120
|
|
|
if (!$this->queue) { |
121
|
|
|
$this->runCraftQueue(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
|
|
|
|
126
|
|
|
* |
127
|
|
|
*/ |
|
|
|
|
128
|
|
|
private function runCraftQueue(): void |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
// This might take a while |
131
|
|
|
App::maxPowerCaptain(); |
132
|
|
|
$queue = Craft::$app->getQueue(); |
133
|
|
|
if ($queue instanceof QueueInterface) { |
134
|
|
|
$queue->run(); |
135
|
|
|
} elseif ($queue instanceof RedisQueue) { |
136
|
|
|
$queue->run(false); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|