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