quality calculate
This commit is contained in:
parent
e70e1d0a67
commit
ba4c6901f9
|
|
@ -2,12 +2,14 @@
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from utils.methods import get_url_images_in_text
|
from utils.methods import get_url_images_in_text, get_page_size_by_url
|
||||||
from news.models import News
|
from news.models import News
|
||||||
from sorl.thumbnail import get_thumbnail
|
from sorl.thumbnail import get_thumbnail
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
SORL_THUMBNAIL_ALIASES = 'news_description'
|
SORL_THUMBNAIL_ALIASES = 'news_description'
|
||||||
|
IMAGE_MAX_SIZE_IN_BYTES = 1048576 # ~ 1mb
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -16,24 +18,51 @@ class Command(BaseCommand):
|
||||||
default=self.SORL_THUMBNAIL_ALIASES,
|
default=self.SORL_THUMBNAIL_ALIASES,
|
||||||
help='Ключ для параметров оптимизации',
|
help='Ключ для параметров оптимизации',
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-s',
|
||||||
|
'--size',
|
||||||
|
default=self.IMAGE_MAX_SIZE_IN_BYTES,
|
||||||
|
help='Максимальный размер файла в байтах',
|
||||||
|
type=int
|
||||||
|
)
|
||||||
|
|
||||||
def optimize(self, text, alias):
|
def optimize(self, text, alias, max_size):
|
||||||
|
"""optimize news images"""
|
||||||
for image in get_url_images_in_text(text):
|
for image in get_url_images_in_text(text):
|
||||||
|
size = get_page_size_by_url(image)
|
||||||
|
|
||||||
|
if size < max_size:
|
||||||
|
continue
|
||||||
|
|
||||||
|
quality = round(max_size / (size * 0.01))
|
||||||
optimized_image = get_thumbnail(
|
optimized_image = get_thumbnail(
|
||||||
file_=image,
|
file_=image,
|
||||||
**settings.SORL_THUMBNAIL_ALIASES[alias]
|
**settings.SORL_THUMBNAIL_ALIASES[alias],
|
||||||
|
quality=100,
|
||||||
|
upscale=False
|
||||||
).url
|
).url
|
||||||
text = text.replace(image, optimized_image)
|
text = text.replace(image, optimized_image)
|
||||||
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}...\n'))
|
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n'
|
||||||
|
f'Quality [{quality}%]\n'))
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
alias = options['alias']
|
||||||
|
size = options['size']
|
||||||
|
|
||||||
|
if alias not in settings.SORL_THUMBNAIL_ALIASES:
|
||||||
|
self.stdout.write(self.style.ERROR(f'{alias} not found in alias'))
|
||||||
|
return
|
||||||
|
|
||||||
|
self.optimize('https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg',
|
||||||
|
'news_description', size)
|
||||||
|
|
||||||
for news in News.objects.all():
|
for news in News.objects.all():
|
||||||
if not isinstance(news.description, dict):
|
if not isinstance(news.description, dict):
|
||||||
continue
|
continue
|
||||||
news.description = {
|
news.description = {
|
||||||
locale: self.optimize(text, options['alias'])
|
locale: self.optimize(text, alias, size)
|
||||||
for locale, text in news.description.items()
|
for locale, text in news.description.items()
|
||||||
}
|
}
|
||||||
news.save()
|
news.save()
|
||||||
|
|
@ -174,3 +174,8 @@ def section_name_into_index_name(section_name: str):
|
||||||
def get_url_images_in_text(text):
|
def get_url_images_in_text(text):
|
||||||
"""Find images urls in text"""
|
"""Find images urls in text"""
|
||||||
return re.findall(r'(?:http:|https:)?//.*\.(?:png|jpg|svg)', text)
|
return re.findall(r'(?:http:|https:)?//.*\.(?:png|jpg|svg)', text)
|
||||||
|
|
||||||
|
def get_page_size_by_url(url):
|
||||||
|
"""Get page size by Content-Length header"""
|
||||||
|
response = requests.head(url)
|
||||||
|
return int(response.headers.get('content-length'))
|
||||||
|
|
@ -385,7 +385,7 @@ THUMBNAIL_QUALITY = 85
|
||||||
THUMBNAIL_DEBUG = False
|
THUMBNAIL_DEBUG = False
|
||||||
SORL_THUMBNAIL_ALIASES = {
|
SORL_THUMBNAIL_ALIASES = {
|
||||||
'news_preview': {'geometry_string': '300x260', 'crop': 'center'},
|
'news_preview': {'geometry_string': '300x260', 'crop': 'center'},
|
||||||
'news_description': {'geometry_string': '100x100', 'quality': 50},
|
'news_description': {'geometry_string': '100x100'},
|
||||||
'news_promo_horizontal_web': {'geometry_string': '1900x600', 'crop': 'center'},
|
'news_promo_horizontal_web': {'geometry_string': '1900x600', 'crop': 'center'},
|
||||||
'news_promo_horizontal_mobile': {'geometry_string': '375x260', 'crop': 'center'},
|
'news_promo_horizontal_mobile': {'geometry_string': '375x260', 'crop': 'center'},
|
||||||
'news_tile_horizontal_web': {'geometry_string': '300x275', 'crop': 'center'},
|
'news_tile_horizontal_web': {'geometry_string': '300x275', 'crop': 'center'},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user