Skip to content

Commit

Permalink
Added style action
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 committed Jun 20, 2024
1 parent fc65caf commit ead5b2a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: style

on:
push:
branches:
- main
jobs:
style:
name: Style
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
coverage: none

- name: Install dependencies
run: composer install

- name: Style
run: composer fix-style

- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling changes
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
}
],
"scripts": {
"analyse": "phpstan"
"analyse": "phpstan",
"fix-style": "pint"
},
"minimum-stability": "dev"
}
2 changes: 1 addition & 1 deletion config/glide-directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Set 'webp' for WebP only.
// Set 'mime_type' for the original image mime type.
// Set 'both' to use both sources.
'sources' => 'webp'
'sources' => 'webp',
];
23 changes: 11 additions & 12 deletions src/Responsive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace JustBetter\GlideDirective;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Statamic\Assets\Asset;
use Statamic\Statamic;
use Illuminate\Contracts\View\View;
use Illuminate\Contracts\View\Factory;

class Responsive
{
Expand All @@ -30,7 +30,7 @@ public static function getPresets(Asset $image): array
{
$config = config('statamic.assets.image_manipulation.presets');

if (!config('justbetter.glide-directive.placeholder') && isset($config['placeholder'])) {
if (! config('justbetter.glide-directive.placeholder') && isset($config['placeholder'])) {
unset($config['placeholder']);
}

Expand All @@ -48,7 +48,7 @@ public static function getPresets(Asset $image): array

$index = 0;
foreach ($configPresets as $preset => $data) {
$size = $data['w'] . 'w';
$size = $data['w'].'w';

if ($index < (count($configPresets) - 1)) {
$size .= ', ';
Expand All @@ -57,18 +57,17 @@ public static function getPresets(Asset $image): array
if (self::canUseWebpSource()) {
$glideUrl = Statamic::tag($preset === 'placeholder' ? 'glide:data_url' : 'glide')->params(['preset' => $preset, 'src' => $image->url(), 'format' => 'webp', 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
$presets['webp'] .= $glideUrl . ' ' . $size;
$presets['webp'] .= $glideUrl.' '.$size;
}
}

if (self::canUseMimeTypeSource()) {
$glideUrl = Statamic::tag($preset === 'placeholder' ? 'glide:data_url' : 'glide')->params(['preset' => $preset, 'src' => $image->url(), 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
$presets[$image->mimeType()] .= $glideUrl . ' ' . $size;
$presets[$image->mimeType()] .= $glideUrl.' '.$size;
}
}


if ($preset === 'placeholder') {
$glideUrl = Statamic::tag('glide:data_url')->params(['preset' => 'placeholder', 'src' => $image->url(), 'fit' => $data['crop'] ?? 'crop_focal'])->fetch();
if ($glideUrl) {
Expand All @@ -79,7 +78,7 @@ public static function getPresets(Asset $image): array
$index++;
}

if (!isset($presets['placeholder'])) {
if (! isset($presets['placeholder'])) {
$glideUrl = Statamic::tag('glide:data_url')->params(['preset' => collect($configPresets)->keys()->first(), 'src' => $image->url(), 'fit' => 'crop_focal'])->fetch();
$presets['placeholder'] = $glideUrl;
}
Expand All @@ -95,12 +94,12 @@ protected static function getPresetsByRatio(Asset $image, array $config): array
// filter config based on aspect ratio
// if ratio < 1 get all presets with a height bigger than the width, else get all presets with width equal or bigger than the height.
if ($ratio < 0.999) {
$presets = $presets->filter(fn($preset, $key) => $preset['h'] > $preset['w']);
$presets = $presets->filter(fn ($preset, $key) => $preset['h'] > $preset['w']);
if ($presets->isNotEmpty() && isset($config['placeholder'])) {
$presets->prepend($config['placeholder'], 'placeholder');
}
} else {
$presets = $presets->filter(fn($preset, $key) => $key === 'placeholder' || $preset['w'] >= $preset['h']);
$presets = $presets->filter(fn ($preset, $key) => $key === 'placeholder' || $preset['w'] >= $preset['h']);
}

return $presets->isNotEmpty() ? $presets->toArray() : $config;
Expand All @@ -111,9 +110,9 @@ protected static function getAttributeBag(array $arguments): string
$excludedAttributes = ['src', 'class', 'alt', 'width', 'height', 'onload'];

return collect($arguments)
->filter(fn ($value, $key) => !in_array($key, $excludedAttributes))
->filter(fn ($value, $key) => ! in_array($key, $excludedAttributes))
->map(function ($value, $key) {
return $key . '="' . $value . '"';
return $key.'="'.$value.'"';
})->implode(' ');
}

Expand Down
9 changes: 4 additions & 5 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public function boot(): void

protected function bootConfig(): static
{
$this->mergeConfigFrom(__DIR__ . '/../config/glide-directive.php', 'justbetter.glide-directive');
$this->mergeConfigFrom(__DIR__.'/../config/glide-directive.php', 'justbetter.glide-directive');

$this->publishes([
__DIR__ . '/../config/glide-directive.php' => config_path('glide-directive.php'),
__DIR__.'/../config/glide-directive.php' => config_path('glide-directive.php'),
], 'config');


if (empty(config('statamic.assets.image_manipulation.presets'))) {
config()->set('statamic.assets.image_manipulation.presets', config('justbetter.glide-directive.presets'));
}
Expand All @@ -43,7 +42,7 @@ protected function bootDirectives(): static

protected function bootViews(): static
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'statamic-glide-directive');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'statamic-glide-directive');

return $this;
}
Expand All @@ -54,4 +53,4 @@ protected function bootClasses(): static

return $this;
}
}
}

0 comments on commit ead5b2a

Please sign in to comment.