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 Methods |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create all of the OptimizedImages Field variants by creating all of the |
37
|
|
|
* responsive image variant transforms |
38
|
|
|
* |
39
|
|
|
* @param string|null $volumeHandle |
|
|
|
|
40
|
|
|
* |
41
|
|
|
* @throws \yii\base\InvalidConfigException |
42
|
|
|
*/ |
|
|
|
|
43
|
|
|
public function actionCreate($volumeHandle = null) |
44
|
|
|
{ |
45
|
|
|
echo 'Creating optimized image variants'.PHP_EOL; |
46
|
|
|
|
47
|
|
|
if ($volumeHandle === null) { |
48
|
|
|
// Re-save all of the optimized image variants in all volumes |
49
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
50
|
|
|
} else { |
51
|
|
|
// Re-save all of the optimized image variants in a specific volume |
52
|
|
|
$volumes = Craft::$app->getVolumes(); |
53
|
|
|
$volume = $volumes->getVolumeByHandle($volumeHandle); |
54
|
|
|
if ($volume) { |
55
|
|
|
/** @var Volume $volume */ |
|
|
|
|
56
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
57
|
|
|
} else { |
58
|
|
|
echo 'Unknown Asset Volume handle: '.$volumeHandle.PHP_EOL; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$this->runCraftQueue(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a single OptimizedImage for the passed in Asset ID |
66
|
|
|
* |
67
|
|
|
* @param int|null $id |
|
|
|
|
68
|
|
|
*/ |
|
|
|
|
69
|
|
|
public function actionCreateAsset($id = null) |
70
|
|
|
{ |
71
|
|
|
echo 'Creating optimized image variants'.PHP_EOL; |
72
|
|
|
|
73
|
|
|
if ($id === null) { |
74
|
|
|
echo 'No Asset ID specified'.PHP_EOL; |
75
|
|
|
} else { |
76
|
|
|
// Re-save a single Asset ID |
77
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAsset($id); |
78
|
|
|
} |
79
|
|
|
$this->runCraftQueue(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* |
84
|
|
|
*/ |
|
|
|
|
85
|
|
|
private function runCraftQueue() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
// This might take a while |
88
|
|
|
App::maxPowerCaptain(); |
89
|
|
|
$queue = Craft::$app->getQueue(); |
90
|
|
|
if ($queue instanceof QueueInterface) { |
|
|
|
|
91
|
|
|
$queue->run(); |
92
|
|
|
} elseif ($queue instanceof RedisQueue) { |
93
|
|
|
$queue->run(false); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|