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