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\helpers\FileHelper; |
19
|
|
|
use craft\utilities\ClearCaches; |
20
|
|
|
|
21
|
|
|
use yii\console\Controller; |
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 might take a while |
62
|
|
|
App::maxPowerCaptain(); |
63
|
|
|
Craft::$app->getQueue()->run(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|