From: Dan Brown Date: Sun, 22 May 2022 10:52:42 +0000 (+0100) Subject: Added SVG support to the image gallery. X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/c9aa1c979f7689aea6bae6ad7c2f2b75f45f4c0e Added SVG support to the image gallery. --- diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 5b2221fc1..01911808f 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -219,6 +219,6 @@ abstract class Controller extends BaseController */ protected function getImageValidationRules(): array { - return ['image_extension', 'mimes:jpeg,png,gif,webp', 'max:' . (config('app.upload_limit') * 1000)]; + return ['image_extension', 'mimes:jpeg,png,gif,webp,svg', 'max:' . (config('app.upload_limit') * 1000)]; } } diff --git a/app/Uploads/ImageService.php b/app/Uploads/ImageService.php index ca0db997b..b5e048892 100644 --- a/app/Uploads/ImageService.php +++ b/app/Uploads/ImageService.php @@ -30,7 +30,7 @@ class ImageService protected $image; protected $fileSystem; - protected static $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp']; + protected static $supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg']; /** * ImageService constructor. @@ -230,6 +230,14 @@ class ImageService return strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'gif'; } + /** + * Check if the given image is an SVG image file. + */ + protected function isSvg(Image $image): bool + { + return strtolower(pathinfo($image->path, PATHINFO_EXTENSION)) === 'svg'; + } + /** * Check if the given image and image data is apng. */ @@ -255,8 +263,8 @@ class ImageService */ public function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio = false): string { - // Do not resize GIF images where we're not cropping - if ($keepRatio && $this->isGif($image)) { + // Do not resize GIF images where we're not cropping or SVG images. + if (($keepRatio && $this->isGif($image)) || $this->isSvg($image)) { return $this->getPublicUrl($image->path); } diff --git a/tests/Uploads/ImageTest.php b/tests/Uploads/ImageTest.php index 01754d2de..418896828 100644 --- a/tests/Uploads/ImageTest.php +++ b/tests/Uploads/ImageTest.php @@ -74,6 +74,23 @@ class ImageTest extends TestCase $this->assertStringNotContainsString('thumbs-', $imgDetails['response']->thumbs->display); } + public function test_svg_upload() + { + /** @var Page $page */ + $page = Page::query()->first(); + $admin = $this->getAdmin(); + $this->actingAs($admin); + + $imgDetails = $this->uploadGalleryImage($page, 'diagram.svg', 'image/svg+xml'); + $this->assertFileExists(public_path($imgDetails['path'])); + $this->assertTrue( + $imgDetails['response']->url === $imgDetails['response']->thumbs->gallery + && $imgDetails['response']->url === $imgDetails['response']->thumbs->display, + ); + + $this->deleteImage($imgDetails['path']); + } + public function test_image_edit() { $editor = $this->getEditor(); diff --git a/tests/Uploads/UsesImages.php b/tests/Uploads/UsesImages.php index b55572248..513a5d49e 100644 --- a/tests/Uploads/UsesImages.php +++ b/tests/Uploads/UsesImages.php @@ -3,6 +3,7 @@ namespace Tests\Uploads; use BookStack\Entities\Models\Page; +use BookStack\Uploads\Image; use Illuminate\Http\UploadedFile; use stdClass; @@ -84,21 +85,19 @@ trait UsesImages * Returns the image name. * Can provide a page to relate the image to. * - * @param Page|null $page - * * @return array{name: string, path: string, page: Page, response: stdClass} */ - protected function uploadGalleryImage(Page $page = null, ?string $testDataFileName = null) + protected function uploadGalleryImage(Page $page = null, string $testDataFileName = 'first-image.png', string $contentType = 'image/png') { if ($page === null) { $page = Page::query()->first(); } - $imageName = $testDataFileName ?? 'first-image.png'; + $imageName = $testDataFileName; $relPath = $this->getTestImagePath('gallery', $imageName); $this->deleteImage($relPath); - $upload = $this->uploadImage($imageName, $page->id, 'image/png', $testDataFileName); + $upload = $this->uploadImage($imageName, $page->id, $contentType, $testDataFileName); $upload->assertStatus(200); return [ diff --git a/tests/test-data/diagram.svg b/tests/test-data/diagram.svg new file mode 100644 index 000000000..75e3a49e0 --- /dev/null +++ b/tests/test-data/diagram.svg @@ -0,0 +1,3 @@ + + +
Hello!
Hello!
Text is not SVG - cannot display
\ No newline at end of file