try to fix issue w/ news images optimization
This commit is contained in:
parent
92f349d8e7
commit
9ce5f89d74
|
|
@ -28,29 +28,30 @@ class Command(BaseCommand):
|
|||
|
||||
def optimize(self, text, max_size, max_quality):
|
||||
"""optimize news images"""
|
||||
for image in get_url_images_in_text(text):
|
||||
try:
|
||||
size, width, height = get_image_meta_by_url(image)
|
||||
except IOError as ie:
|
||||
self.stdout.write(self.style.NOTICE(f'{ie}\n'))
|
||||
continue
|
||||
if isinstance(text, str):
|
||||
for image in get_url_images_in_text(text):
|
||||
try:
|
||||
size, width, height = get_image_meta_by_url(image)
|
||||
except IOError as ie:
|
||||
self.stdout.write(self.style.NOTICE(f'{ie}\n'))
|
||||
continue
|
||||
|
||||
if size < max_size:
|
||||
self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n'))
|
||||
continue
|
||||
if size < max_size:
|
||||
self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n'))
|
||||
continue
|
||||
|
||||
percents = round(max_size / (size * 0.01))
|
||||
width = round(width * percents / 100)
|
||||
height = round(height * percents / 100)
|
||||
optimized_image = get_thumbnail(
|
||||
file_=image,
|
||||
geometry_string=f'{width}x{height}',
|
||||
upscale=False,
|
||||
quality=max_quality
|
||||
).url
|
||||
text = text.replace(image, optimized_image)
|
||||
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n'
|
||||
f'Quality [{percents}%]\n'))
|
||||
percents = round(max_size / (size * 0.01))
|
||||
width = round(width * percents / 100)
|
||||
height = round(height * percents / 100)
|
||||
optimized_image = get_thumbnail(
|
||||
file_=image,
|
||||
geometry_string=f'{width}x{height}',
|
||||
upscale=False,
|
||||
quality=max_quality
|
||||
).url
|
||||
text = text.replace(image, optimized_image)
|
||||
self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n'
|
||||
f'Quality [{percents}%]\n'))
|
||||
|
||||
return text
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user