svg image support #374

This commit is contained in:
rimu 2024-12-06 10:21:44 +13:00
parent 4f95d1237e
commit 4e87a365b3
7 changed files with 43 additions and 30 deletions

View file

@ -155,7 +155,7 @@ class CreateImageForm(CreatePostForm):
def validate(self, extra_validators=None) -> bool: def validate(self, extra_validators=None) -> bool:
uploaded_file = request.files['image_file'] uploaded_file = request.files['image_file']
if uploaded_file and uploaded_file.filename != '': if uploaded_file and uploaded_file.filename != '' and not uploaded_file.filename.endswith('.svg'):
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485
# Do not allow fascist meme content # Do not allow fascist meme content
try: try:

View file

@ -679,6 +679,7 @@ def add_post(actor, type):
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485
# resize if necessary # resize if necessary
if not final_place.endswith('.svg'):
img = Image.open(final_place) img = Image.open(final_place)
if '.' + img.format.lower() in allowed_extensions: if '.' + img.format.lower() in allowed_extensions:
img = ImageOps.exif_transpose(img) img = ImageOps.exif_transpose(img)

View file

@ -25,7 +25,7 @@ from sqlalchemy import func, desc, text
import os import os
allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo', '.avif'] allowed_extensions = ['.gif', '.jpg', '.jpeg', '.png', '.webp', '.heic', '.mpo', '.avif', '.svg']
def search_for_community(address: str): def search_for_community(address: str):
@ -649,11 +649,19 @@ def save_icon_file(icon_file, directory='communities') -> File:
if file_ext.lower() == '.heic': if file_ext.lower() == '.heic':
register_heif_opener() register_heif_opener()
elif file_ext.lower() == '.avif':
import pillow_avif
# resize if necessary # resize if necessary
if file_ext.lower() in allowed_extensions:
if file_ext.lower() == '.svg': # svgs don't need to be resized
file = File(file_path=final_place, file_name=new_filename + file_ext, alt_text=f'{directory} icon',
thumbnail_path=final_place)
db.session.add(file)
return file
else:
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485
img = Image.open(final_place) img = Image.open(final_place)
if '.' + img.format.lower() in allowed_extensions:
img = ImageOps.exif_transpose(img) img = ImageOps.exif_transpose(img)
img_width = img.width img_width = img.width
img_height = img.height img_height = img.height
@ -695,6 +703,8 @@ def save_banner_file(banner_file, directory='communities') -> File:
if file_ext.lower() == '.heic': if file_ext.lower() == '.heic':
register_heif_opener() register_heif_opener()
elif file_ext.lower() == '.avif':
import pillow_avif
# resize if necessary # resize if necessary
Image.MAX_IMAGE_PIXELS = 89478485 Image.MAX_IMAGE_PIXELS = 89478485

View file

@ -905,6 +905,7 @@ div.navbar {
} }
.post_teaser_image_preview img { .post_teaser_image_preview img {
max-width: 100%; max-width: 100%;
min-width: 150px;
margin-right: 4px; margin-right: 4px;
border-radius: 5px; border-radius: 5px;
height: auto; height: auto;

View file

@ -501,6 +501,7 @@ div.navbar {
} }
img { img {
max-width: 100%; max-width: 100%;
min-width: 150px;
margin-right: 4px; margin-right: 4px;
border-radius: 5px; border-radius: 5px;
height: auto; height: auto;

View file

@ -22,9 +22,9 @@
</div> </div>
{{ render_field(form.description) }} {{ render_field(form.description) }}
{{ render_field(form.icon_file) }} {{ render_field(form.icon_file) }}
<small class="field_hint">Provide a square image that looks good when small.</small> <small class="field_hint">{{ _('Provide a square image that looks good when small. SVG is allowed.') }}</small>
{{ render_field(form.banner_file) }} {{ render_field(form.banner_file) }}
<small class="field_hint">Provide a wide image - letterbox orientation.</small> <small class="field_hint">{{ _('Provide a wide image - letterbox orientation.') }}</small>
{{ render_field(form.rules) }} {{ render_field(form.rules) }}
{{ render_field(form.nsfw) }} {{ render_field(form.nsfw) }}
{{ render_field(form.local_only) }} {{ render_field(form.local_only) }}

View file

@ -187,7 +187,7 @@ def make_cache_key(sort=None, post_id=None, view_filter=None):
def is_image_url(url): def is_image_url(url):
common_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.avif'] common_image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.avif', '.svg+xml', '.svg+xml; charset=utf-8']
mime_type = mime_type_using_head(url) mime_type = mime_type_using_head(url)
if mime_type: if mime_type:
mime_type_parts = mime_type.split('/') mime_type_parts = mime_type.split('/')