Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: spatie/laravel-medialibrary
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: virtruvio/laravel-medialibrary
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: bugfix
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
Loading
2 changes: 1 addition & 1 deletion src/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
@@ -147,7 +147,7 @@ protected function deleteResponsiveImagesForDeprecatedConversions(Media $media)
->map(function (Conversion $conversion) {
return $conversion->getName();
})
->push('medialibrary_original');
->push('ml_bri');

$responsiveImagesGeneratedFor = array_keys($media->responsive_images);

13 changes: 6 additions & 7 deletions src/Conversion/Conversion.php
Original file line number Diff line number Diff line change
@@ -239,21 +239,20 @@ public function shouldBeQueued(): bool
}

/*
* Get the extension that the result of this conversion must have.
*/
* Get the extension that the result of this conversion must have.
*/
public function getResultExtension(string $originalFileExtension = ''): string
{
if ($this->shouldKeepOriginalImageFormat()) {
if (in_array($originalFileExtension, ['jpg', 'jpeg', 'pjpg', 'png', 'gif'])) {
return $originalFileExtension;
}
$supportedFormats = ['jpg', 'pjpg', 'png', 'gif'];
if ($this->shouldKeepOriginalImageFormat() && in_array($originalFileExtension, $supportedFormats)) {
return $originalFileExtension;
}

if ($manipulationArgument = $this->manipulations->getManipulationArgument('format')) {
return $manipulationArgument;
}

return $originalFileExtension;
return 'jpg';
}

public function getConversionFile(string $file): string
2 changes: 1 addition & 1 deletion src/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ public function removeFile(Media $media, string $path)
$this->filesystem->disk($media->disk)->delete($path);
}

public function removeResponsiveImages(Media $media, string $conversionName = 'medialibrary_original')
public function removeResponsiveImages(Media $media, string $conversionName = 'ml_bri')
{
$responsiveImagesDirectory = $this->getResponsiveImagesDirectory($media);

21 changes: 17 additions & 4 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
@@ -264,13 +264,24 @@ public function toHtml()
return $this->img();
}

/**
* @param string $viewFile
*
* @return string
*/
public function toEmbed(String $viewFile) {
return $this->img('', [], $viewFile);
}

