Skip to content

Commit

Permalink
Added Image Info for SVG files (#1146)
Browse files Browse the repository at this point in the history
Co-authored-by: leorochael
  • Loading branch information
drfho committed Sep 16, 2023
1 parent 59f6870 commit a9b1e5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
- Update ``AccessControl`` to version 6.2 to mitigate a security problem.
(CVE-2023-41050)

- Added image dimensions to SVG file properties
`#1146 <https://github.com/zopefoundation/Zope/pull/1146>`_.

5.8.3 (2023-06-15)
------------------
Expand Down
34 changes: 34 additions & 0 deletions src/OFS/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import struct
from email.generator import _make_boundary
from io import BytesIO
from xml.dom import minidom

import ZPublisher.HTTPRequest
from AccessControl.class_init import InitializeClass
Expand Down Expand Up @@ -835,6 +836,39 @@ def getImageInfo(data):
except Exception:
pass

# handle SVGs
elif (size >= 16) and ((b'<?xml' in data[:16]) or (b'<svg' in data[:16])):
try:
xmldoc = minidom.parseString(data)
except Exception:
return content_type, width, height
for svg in xmldoc.getElementsByTagName('svg'):
content_type = 'image/svg+xml'
if 'height' in svg.attributes and 'width' in svg.attributes:
w = svg.attributes['width'].value
h = svg.attributes['height'].value
try:
w = int(float(w))
h = int(float(h))
except Exception:
if str(w).endswith('px'):
w = int(float(w[:-2]))
h = int(float(h[:-2]))
elif str(w).endswith('mm'):
w = int(float(w[:-2]) * 3.7795)
h = int(float(h[:-2]) * 3.7795)
elif str(w).endswith('cm'):
w = int(float(w[:-2]) * 37.795)
h = int(float(h[:-2]) * 37.795)
break
elif 'viewBox' in svg.attributes:
viewBox = svg.attributes['viewBox'].value
viewBox = [int(float(x)) for x in viewBox.split(' ')]
w = viewBox[2] - viewBox[0]
h = viewBox[3] - viewBox[1]
width = int(w)
height = int(h)

return content_type, width, height


Expand Down

0 comments on commit a9b1e5b

Please sign in to comment.