Skip to content

Commit

Permalink
Add withResponsiveImages() to custom collection (#1681)
Browse files Browse the repository at this point in the history
* Add withResponsiveImages() method when defining a custom media collection

* Document 'withResponsiveImages' method to custom collection (#1681)
  • Loading branch information
50bhan authored and freekmurze committed Jan 5, 2020
1 parent acf2141 commit cf4bb43
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/working-with-media-collections/defining-media-collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,18 @@ $yourModel->getFirstMediaUrl('thumb') // returns an url to a 100x100 version of
```

Take a look at the [defining conversions section](/laravel-medialibrary/v7/converting-images/defining-conversions) to learn all the functions you can tack on to `addMediaConversion`.

## Generating responsive images

If you want to also generate responsive images for any media added to a collection you defined, you can simply use `withResponsiveImages` while defining it.

```php
// in your model

public function registerMediaCollections()
{
$this
->addMediaCollection('my-collection')
->withResponsiveImages();
}
```
11 changes: 11 additions & 0 deletions src/FileAdder/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ protected function processMediaItem(HasMedia $model, Media $media, self $fileAdd
{
$this->guardAgainstDisallowedFileAdditions($media, $model);

$this->checkGenerateResponsiveImages($media);

$model->media()->save($media);

if ($fileAdder->file instanceof RemoteFile) {
Expand Down Expand Up @@ -431,4 +433,13 @@ protected function guardAgainstDisallowedFileAdditions(Media $media)
throw FileUnacceptableForCollection::create($file, $collection, $this->subject);
}
}

protected function checkGenerateResponsiveImages(Media $media)
{
$collection = optional($this->getMediaCollection($media->collection_name))->generateResponsiveImages;

if ($collection) {
$this->withResponsiveImages();
}
}
}
10 changes: 10 additions & 0 deletions src/MediaCollection/MediaCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class MediaCollection
/** @var callable */
public $mediaConversionRegistrations;

/** @var bool */
public $generateResponsiveImages = false;

/** @var callable */
public $acceptsFile;

Expand Down Expand Up @@ -109,4 +112,11 @@ public function useFallbackPath(string $path): self

return $this;
}

public function withResponsiveImages(): self
{
$this->generateResponsiveImages = true;

return $this;
}
}
27 changes: 27 additions & 0 deletions tests/Feature/FileAdder/MediaConversions/MediaCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ public function registerMediaCollections()
$model->addMedia($this->getTestPdf())->preservingOriginal()->toMediaCollection('images');
}

/** @test * */
public function it_can_generate_responsive_images()
{
$testModel = new class extends TestModelWithConversion {
public function registerMediaCollections()
{
$this
->addMediaCollection('images')
->withResponsiveImages();
}
};

$model = $testModel::create(['name' => 'testmodel']);

$model->addMedia($this->getTestJpg())->preservingOriginal()->toMediaCollection('images');

$media = $model->getMedia('images')->first();

$this->assertEquals([
'http://localhost/media/1/responsive-images/test___medialibrary_original_340_280.jpg',
'http://localhost/media/1/responsive-images/test___medialibrary_original_284_233.jpg',
'http://localhost/media/1/responsive-images/test___medialibrary_original_237_195.jpg',
], $media->getResponsiveImageUrls());

$this->assertEquals([], $media->getResponsiveImageUrls('non-existing-conversion'));
}

/** @test */
public function if_the_single_file_method_is_specified_it_will_delete_all_other_media_and_will_only_keep_the_new_one()
{
Expand Down

0 comments on commit cf4bb43

Please sign in to comment.