thumbor / thumbor

thumbor is an open-source photo thumbnail service by globo.com

Home Page:http://thumbor.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fit-in filter causing division by zero exception and HTTP status 500 in some cases

bondido opened this issue · comments

Thumbor generates HTTP 500 error status in case the 'fit-in' filter is called on image with some "unusual" proportions - e.g. the attached image 1000px wide and 4px high to be fitted in 50x50 rectangle. Resulting resolution is 20 times smaller - giving 50px width and 0.2px height which rounds to 0, thus causing "ZeroDivisionError" while scaling

Thumbor request URL

/unsafe/fit-in/50x50/localhost/image-1000x4.jpg

Expected behaviour

Considering the image resolution (width/height ratio) it should be 50x1px image

Actual behaviour

HTTP status 500

Thumbor log (from thumbor 7.6.0, but 7.7.1 behaves the same):

thumbor:ERROR ERROR: Traceback (most recent call last):
File "/home/thumbor/venv/lib/python3.11/site-packages/tornado/web.py", line 1786, in _execute
result = await result
^^^^^^^^^^^^
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/handlers/imaging.py", line 119, in get
return await self.check_image(kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/handlers/imaging.py", line 116, in check_image
return await self.execute_image_operations()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/handlers/init.py", line 205, in execute_image_operations
await self.get_image()
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/handlers/init.py", line 305, in get_image
await self.context.transformer.transform()
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/transformer.py", line 38, in transform
await self.do_image_operations()
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/transformer.py", line 218, in do_image_operations
await self.context.thread_pool.queue(
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/threadpool.py", line 53, in queue
return await self._execute_in_foreground(operation, *args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/threadpool.py", line 45, in _execute_in_foreground
return operation(*args)
^^^^^^^^^^^^^^^^
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/transformer.py", line 237, in img_operation_worker
self.fit_in_resize()
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/transformer.py", line 422, in fit_in_resize
self.engine.resize(resize_width, resize_height)
File "/home/thumbor/venv/lib/python3.11/site-packages/thumbor/engines/pil.py", line 212, in resize
self.image.draft(None, size)
File "/home/thumbor/venv/lib/python3.11/site-packages/PIL/JpegImagePlugin.py", line 441, in draft
scale = min(self.size[0] // size[0], self.size[1] // size[1])
~~~~~~~~~~~~~^^~~~~~~~~
ZeroDivisionError: integer division or modulo by zero
tornado.access:ERROR 500 GET ...

Operating system

docker image built on debian bookworm OS

Your thumbor.conf

I think it's irrelevant but tested it with my custom config, as well as with the default config generated by thumbor-conf

Sample image

image-1000x4

I tried to do some code analysis and found that there is 0px avoidance line in resize function:

self.target_width or 1, self.target_height or 1

...but there is no such avoidance in fit_in_resize function:

self.engine.resize(resize_width, resize_height)

Shuldn't it be coded the same way in both cases?

What's more - shouldn't it respect the MIN_WIDTH/MIN_HEIGHT configuration parameters?
https://thumbor.readthedocs.io/en/latest/configuration.html#min-width-and-min-height

This issue is stale because it has been open 30 days with no activity. Remove the stale label or add a comment, or this issue will be closed in 15 days. You can always re-open if you still feel this is still an issue. Tag @heynemann for more information.

This issue is stale because it has been open 30 days with no activity. Remove the stale label or add a comment, or this issue will be closed in 15 days. You can always re-open if you still feel this is still an issue. Tag @heynemann for more information.

Still an issue

This issue is stale because it has been open 30 days with no activity. Remove the stale label or add a comment, or this issue will be closed in 15 days. You can always re-open if you still feel this is still an issue. Tag @heynemann for more information.

This issue was closed because it has been stale for 15 days with no activity.