Skip to content

Commit 25510ab

Browse files
committed
media conversions
1 parent 7a91513 commit 25510ab

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

packages/media/src/Models/Media.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,31 @@ public function collection()
5454

5555
public function registerMediaConversions(?BaseMedia $media = null): void
5656
{
57-
$this->addMediaConversion('preview')
57+
$this->addMediaConversion('thumbnail')
5858
->nonQueued()
59-
->fit(Fit::Contain, 300, 300);
59+
->fit(Fit::Contain, 150, 150);
6060

61-
$this->addMediaConversion('thumb')
61+
$this->addMediaConversion('preview')
6262
->nonQueued()
63-
->fit(Fit::Contain, 150, 150);
63+
->width(300);
6464

65-
$this->addMediaConversion('medium')
65+
$this->addMediaConversion('medium_large')
6666
->nonQueued()
67-
->fit(Fit::Contain, 800, 600);
67+
->width(768);
6868

6969
$this->addMediaConversion('large')
7070
->nonQueued()
71-
->fit(Fit::Contain, 1200, 900)
71+
->width(1024)
72+
->quality(80);
73+
74+
$this->addMediaConversion('1536')
75+
->nonQueued()
76+
->width(1536)
77+
->quality(80);
78+
79+
$this->addMediaConversion('2048')
80+
->nonQueued()
81+
->width(2048)
7282
->quality(80);
7383
}
7484

packages/media/src/Resources/MediaResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static function form(Schema $schema): Schema
7070
return $schema->schema([
7171
SpatieMediaLibraryFileUpload::make('image')
7272
->columnSpanFull()
73+
->conversion('thumbnail')
7374
->collection(function ($record) {
7475
$mediaCollection = $record->collection_name;
7576

packages/media/src/Tables/Columns/CustomImageColumn.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class CustomImageColumn extends ImageColumn
1111
{
12+
protected ?string $conversion = null;
13+
1214
private array $iconMap = [
1315
'application/pdf' => '/vendor/file-icons/pdf.svg',
1416
'application/msword' => '/vendor/file-icons/doc.svg',
@@ -67,7 +69,9 @@ public function getState(): mixed
6769
}
6870

6971
if ($record instanceof Media) {
70-
return $record->getUrl();
72+
return $this->conversion
73+
? $record->getUrl($this->conversion)
74+
: $record->getUrl();
7175
}
7276

7377
$mediaIds = MediaUsable::where('media_usable_id', $record->getKey())
@@ -93,7 +97,9 @@ public function getImageUrl(?string $state = null): ?string
9397

9498
if ($record instanceof Media) {
9599
if (str_starts_with($record->mime_type, 'image/')) {
96-
return $record->getUrl();
100+
return $this->conversion
101+
? $record->getUrl($this->conversion)
102+
: $record->getUrl();
97103
}
98104

99105
return $this->iconMap[$record->mime_type] ?? '/vendor/file-icons/svg/unknown.svg';
@@ -116,9 +122,18 @@ public function getImageUrl(?string $state = null): ?string
116122
}
117123

118124
if (str_starts_with($media->mime_type, 'image/')) {
119-
return $media->getUrl();
125+
return $this->conversion
126+
? $media->getUrl($this->conversion)
127+
: $media->getUrl();
120128
}
121129

122130
return $this->iconMap[$media->mime_type] ?? '/vendor/file-icons/svg/unknown.svg';
123131
}
132+
133+
public function conversion(?string $conversion): static
134+
{
135+
$this->conversion = $conversion;
136+
137+
return $this;
138+
}
124139
}

0 commit comments

Comments
 (0)