Compare commits

...

2 commits

Author SHA1 Message Date
934843d9d7 Merge pull request 'Compress images when uploaded' (#11) from dev into main
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #11
2023-07-19 12:22:25 +02:00
Jannis Portmann
8d8e1b69d7 Compress images when uploaded
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
continuous-integration/drone/promote/production Build is passing
2023-07-18 17:03:19 +02:00
3 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,37 @@
import sys
from io import BytesIO
from PIL import Image
from django.core.files.uploadedfile import InMemoryUploadedFile
from pflaenzli_django.settings import IMAGE_MAX_SIZE, IMAGE_QUALITY
def compress_image(image, path):
tmp_img = Image.open(image)
fmt = tmp_img.format
if fmt not in ['JPEG', 'PNG']:
raise TypeError(f'Invalid image format: {fmt}')
width, height = tmp_img.size
compressed_width, compressed_height = get_compressed_size(width, height)
tmp_img_resized = tmp_img.resize((compressed_width, compressed_height))
outputIoStream = BytesIO()
tmp_img_resized.save(outputIoStream, format=fmt, quality=IMAGE_QUALITY)
outputIoStream.seek(0)
return InMemoryUploadedFile(file=outputIoStream, field_name='ImageField',
content_type=f'image/{fmt.lower()}', name=path, size=sys.getsizeof(outputIoStream),
charset=None)
def get_compressed_size(width, height):
if width > height:
compressed_width = min(width, IMAGE_MAX_SIZE)
compressed_height = int(compressed_width * (height / width))
else:
compressed_height = min(height, IMAGE_MAX_SIZE)
compressed_width = int(compressed_height * (width / height))
return compressed_width, compressed_height

View file

@ -15,6 +15,7 @@ from .models import PflaenzliUser, Offer, Wish
from .mail import send_offer_email
from .upload import generate_unique_filename
from .utils.distance import calculate_distance, filter_by_distance
from .utils.compress_image import compress_image
def list_offers(request, filters=None):
@ -37,6 +38,7 @@ def create_offer(request):
if form.is_valid():
offer = form.save(commit=False)
offer.image.name = generate_unique_filename(form.cleaned_data['image'].name)
offer.image = compress_image(offer.image, offer.image.name)
offer.user = request.user
offer.title = offer.title.title()
offer.save()
@ -89,6 +91,7 @@ def offer_edit(request, offer_id):
offer = form.save(commit=False)
if 'image' in request.FILES:
offer.image.name = generate_unique_filename(form.cleaned_data['image'].name)
offer.image = compress_image(offer.image, offer.image.name)
else:
offer.image = offer.image
offer.user = request.user

View file

@ -180,3 +180,8 @@ FRC_CAPTCHA_VERIFICATION_URL = 'https://api.friendlycaptcha.com/api/v1/siteverif
# Loco translation managment
LOCO_API_KEY = os.getenv('LOCO_API_KEY')
# Image compression settings
IMAGE_MAX_SIZE = 1500 # Long edge size
IMAGE_QUALITY = 75 # 0-100%