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

Added withResponsiveImagesIf() on Media Conversions #2160

Closed
wants to merge 5 commits into from
Closed
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
12 changes: 11 additions & 1 deletion docs/responsive-images/getting-started-with-responsive-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,18 @@ $yourModel
->toMediaCollection();
```

Behind the scenes, the media library will generate multiple size variations of your image. To learn which variations are generated and how to customize head over [here](/laravel-medialibrary/v9/responsive-images/using-your-own-width-calculator).
You can also use it on the conversions.

```php
$this->addMediaConversion('thumb')
->width(368)
->height(232)
->withResponsiveImages()
// or if you want to add it based on a condition then use
->withResponsiveImagesIf($condition); // accepts "closure or boolean"
```

Behind the scenes, the media library will generate multiple size variations of your image. To learn which variations are generated and how to customize head over [here](/laravel-medialibrary/v9/responsive-images/using-your-own-width-calculator).

### Displaying responsive images

Expand Down
13 changes: 10 additions & 3 deletions src/Conversions/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getName(): string

public function getPerformOnCollections(): array
{
if (! count($this->performOnCollections)) {
if (!count($this->performOnCollections)) {
return ['default'];
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public function withoutManipulations(): self

public function __call($name, $arguments)
{
if (! method_exists($this->manipulations, $name)) {
if (!method_exists($this->manipulations, $name)) {
throw new BadMethodCallException("Manipulation `{$name}` does not exist");
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function performOnCollections(...$collectionNames): self
public function shouldBePerformedOn(string $collectionName): bool
{
//if no collections were specified, perform conversion on all collections
if (! count($this->performOnCollections)) {
if (!count($this->performOnCollections)) {
return true;
}

Expand Down Expand Up @@ -191,6 +191,13 @@ public function withResponsiveImages(): self
return $this;
}

public function withResponsiveImagesIf($condition): self
{
$this->generateResponsiveImages = (bool) (is_callable($condition) ? $condition() : $condition);

return $this;
}

public function shouldGenerateResponsiveImages(): bool
{
return $this->generateResponsiveImages;
Expand Down