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

Fix Htmlable render media #2864

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ parameters:
count: 2
path: src/Conversions/ConversionCollection.php

-
message: "#^Access to an undefined property mixed\\:\\:\\$mediaConversions\\.$#"
count: 1
path: src/Conversions/ConversionCollection.php

-
message: "#^Method Spatie\\\\MediaLibrary\\\\Conversions\\\\ConversionCollection\\:\\:addManipulationToConversion\\(\\) has no return type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1495,11 +1490,6 @@ parameters:
count: 1
path: src/MediaCollections/Models/Media.php

-
message: "#^Property Spatie\\\\MediaLibrary\\\\MediaCollections\\\\Models\\\\Media\\:\\:\\$casts type has no value type specified in iterable type array\\.$#"
count: 1
path: src/MediaCollections/Models/Media.php

-
message: "#^Unable to resolve the template type TKey in call to function collect$#"
count: 1
Expand Down
5 changes: 4 additions & 1 deletion src/MediaCollections/HtmlableMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Spatie\MediaLibrary\Conversions\ConversionCollection;
use Spatie\MediaLibrary\Conversions\ImageGenerators\Image;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class HtmlableMedia implements Htmlable, \Stringable
Expand Down Expand Up @@ -43,7 +44,9 @@ public function lazy(): self

public function toHtml()
{
if (! (new Image())->canHandleMime($this->media->mime_type)) {
$imageGenerator = ImageGeneratorFactory::forMedia($this->media) ?? new Image();

if (! $imageGenerator->canHandleMime($this->media->mime_type)) {
return '';
}

Expand Down
13 changes: 12 additions & 1 deletion tests/Feature/Media/ToHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@

test('converting a non image to an image tag will not blow up', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestPdf())
->addMedia($this->getTestTiff())
->toMediaCollection();

expect($media->img())->toEqual('');
});

it('can render pdf thumbnail as an image', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestPdf())
->toMediaCollection();

$this->assertEquals(
"<img src=\"/media/{$media->id}/conversions/test-thumb.jpg\" alt=\"test\">",
$media->img('thumb'),
);
});

it('can render itself with responsive images and a placeholder', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestJpg())
Expand Down