diff --git a/composer.json b/composer.json index 150aaf4..47ebbf1 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ } }, "require": { + "php": "^8.0", "statamic/cms": "^3.4" }, "config": { diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..51df618 --- /dev/null +++ b/config/config.php @@ -0,0 +1,58 @@ + [ + 'default' => 0, + 'sm' => 640, + 'md' => 768, + 'lg' => 1024, + 'xl' => 1280, + '2xl' => 1536, + ], + + /* + |-------------------------------------------------------------------------- + | Size multipliers + |-------------------------------------------------------------------------- + | + | Define in which multiples of the requested image size should sources be + | generated when using the `sizes` attribute. + | + */ + + 'size_multipliers' => [1, 1.5, 2], + + /* + |-------------------------------------------------------------------------- + | Device pixel ratios + |-------------------------------------------------------------------------- + | + | Define for which DPRs should sources be generated when *not* using the + | `sizes` attribute. Use int or float values, e. g. for 2x => 2 + | + */ + + 'dpr' => [1, 2], + + /* + |-------------------------------------------------------------------------- + | Supported filetypes + |-------------------------------------------------------------------------- + | + | Define the supported filetypes for image processing. + | + */ + + 'supported_filetypes' => ['jpg', 'jpeg', 'png', 'webp'], + +]; \ No newline at end of file diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6fd04d9..77a3408 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -3,6 +3,7 @@ namespace VV\Picturesque; use Statamic\Providers\AddonServiceProvider; +use Statamic\Statamic; class ServiceProvider extends AddonServiceProvider { @@ -12,6 +13,18 @@ class ServiceProvider extends AddonServiceProvider public function bootAddon() { - // + parent::boot(); + + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'picturesque'); + + if ($this->app->runningInConsole()) { + $this->publishes([ + __DIR__.'/../config/config.php' => config_path('picturesque.php'), + ], 'picturesque'); + } + + Statamic::afterInstalled(function ($command) { + $command->call('vendor:publish', ['--tag' => 'picturesque']); + }); } } diff --git a/src/Tags/Picture.php b/src/Tags/Picture.php index 3d52b85..99efba3 100644 --- a/src/Tags/Picture.php +++ b/src/Tags/Picture.php @@ -23,23 +23,6 @@ class Picture extends Tags private $mode = 'html'; - // CONFIG - private $breakpoints = [ - 'default' => 0, - 'sm' => 640, - 'md' => 768, - 'lg' => 1024, - 'xl' => 1280, - '2xl' => 1536, - ]; - - private $sizeMultipliers = [1, 1.5, 2]; - - private $dpr = [1, 2]; - - private $supportedFiletypes = ['jpg', 'jpeg', 'png', 'webp']; - // ENDCONFIG - /** * {{ picture src="[src]" }}. * @@ -118,7 +101,7 @@ private function output($asset) */ // breakpoint-based sources - if ($this->params->get(array_keys($this->breakpoints))) { + if ($this->params->get(array_keys(config('picturesque.breakpoints')))) { $this->data['sources'] = $this->generateBreakpointSourceTags(); } @@ -248,7 +231,7 @@ private function generateSourceTag(array|string $sourceData, string $format = 'w // media if ($breakpoint) { - $source['media'] = "(min-width: {$this->breakpoints[$breakpoint]}px)"; + $source['media'] = "(min-width: ".config('picturesque.breakpoints')[$breakpoint]."px)"; } // srcset @@ -264,7 +247,7 @@ private function generateSourceTag(array|string $sourceData, string $format = 'w private function generateBreakpointSourceTags(): array { - return collect($this->breakpoints) + return collect(config('picturesque.breakpoints')) ->sortDesc() ->map(function ($px, $breakpoint) { if ($this->params->get($breakpoint)) { @@ -296,7 +279,7 @@ private function generateSrcsetAttribute(array $sourceData, string $format, $gli foreach ($sourceData['srcset'] as $source) { // with sizes if ($sourceData['sizes']) { - foreach ($this->sizeMultipliers as $index => $multiplier) { + foreach (config('picturesque.size_multipliers') as $multiplier) { $w = ((float) $source['width']) * $multiplier; // make sure to not generate a size twice @@ -318,7 +301,7 @@ private function generateSrcsetAttribute(array $sourceData, string $format, $gli } // with dpr else { - foreach ($this->dpr as $dpr) { + foreach (config('picturesque.dpr') as $dpr) { $w = ((float) $source['width']) * $dpr; $glideOptions['width'] = $w; @@ -465,6 +448,6 @@ private function checkIfGlideSupportedFileType(): bool|null } $filetype = strtolower(explode('/', $this->sourceAsset->meta()['mime_type'])[1]); - return in_array($filetype, $this->supportedFiletypes); + return in_array($filetype, config('picturesque.supported_filetypes')); } }