Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start with removing webp checks, since it will be enabled by default #29

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/filament-media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
return [
'conversion' => LocalConversion::class,
'enable-format-generate-action' => true,
'enable-webp-generation' => true,
'extensions' => [
'image' => [
'jpg',
Expand Down
7 changes: 0 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
+ [Format definition](#format-definition)
* [Registering formats](#registering-formats)
+ [Preparing your model](#preparing-your-model)
* [Webp formats](#webp-formats)
- [Attachment model methods and attributes](#attachment-model-methods-and-attributes)
* [Methods](#methods)
+ [getStorage](#getstorage)
Expand Down Expand Up @@ -64,7 +63,6 @@ The basic config file consists of the following contents:
return [
'conversion' => \Codedor\MediaLibrary\Conversions\LocalConversion::class,
'enable-format-generate-action' => true,
'enable-webp-generation' => true,
'extensions' => [
'image' => [
'jpg',
Expand Down Expand Up @@ -185,11 +183,6 @@ public static function getFormats(Collection $formats): Collection
}
```

#### Webp formats

In the config you can enable webp formats. This will generate a webp versions of the formats and save them seperatly.
You can fetch a webp format by using `->getWebpFormatOrOriginal($format)` function.

## Attachment model methods and attributes

### Methods
Expand Down
30 changes: 10 additions & 20 deletions resources/views/components/picture.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,23 @@
<picture class="{{ $pictureClass }}">
@if ($formats)
@foreach ($formats as $breakpoint => $mobileFormat)
@if ($hasWebp)
<source
media="(max-width: {{ $breakpoint ?? '576' }}px)"
type="image/webp"
srcset="{{ $image->getWebpFormatOrOriginal($mobileFormat) }}"
>
@endif

<source
media="(max-width: {{ $breakpoint ?? '576' }}px)"
type="{{ $image->mime_type }}"
type="image/webp"
srcset="{{ $image->getFormatOrOriginal($mobileFormat) }}"
>
@endforeach
@endif

@if ($hasWebp)
<source
type="image/webp"
@if ($lazyload)
srcset="{{ $image->getWebpFormatOrOriginal($lazyloadInitialFormat) }}"
data-srcset="{{ $image->getWebpFormatOrOriginal($format) }}"
@else
srcset="{{ $image->getWebpFormatOrOriginal($format) }}"
@endif
>
@endif
<source
type="image/webp"
@if ($lazyload)
srcset="{{ $image->getFormatOrOriginal($lazyloadInitialFormat) }}"
data-srcset="{{ $image->getFormatOrOriginal($format) }}"
@else
srcset="{{ $image->getFormatOrOriginal($format) }}"
@endif
>

<img
alt="{{ $alt }}"
Expand Down
12 changes: 0 additions & 12 deletions src/Conversions/LocalConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\WebP;
use Illuminate\Support\Str;
use Spatie\Image\Image;

Expand Down Expand Up @@ -36,17 +35,6 @@ public function convert(Attachment $attachment, Format $format, bool $force = fa
->save($savePath);
}

if (
WebP::isEnabled() && (
$force ||
! $attachment->getStorage()->exists(WebP::path($savePath, $attachment->extension))
)
) {
Image::load($attachment->absolute_file_path)
->manipulate($format->definition())
->save(WebP::path($savePath, $attachment->extension));
}

return true;
}
}
2 changes: 1 addition & 1 deletion src/Formats/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final public function __construct(protected string $column = '')

public function filename(Attachment $attachment): string
{
return $this->prefix() . $attachment->file_name;
return $this->prefix() . Str::replaceLast('jpg', 'webp', $attachment->file_name);
jyrkidn marked this conversation as resolved.
Show resolved Hide resolved
}

public function prefix(): string
Expand Down
11 changes: 0 additions & 11 deletions src/Livewire/FormatterModal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\WebP;
use Filament\Notifications\Notification;
use Illuminate\Support\Collection;
use Intervention\Image\Facades\Image;
use Livewire\Attributes\On;
use Livewire\Component;

Expand Down Expand Up @@ -66,15 +64,6 @@ public function saveCrop($event)
$crop = base64_decode(str_replace(' ', '+', $crop));
$this->attachment->getStorage()->put("{$this->attachment->directory}/{$filename}", $crop);

if (WebP::isEnabled()) {
Image::make("{$this->attachment->absolute_directory_path}/{$filename}")
->encode('webp')
->save(WebP::path(
"{$this->attachment->absolute_directory_path}/{$filename}",
$this->attachment->extension
));
}

// Save the crop on the attachment, for later adjustments
$this->attachment->formats()->updateOrCreate([
'attachment_id' => $this->attachment->id,
Expand Down
14 changes: 0 additions & 14 deletions src/Models/Traits/HasFormats.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Codedor\MediaLibrary\Exceptions\FormatNotFound;
use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Models\AttachmentFormat;
use Codedor\MediaLibrary\WebP;
use Illuminate\Database\Eloquent\Relations\HasMany;

trait HasFormats
Expand Down Expand Up @@ -52,17 +51,4 @@ public function generateFormats(bool $force = false)
{
Formats::dispatchGeneration($this, $force);
}

public function getWebpFormatOrOriginal(?string $format): ?string
{
if (! $format) {
return $this->url;
}

if (! WebP::isEnabled()) {
return $this->getFormatOrOriginal($format);
}

return $this->getFormat($format, 'webp') ?? $this->getFormatOrOriginal($format);
}
}
14 changes: 0 additions & 14 deletions src/Views/Picture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\WebP;
use Illuminate\View\Component;

class Picture extends Component
{
public ?Format $formatClass = null;

public bool $hasWebp = false;

public function __construct(
public Attachment $image,
public ?string $format = null,
Expand All @@ -29,10 +26,6 @@ public function __construct(
if ($this->format) {
$this->getFormatClass();
}

if ($image->exists) {
$this->hasWebp = method_exists($image, 'getWebpFormatOrOriginal') && $image->getWebpFormatOrOriginal($format);
}
}

protected function getFormatClass()
Expand Down Expand Up @@ -76,13 +69,6 @@ public function getDimension(string $dimension): ?int
$filename = $this->formatClass->filename($this->image);
$path = "{$this->image->absolute_directory_path}/{$filename}";

if (WebP::isEnabled()) {
$path = WebP::path(
$path,
$this->image->extension
);
}

if (file_exists($path)) {
$dimensions = getimagesize($path);

Expand Down
18 changes: 0 additions & 18 deletions src/WebP.php

This file was deleted.

4 changes: 0 additions & 4 deletions tests/Mixins/UploadedFileMixinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Codedor\MediaLibrary\Jobs\GenerateAttachmentFormat;
use Codedor\MediaLibrary\Models\Attachment;
use Codedor\MediaLibrary\Tests\TestFormats\TestHero;
use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue;
Expand All @@ -20,7 +19,6 @@

\Codedor\MediaLibrary\Facades\Formats::register([
TestHero::class,
TestHeroWebp::class,
]);

$file = UploadedFile::fake()->image('test.jpg', 100, 100);
Expand All @@ -40,7 +38,6 @@

\Codedor\MediaLibrary\Facades\Formats::register([
TestHero::class,
TestHeroWebp::class,
]);

assertDatabaseCount(Attachment::class, 0);
Expand Down Expand Up @@ -75,7 +72,6 @@

\Codedor\MediaLibrary\Facades\Formats::register([
TestHero::class,
TestHeroWebp::class,
]);

Storage::fake($disk);
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function setUp(): void
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
config()->set('filament-media-library.enable-webp-generation', false);
}

protected function getPackageProviders($app)
Expand Down
27 changes: 0 additions & 27 deletions tests/TestFormats/TestHeroWebp.php

This file was deleted.

10 changes: 4 additions & 6 deletions tests/Unit/Collections/FormatsCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Codedor\MediaLibrary\Facades\Formats;
use Codedor\MediaLibrary\Formats\Format;
use Codedor\MediaLibrary\Tests\TestFormats\TestHero;
use Codedor\MediaLibrary\Tests\TestFormats\TestHeroWebp;
use Codedor\MediaLibrary\Tests\TestModels\TestModel;

it('registers model', function () {
Expand All @@ -16,7 +15,7 @@
});

it('returns format if format is registered', function () {
Formats::register([TestHero::class, TestHeroWebp::class]);
Formats::register([TestHero::class]);

expect(Formats::exists('test-hero'))
->toBeInstanceOf(TestHero::class);
Expand All @@ -26,16 +25,15 @@
});

it('returns null if format is not registered', function () {
Formats::register([TestHero::class, TestHeroWebp::class]);
Formats::register([TestHero::class]);

expect(Formats::exists('format-does-not-exist'))
->toBeNull();
});

it('returns collection with kebab keys', function () {
Formats::register([TestHero::class, TestHeroWebp::class]);
Formats::register([TestHero::class]);

expect(Formats::mapToKebab())
->toHaveKey('test-hero')
->toHaveKey('test-hero-webp');
->toHaveKey('test-hero');
});
Loading