From fe3f17e8e5be30b056bd9fcc610e975d8199e154 Mon Sep 17 00:00:00 2001 From: Nicolas Beauvais Date: Mon, 9 Dec 2024 17:19:56 +0100 Subject: [PATCH] Fix SVG files loosing transparency during conversion (#3728) * Fix SVG files loosing transparency during conversion * Update SvgTest to allow png format * Test that SVG conversion maintain transparency * Allow SvgTest to be run locally --- src/Conversions/ImageGenerators/Svg.php | 9 +++++---- tests/Conversions/ImageGenerators/SvgTest.php | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Conversions/ImageGenerators/Svg.php b/src/Conversions/ImageGenerators/Svg.php index a40eb53df..881fe13ab 100644 --- a/src/Conversions/ImageGenerators/Svg.php +++ b/src/Conversions/ImageGenerators/Svg.php @@ -11,12 +11,13 @@ class Svg extends ImageGenerator { public function convert(string $file, ?Conversion $conversion = null): string { - $imageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.jpg'; + $imageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.png'; - $image = new Imagick; - $image->readImage($file); + $image = new Imagick(); $image->setBackgroundColor(new ImagickPixel('none')); - $image->setImageFormat('jpg'); + $image->readImage($file); + + $image->setImageFormat('png32'); file_put_contents($imageFile, $image); diff --git a/tests/Conversions/ImageGenerators/SvgTest.php b/tests/Conversions/ImageGenerators/SvgTest.php index fb6eebbae..9bfea5d0d 100644 --- a/tests/Conversions/ImageGenerators/SvgTest.php +++ b/tests/Conversions/ImageGenerators/SvgTest.php @@ -17,5 +17,7 @@ $imageFile = $imageGenerator->convert($media->getPath()); - expect(mime_content_type($imageFile))->toEqual('image/jpeg'); -})->skipWhenRunningLocally(); + expect(mime_content_type($imageFile))->toEqual('image/png'); + + expect((new Imagick($imageFile))->getImageAlphaChannel())->toBeTrue(); +});