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