Skip to content

Commit

Permalink
refacotr svg generation
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 8, 2017
1 parent a96db80 commit 50fb8e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
7 changes: 4 additions & 3 deletions resources/views/placeholderSvg.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" x="0" y="0" viewBox="0 0 {{ $largestResponsiveImage->width() }} {{ $largestResponsiveImage->height() }}">
<image width="{{ $largestResponsiveImage->width() }}" height="{{ $largestResponsiveImage->height() }}" xlink:href="{{ $tinyJpgBase64 }}">
</image>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" x="0"
y="0" viewBox="0 0 {{ $originalImageWidth }} {{ $originalImageHeight }}">
<image width="{{ $originalImageWidth }}" height="{{ $originalImageHeight }}" xlink:href="{{ $tinyImageBase64 }}">
</image>
</svg>
12 changes: 1 addition & 11 deletions src/ResponsiveImages/RegisteredResponsiveImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,11 @@ public function getSrcset(): string
return "{$responsiveImage->url()} {$responsiveImage->width()}w";
})
->implode(', ');

return $filesSrcset . ', ' . $this->getPlaceholderSvg() . ' 32w';
}

public function getPlaceholderSvg(): string
{
$largestResponsiveImage = $this->files->first();

$tinyJpgBase64 = $this->media->responsive_images[$this->generatedFor]['tinyJpgBase64'];

$svg = view('medialibrary::placeholderSvg', compact(
'largestResponsiveImage',
'tinyJpgBase64'
));

return 'data:image/svg+xml;base64,' . base64_encode($svg);
return $this->media->responsive_images[$this->generatedFor]['base64svg'];
}
}
8 changes: 2 additions & 6 deletions src/ResponsiveImages/ResponsiveImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ public static function register(Media $media, $fileName, $conversionName)
$media->save();
}

public static function registerTinyJpg(Media $media, string $filePath, string $conversionName)
public static function registerTinySvg(Media $media, string $base64Svg, string $conversionName)
{
$responsiveImages = $media->responsive_images;

$imageData = file_get_contents($filePath);

$base64 = 'data:image/jpeg;base64,' . base64_encode($imageData);

$responsiveImages[$conversionName]['tinyJpgBase64'] = $base64;
$responsiveImages[$conversionName]['base64svg'] = $base64Svg;

$media->responsive_images = $responsiveImages;

Expand Down
24 changes: 21 additions & 3 deletions src/ResponsiveImages/ResponsiveImageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,31 @@ public function generateResponsiveImage(
ResponsiveImage::register($media, $finalImageFileName, $conversionName);
}

public function generateTinyJpg(Media $media, string $baseImage, string $conversionName, BaseTemporaryDirectory $temporaryDirectory)
public function generateTinyJpg(Media $media, string $originalImage, string $conversionName, BaseTemporaryDirectory $temporaryDirectory)
{
$tempDestination = $temporaryDirectory->path('tiny.jpg');

Image::load($baseImage)->width(32)->blur(10)->save($tempDestination);
$originalImage = Image::load($originalImage);

ResponsiveImage::registerTinyJpg($media, $tempDestination, $conversionName);
$originalImageWidth = $originalImage->getWidth();

$originalImageHeight = $originalImage->getHeight();

$originalImage->width(32)->blur(5)->save($tempDestination);

$tinyImageDataBase64 = base64_encode(file_get_contents($tempDestination));

$tinyImageBase64 = 'data:image/jpeg;base64,' . $tinyImageDataBase64;

$svg = view('medialibrary::placeholderSvg', compact(
'originalImageWidth',
'originalImageHeight',
'tinyImageBase64'
));

$base64Svg = 'data:image/svg+xml;base64,' . base64_encode($svg);

ResponsiveImage::registerTinySvg($media, $base64Svg, $conversionName);
}

protected function appendToFileName(string $filePath, string $suffix): string
Expand Down

0 comments on commit 50fb8e3

Please sign in to comment.