optimize collection images
This commit is contained in:
parent
440a263ea5
commit
c110dc3b96
|
|
@ -0,0 +1,29 @@
|
|||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import transaction
|
||||
from sorl.thumbnail import get_thumbnail
|
||||
|
||||
from collection.models import Collection
|
||||
from utils.methods import image_url_valid, get_image_meta_by_url
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
SORL_THUMBNAIL_ALIAS = 'collection_image'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with transaction.atomic():
|
||||
for collection in Collection.objects.all():
|
||||
if not image_url_valid(collection.image_url):
|
||||
continue
|
||||
|
||||
_, width, height = get_image_meta_by_url(collection.image_url)
|
||||
sorl_settings = settings.SORL_THUMBNAIL_ALIASES[self.SORL_THUMBNAIL_ALIAS]
|
||||
sorl_width_height = sorl_settings['geometry_string'].split('x')
|
||||
|
||||
if int(sorl_width_height[0]) > width or int(sorl_width_height[1]) > height:
|
||||
collection.image_url = get_thumbnail(
|
||||
file_=collection.image_url,
|
||||
**settings.SORL_THUMBNAIL_ALIASES[self.SORL_THUMBNAIL_ALIAS]
|
||||
).url
|
||||
|
||||
collection.save()
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import transaction
|
||||
from sorl.thumbnail import get_thumbnail
|
||||
|
||||
from establishment.models import Establishment
|
||||
from utils.methods import image_url_valid, get_image_meta_by_url
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
SORL_THUMBNAIL_ALIAS = 'establishment_collection_image'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with transaction.atomic():
|
||||
for establishment in Establishment.objects.all():
|
||||
if not image_url_valid(establishment.preview_image_url):
|
||||
continue
|
||||
|
||||
_, width, height = get_image_meta_by_url(establishment.preview_image_url)
|
||||
sorl_settings = settings.SORL_THUMBNAIL_ALIASES[self.SORL_THUMBNAIL_ALIAS]
|
||||
sorl_width_height = sorl_settings['geometry_string'].split('x')
|
||||
|
||||
if int(sorl_width_height[0]) > width or int(sorl_width_height[1]) > height:
|
||||
establishment.preview_image_url = get_thumbnail(
|
||||
file_=establishment.preview_image_url,
|
||||
**sorl_settings
|
||||
)
|
||||
establishment.save()
|
||||
|
|
@ -3,9 +3,10 @@ import logging
|
|||
import random
|
||||
import re
|
||||
import string
|
||||
from collections import namedtuple
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ class GMEngine(PILEngine):
|
|||
image = self.remove_border(image, options)
|
||||
image = self.scale(image, geometry, options)
|
||||
image = self.crop(image, geometry, options)
|
||||
image = self.scale(image, geometry, options)
|
||||
image = self.rounded(image, geometry, options)
|
||||
image = self.blur(image, geometry, options)
|
||||
image = self.padding(image, geometry, options)
|
||||
|
|
|
|||
|
|
@ -412,6 +412,8 @@ SORL_THUMBNAIL_ALIASES = {
|
|||
'city_detail': {'geometry_string': '1120x1120', 'crop': 'center'},
|
||||
'city_original': {'geometry_string': '2048x1536', 'crop': 'center'},
|
||||
'type_preview': {'geometry_string': '300x260', 'crop': 'center'},
|
||||
'collection_image': {'geometry_string': '940x620', 'upscale': False, 'quality': 100},
|
||||
'establishment_collection_image': {'geometry_string': '940x620', 'upscale': False, 'quality': 100}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user