Skip to content

Commit

Permalink
feat: use dedicated config file
Browse files Browse the repository at this point in the history
  • Loading branch information
simonerd committed Mar 10, 2023
1 parent 8c8b3a3 commit fc5ab26
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
}
},
"require": {
"php": "^8.0",
"statamic/cms": "^3.4"
},
"config": {
Expand Down
58 changes: 58 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Breakpoints
|--------------------------------------------------------------------------
|
| Define supported breakpoints for generating breakpoint-based sources.
|
*/

'breakpoints' => [
'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'],

];
15 changes: 14 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace VV\Picturesque;

use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -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']);
});
}
}
29 changes: 6 additions & 23 deletions src/Tags/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]" }}.
*
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit fc5ab26

Please sign in to comment.