/**
* @param string|array $conversion
* @param array $extraAttributes
*
* @return string
*/
public function img($conversion = '', array $extraAttributes = []): string
public function img($conversion = '',
array $extraAttributes = [],
$viewFile = 'responsiveImageWithPlaceholder'): string
{
if (! (new Image())->canHandleMime($this->mime_type)) {
return '';
@@ -302,9 +313,11 @@ public function img($conversion = '', array $extraAttributes = []): string
$width = '';

if ($this->hasResponsiveImages($conversion)) {
$viewName = config('medialibrary.responsive_images.use_tiny_placeholders')
? 'responsiveImageWithPlaceholder'
: 'responsiveImage';
// $viewName = config('medialibrary.responsive_images.use_tiny_placeholders')
// ? 'responsiveImageWithPlaceholder'
// : 'responsiveImage';

$viewName = $viewFile;

$width = $this->responsiveImages($conversion)->files->first()->width();
}
19 changes: 13 additions & 6 deletions src/ResponsiveImages/RegisteredResponsiveImages.php
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public function __construct(Media $media, string $conversionName = '')
$this->media = $media;

$this->generatedFor = $conversionName === ''
? 'medialibrary_original'
? 'ml_bri'
: $conversionName;

$this->files = collect($media->responsive_images[$this->generatedFor]['urls'] ?? [])
@@ -44,11 +44,18 @@ public function getUrls(): array

public function getSrcset(): string
{
$filesSrcset = $this->files
->map(function (ResponsiveImage $responsiveImage) {
return "{$responsiveImage->url()} {$responsiveImage->width()}w";
})
->implode(', ');
$filesSrcset = $this->files
->map(function (ResponsiveImage $responsiveImage) {
if ($this->media->hasCustomProperty('custom.art.'.$responsiveImage->width().'.active'))
{
$customUrl = $this->media->getUrl(). $this->media->getCustomProperty('custom.art.'.$responsiveImage->width().'.qS');
$customUrl = str_replace('storage','xstorm', $customUrl);
return "{$customUrl} {$responsiveImage->width()}w";
} else {
return "{$responsiveImage->url()} {$responsiveImage->width()}w";
}
})
->implode(', ');

$shouldAddPlaceholderSvg = config('medialibrary.responsive_images.use_tiny_placeholders')
&& $this->getPlaceholderSvg();
6 changes: 3 additions & 3 deletions src/ResponsiveImages/ResponsiveImageGenerator.php
Original file line number Diff line number Diff line change
@@ -50,12 +50,12 @@ public function generateResponsiveImages(Media $media)
$media = $this->cleanResponsiveImages($media);

foreach ($this->widthCalculator->calculateWidthsFromFile($baseImage) as $width) {
$this->generateResponsiveImage($media, $baseImage, 'medialibrary_original', $width, $temporaryDirectory);
$this->generateResponsiveImage($media, $baseImage, 'ml_bri', $width, $temporaryDirectory);
}

event(new ResponsiveImagesGenerated($media));

$this->generateTinyJpg($media, $baseImage, 'medialibrary_original', $temporaryDirectory);
$this->generateTinyJpg($media, $baseImage, 'ml_bri', $temporaryDirectory);

$temporaryDirectory->delete();
}
@@ -155,7 +155,7 @@ protected function guardAgainstInvalidTinyPlaceHolder(string $tinyPlaceholderPat
}
}

protected function cleanResponsiveImages(Media $media, string $conversionName = 'medialibrary_original') : Media
protected function cleanResponsiveImages(Media $media, string $conversionName = 'ml_bri') : Media
{
$responsiveImages = $media->responsive_images;
$responsiveImages[$conversionName]['urls'] = [];
9 changes: 8 additions & 1 deletion src/UrlGenerator/LocalUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace Spatie\MediaLibrary\UrlGenerator;

use Auth;
use DateTimeInterface;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\Exceptions\UrlCannotBeDetermined;
@@ -49,8 +50,14 @@ public function getPath(): string

protected function getBaseMediaDirectoryUrl(): string
{
if ($diskUrl = $this->config->get("filesystems.disks.{$this->media->disk}.url")) {
if (Auth::check() || app()->runningInConsole()) {
if ($diskUrl = $this->config->get("filesystems.disks.{$this->media->disk}.url")) {
return str_replace(url('/'), '', $diskUrl);
}
} else {
if ($diskUrl = $this->config->get('filesystems.disks.cloudfront.url')) {
return str_replace(url('/'), '', $diskUrl);
}
}

if (! Str::startsWith($this->getStoragePath(), public_path())) {
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

return '<img srcset="/media/2/responsive-images/test___medialibrary_original_340_280.jpg 340w, /media/2/responsive-images/test___medialibrary_original_284_233.jpg 284w, /media/2/responsive-images/test___medialibrary_original_237_195.jpg 237w, data:image/svg+xml;base64,PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiIHg9IjAiCiB5PSIwIiB2aWV3Qm94PSIwIDAgMzQwIDI4MCI+Cgk8aW1hZ2Ugd2lkdGg9IjM0MCIgaGVpZ2h0PSIyODAiIHhsaW5rOmhyZWY9ImRhdGE6aW1hZ2UvanBlZztiYXNlNjQsLzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELy9nQTdRMUpGUVZSUFVqb2daMlF0YW5CbFp5QjJNUzR3SUNoMWMybHVaeUJKU2tjZ1NsQkZSeUIyT1RBcExDQnhkV0ZzYVhSNUlEMGdPVEFLLzlzQVF3QURBZ0lEQWdJREF3TURCQU1EQkFVSUJRVUVCQVVLQndjR0NBd0tEQXdMQ2dzTERRNFNFQTBPRVE0TEN4QVdFQkVURkJVVkZRd1BGeGdXRkJnU0ZCVVUvOXNBUXdFREJBUUZCQVVKQlFVSkZBMExEUlFVRkJRVUZCUVVGQlFVRkJRVUZCUVVGQlFVRkJRVUZCUVVGQlFVRkJRVUZCUVVGQlFVRkJRVUZCUVVGQlFVRkJRVS84QUFFUWdBR2dBZ0F3R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UE5QOEFESGkzNGd3R08ydFpDeEdlaHIxYjludWZVL0NWNVBvMnFvNnlvMk1OWDAzMUtGYWxWOWpPOG8yc3JicnI5eDlYbk9YMGFjS1A5bjF2YVNkbkpKV3QzUjZ0OFJ2QVVIanQ0YmFkZG9VNURZNlY1MTRqK0R1citIbDNXQ0xjMnFEb1JYdnpqOTRLbXVQbXMzQjVIdlh4ZVovV2FWSG13OVRsZCsxelpWSVlpdERuanBvamp2Z2RiWDJoeUpOZjJNY2NKWEhTcXZpdlQ0NS9IaHViTzFDUnVjbGxGZDNiL0xab0J3UGFxYklwbnpnWjljVjZQRDJLeFZGT3RYbnp0M1cxZzlyREQxcDhrZE5VZi8vWiI+Cgk8L2ltYWdlPgo8L3N2Zz4= 32w" onload="this.onload=null;this.sizes=Math.ceil(this.getBoundingClientRect().width/window.innerWidth*100)+\'vw\';" sizes="1px" src="/media/2/test.jpg" width="340">
return '<img srcset="/media/2/responsive-images/test___ml_bri_340_280.jpg 340w, /media/2/responsive-images/test___ml_bri_284_233.jpg 284w, /media/2/responsive-images/test___ml_bri_237_195.jpg 237w, data:image/svg+xml;base64,PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj4KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHhtbDpzcGFjZT0icHJlc2VydmUiIHg9IjAiCiB5PSIwIiB2aWV3Qm94PSIwIDAgMzQwIDI4MCI+Cgk8aW1hZ2Ugd2lkdGg9IjM0MCIgaGVpZ2h0PSIyODAiIHhsaW5rOmhyZWY9ImRhdGE6aW1hZ2UvanBlZztiYXNlNjQsLzlqLzRBQVFTa1pKUmdBQk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FYyZDNoNWVvS0RoSVdHaDRpSmlwS1RsSldXbDVpWm1xS2pwS1dtcDZpcHFyS3p0TFcydDdpNXVzTER4TVhHeDhqSnl0TFQxTlhXMTlqWjJ1TGo1T1htNStqcDZ2THo5UFgyOS9qNSt2L2FBQXdEQVFBQ0VRTVJBRDhBN3JYUEZON3BkMUs2d3RMRWd6Z0NzNlh4OWNhellMTGF0OWprSDNnL0ZVdmlaOFJrK0hFZ00xbUxrVGNESXJ4enhWOFVyN1U3Q1NTejAzeWd3eU5ncnpWaTY4NXpoN0t5amF6L0FKcnZYN2p3OGRVeHVCblRqUnBlMDUzcnJibFhjOWtiNG5yNGJzcEo5UXVrbmZzcW11djhIK01ZUEZ0a2wxYnNDcDZnZHErUE5QOEFESGkzNGd3R08ydFpDeEdlaHIxYjludWZVL0NWNVBvMnFvNnlvMk1OWDAzMUtGYWxWOWpPOG8yc3JicnI5eDlYbk9YMGFjS1A5bjF2YVNkbkpKV3QzUjZ0OFJ2QVVIanQ0YmFkZG9VNURZNlY1MTRqK0R1citIbDNXQ0xjMnFEb1JYdnpqOTRLbXVQbXMzQjVIdlh4ZVovV2FWSG13OVRsZCsxelpWSVlpdERuanBvamp2Z2RiWDJoeUpOZjJNY2NKWEhTcXZpdlQ0NS9IaHViTzFDUnVjbGxGZDNiL0xab0J3UGFxYklwbnpnWjljVjZQRDJLeFZGT3RYbnp0M1cxZzlyREQxcDhrZE5VZi8vWiI+Cgk8L2ltYWdlPgo8L3N2Zz4= 32w" onload="this.onload=null;this.sizes=Math.ceil(this.getBoundingClientRect().width/window.innerWidth*100)+\'vw\';" sizes="1px" src="/media/2/test.jpg" width="340">
';
Original file line number Diff line number Diff line change
@@ -18,10 +18,10 @@ public function it_will_register_generated_responsive_images_in_the_db()
$media = $this->testModel->getFirstMedia();

$this->assertEquals([
'test___medialibrary_original_340_280.jpg',
'test___medialibrary_original_284_233.jpg',
'test___medialibrary_original_237_195.jpg',
], $media->responsive_images['medialibrary_original']['urls']);
'test___ml_bri_340_280.jpg',
'test___ml_bri_284_233.jpg',
'test___ml_bri_237_195.jpg',
], $media->responsive_images['ml_bri']['urls']);
}

/** @test */
@@ -36,7 +36,7 @@ public function it_can_render_a_srcset_when_the_base64svg_is_not_rendered_yet()

$responsiveImages = $media->responsive_images;

unset($responsiveImages['medialibrary_original']['base64svg']);
unset($responsiveImages['ml_bri']['base64svg']);

$media->responsive_images = $responsiveImages;

Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ public function it_can_generate_responsive_images()
->withResponsiveImages()
->toMediaCollection();

$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___medialibrary_original_237_195.jpg'));
$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___medialibrary_original_284_233.jpg'));
$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___medialibrary_original_340_280.jpg'));
$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___ml_bri_237_195.jpg'));
$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___ml_bri_284_233.jpg'));
$this->assertFileExists($this->getTempDirectory('media/1/responsive-images/test___ml_bri_340_280.jpg'));
}

/** @test */