0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Allow upload of SVG images

This commit is contained in:
Joshua Munn 2022-12-21 13:50:03 +00:00 committed by Matt Westcott
parent da7e68ea08
commit d519ad9b66
2 changed files with 13 additions and 1 deletions

View File

@ -9,7 +9,13 @@ from django.forms.fields import FileField, ImageField
from django.template.defaultfilters import filesizeformat
from django.utils.translation import gettext_lazy as _
ALLOWED_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp"]
ALLOW_SVG = getattr(settings, "WAGTAILIMAGES_ALLOW_SVG", False)
if ALLOW_SVG:
ALLOWED_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp", "svg"]
SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG, WEBP, SVG")
else:
ALLOWED_EXTENSIONS = ["gif", "jpg", "jpeg", "png", "webp"]
SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG, WEBP")
class WagtailImageField(ImageField):

View File

@ -47,6 +47,7 @@ IMAGE_FORMAT_EXTENSIONS = {
"png": ".png",
"gif": ".gif",
"webp": ".webp",
"svg": ".svg",
}
@ -579,6 +580,9 @@ class AbstractImage(ImageFileMixin, CollectionMember, index.Indexed, models.Mode
def is_landscape(self):
return self.height < self.width
def is_svg(self):
return self.file.name.endswith(".svg")
@property
def filename(self):
return os.path.basename(self.file.name)
@ -761,6 +765,8 @@ class Filter:
quality = getattr(settings, "WAGTAILIMAGES_WEBP_QUALITY", 85)
return willow.save_as_webp(output, quality=quality)
elif output_format == "svg":
return willow.save_as_svg(output)
raise UnknownOutputImageFormatError(
f"Unknown output image format '{output_format}'"
)