Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
alex 2020-01-20 14:20:54 +03:00
commit 5f4852d11e

View File

@ -28,29 +28,30 @@ class Command(BaseCommand):
def optimize(self, text, max_size, max_quality): def optimize(self, text, max_size, max_quality):
"""optimize news images""" """optimize news images"""
for image in get_url_images_in_text(text): if isinstance(text, str):
try: for image in get_url_images_in_text(text):
size, width, height = get_image_meta_by_url(image) try:
except IOError as ie: size, width, height = get_image_meta_by_url(image)
self.stdout.write(self.style.NOTICE(f'{ie}\n')) except IOError as ie:
continue self.stdout.write(self.style.NOTICE(f'{ie}\n'))
continue
if size < max_size: if size < max_size:
self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n')) self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n'))
continue continue
percents = round(max_size / (size * 0.01)) percents = round(max_size / (size * 0.01))
width = round(width * percents / 100) width = round(width * percents / 100)
height = round(height * percents / 100) height = round(height * percents / 100)
optimized_image = get_thumbnail( optimized_image = get_thumbnail(
file_=image, file_=image,
geometry_string=f'{width}x{height}', geometry_string=f'{width}x{height}',
upscale=False, upscale=False,
quality=max_quality quality=max_quality
).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 [{percents}%]\n')) f'Quality [{percents}%]\n'))
return text return text