|
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\elements\Asset; |
|
18
|
|
|
use craft\helpers\App; |
|
19
|
|
|
use craft\helpers\FileHelper; |
|
20
|
|
|
use craft\utilities\ClearCaches; |
|
21
|
|
|
|
|
22
|
|
|
use yii\console\Controller; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Optimize Command |
|
26
|
|
|
* |
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
28
|
|
|
* @package ImageOptimize |
|
|
|
|
|
|
29
|
|
|
* @since 1.2.0 |
|
|
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
|
class OptimizeController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
// Public Methods |
|
34
|
|
|
// ========================================================================= |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create all of the OptimizedImages Field variants by creating all of the |
|
38
|
|
|
* responsive image variant transforms |
|
39
|
|
|
* |
|
40
|
|
|
* @param string|null $volumeHandle |
|
|
|
|
|
|
41
|
|
|
* |
|
42
|
|
|
* @throws \yii\base\InvalidConfigException |
|
43
|
|
|
*/ |
|
|
|
|
|
|
44
|
|
|
public function actionCreate($volumeHandle = null) |
|
45
|
|
|
{ |
|
46
|
|
|
echo 'Creating optimized image variants'.PHP_EOL; |
|
47
|
|
|
|
|
48
|
|
|
if ($volumeHandle === null) { |
|
49
|
|
|
// Re-save all of the optimized image variants in all volumes |
|
50
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets(); |
|
51
|
|
|
} else { |
|
52
|
|
|
// Re-save all of the optimized image variants in a specific volume |
|
53
|
|
|
$volumes = Craft::$app->getVolumes(); |
|
54
|
|
|
$volume = $volumes->getVolumeByHandle($volumeHandle); |
|
55
|
|
|
if ($volume) { |
|
56
|
|
|
/** @var Volume $volume */ |
|
|
|
|
|
|
57
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume); |
|
58
|
|
|
} else { |
|
59
|
|
|
echo 'Unknown Asset Volume handle: '.$volumeHandle.PHP_EOL; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
// This might take a while |
|
63
|
|
|
App::maxPowerCaptain(); |
|
64
|
|
|
Craft::$app->getQueue()->run(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Clear the Asset transform index cache tables, to force the re-creation of transformed images |
|
69
|
|
|
*/ |
|
|
|
|
|
|
70
|
|
|
public function actionClear() |
|
71
|
|
|
{ |
|
72
|
|
|
foreach (ClearCaches::cacheOptions() as $cacheOption) { |
|
73
|
|
|
if ($cacheOption['key'] !== 'transform-indexes') { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$action = $cacheOption['action']; |
|
78
|
|
|
|
|
79
|
|
|
if (\is_string($action)) { |
|
80
|
|
|
try { |
|
81
|
|
|
FileHelper::clearDirectory($action); |
|
82
|
|
|
} catch (\Throwable $e) { |
|
83
|
|
|
Craft::warning("Could not clear the directory {$action}: ".$e->getMessage(), __METHOD__); |
|
84
|
|
|
} |
|
85
|
|
|
} elseif (isset($cacheOption['params'])) { |
|
86
|
|
|
\call_user_func_array($action, $cacheOption['params']); |
|
87
|
|
|
} else { |
|
88
|
|
|
$action(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Individually queue all assets for optimizing. We run the query |
|
95
|
|
|
* in small batches so we never hit a memory limit. |
|
96
|
|
|
* |
|
97
|
|
|
*/ |
|
|
|
|
|
|
98
|
|
|
public function actionQueueAssets() |
|
99
|
|
|
{ |
|
100
|
|
|
$query = Asset::find(); |
|
101
|
|
|
$batchSize = 100; |
|
102
|
|
|
$assetCount = $query->count(); |
|
103
|
|
|
$numberOfBatches = ceil($assetCount / $batchSize); |
|
104
|
|
|
$query->limit($batchSize); |
|
105
|
|
|
$queue = Craft::$app->getQueue(); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
for ($i = 0; $i < $numberOfBatches; ++$i) { |
|
108
|
|
|
$query->offset = $batchSize * $i; |
|
109
|
|
|
|
|
110
|
|
|
foreach ($query->all() as $asset) { |
|
111
|
|
|
ImageOptimize::$plugin->optimizedImages->resaveAsset($asset->id); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|