This PR fixes ConversionCollection's behavior in finding conversion by name.
If a subject model has multiple collections, and multiple conversions with the same name, but applied to different collections, then ConversionCollection incorrectly determines the conversion which should be applied to the media.
Example:
public function registerMediaCollections(): void
{
$this->addMediaCollection('avatar')->acceptsMimeTypes(['image/png'])->singleFile();
$this->addMediaCollection('signature')->acceptsMimeTypes(['image/jpeg'])->singleFile();
}
public function registerMediaConversions(?Media $media = null): void
{
$this
->addMediaConversion('preview')
->fit(Fit::Crop, 50, 50)
->performOnCollections('avatar')
->format('png');
$this
->addMediaConversion('preview')
->fit(Fit::Crop, 300, 100)
->performOnCollections('signature')
->format('jpeg');
}
Now, whereas conversion files are generated correctly, their URLs and paths are wrong, because ConversionCollection doesn't honor media collection, getting just the first conversion which name matches:
$media = $model->getFirstMedia('signature');
ConversionCollection::createForMedia($media)
->getByName('preview') // <-- issue occurs here, first conversion with this name is fetched
->getManipulations()
->toArray();
Generated URL is wrong too, because internally the fetched conversion is used to determine file extension:
$media->getFirstMediaUrl('signature', 'preview');
Result:
https://.../media/2/c/test-preview.png <-- should be .jpeg
This PR fixes this behavior. Specifically, ConversionCollection::getByName() function first filters conversions by media's collection, and only then searches by conversion name.
This PR fixes
ConversionCollection
's behavior in finding conversion by name.If a subject model has multiple collections, and multiple conversions with the same name, but applied to different collections, then
ConversionCollection
incorrectly determines the conversion which should be applied to the media.Example:
Now, whereas conversion files are generated correctly, their URLs and paths are wrong, because
ConversionCollection
doesn't honor media collection, getting just the first conversion which name matches:Result:
Generated URL is wrong too, because internally the fetched conversion is used to determine file extension:
Result:
This PR fixes this behavior. Specifically,
ConversionCollection::getByName()
function first filters conversions by media's collection, and only then searches by conversion name.Tests included.