nystudio107 /
craft-imageoptimize
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * ImageOptimize 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\imagetransforms; |
||||||
| 12 | |||||||
| 13 | use Craft; |
||||||
| 14 | use craft\elements\Asset; |
||||||
| 15 | use craft\models\ImageTransform as CraftImageTransformModel; |
||||||
| 16 | use nystudio107\imageoptimize\ImageOptimize; |
||||||
| 17 | use nystudio107\imageoptimize\models\Settings; |
||||||
| 18 | |||||||
| 19 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 20 | * @author nystudio107 |
||||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||||
| 21 | * @package ImageOptimize |
||||||
|
0 ignored issues
–
show
|
|||||||
| 22 | * @since 1.6.0 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 23 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 24 | class CraftImageTransform extends ImageTransform |
||||||
| 25 | { |
||||||
| 26 | // Static Methods |
||||||
| 27 | // ========================================================================= |
||||||
| 28 | |||||||
| 29 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 30 | * @var bool |
||||||
| 31 | */ |
||||||
| 32 | public bool $generateTransformsBeforePageLoad; |
||||||
| 33 | |||||||
| 34 | // Public Properties |
||||||
| 35 | // ========================================================================= |
||||||
| 36 | |||||||
| 37 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 38 | * @inheritdoc |
||||||
| 39 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 40 | public static function displayName(): string |
||||||
| 41 | { |
||||||
| 42 | return Craft::t('image-optimize', 'Craft'); |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | // Public Methods |
||||||
| 46 | // ========================================================================= |
||||||
| 47 | |||||||
| 48 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 49 | * @inheritDoc |
||||||
| 50 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 51 | public function init(): void |
||||||
| 52 | { |
||||||
| 53 | /** @var Settings $settings */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 54 | $settings = ImageOptimize::$plugin->getSettings(); |
||||||
|
0 ignored issues
–
show
The method
getSettings() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 55 | // Get our $generateTransformsBeforePageLoad setting |
||||||
| 56 | $this->generateTransformsBeforePageLoad = $settings->generateTransformsBeforePageLoad ?? true; |
||||||
| 57 | } |
||||||
| 58 | |||||||
| 59 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 60 | * @inheritDoc |
||||||
| 61 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 62 | public function getTransformUrl(Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string |
||||||
| 63 | { |
||||||
| 64 | // Generate the URLs to the optimized images |
||||||
| 65 | $oldValue = Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad; |
||||||
| 66 | |||||||
| 67 | if ($this->generateTransformsBeforePageLoad) { |
||||||
| 68 | Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = true; |
||||||
| 69 | } |
||||||
| 70 | $url = $asset->getUrl($transform); |
||||||
| 71 | Craft::$app->getConfig()->getGeneral()->generateTransformsBeforePageLoad = $oldValue; |
||||||
| 72 | |||||||
| 73 | return $url; |
||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 77 | * @inheritDoc |
||||||
| 78 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 79 | public function getWebPUrl(string $url, Asset $asset, CraftImageTransformModel|string|array|null $transform): ?string |
||||||
| 80 | { |
||||||
| 81 | return $this->appendExtension($url, '.webp'); |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 85 | * @inheritdoc |
||||||
| 86 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 87 | public function getSettingsHtml(): ?string |
||||||
| 88 | { |
||||||
| 89 | $imageProcessors = ImageOptimize::$plugin->optimize->getActiveImageProcessors(); |
||||||
| 90 | $variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators(); |
||||||
| 91 | |||||||
| 92 | return Craft::$app->getView()->renderTemplate('craft-image-transform/settings/image-transforms/craft.twig', [ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 93 | 'imageTransform' => $this, |
||||||
| 94 | 'imageProcessors' => $imageProcessors, |
||||||
| 95 | 'variantCreators' => $variantCreators, |
||||||
| 96 | ]); |
||||||
|
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...
|
|||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | /** |
||||||
| 100 | * No savable fields for this component |
||||||
| 101 | * |
||||||
| 102 | * @return array |
||||||
| 103 | */ |
||||||
| 104 | public function fields(): array |
||||||
| 105 | { |
||||||
| 106 | return []; |
||||||
| 107 | } |
||||||
| 108 | |||||||
| 109 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 110 | * @inheritdoc |
||||||
| 111 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 112 | public function rules(): array |
||||||
| 113 | { |
||||||
| 114 | $rules = parent::rules(); |
||||||
| 115 | return array_merge($rules, [ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 116 | ]); |
||||||
|
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...
|
|||||||
| 117 | } |
||||||
| 118 | } |
||||||
| 119 |