Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/119.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix SVG scaling when no width/height attributes available. @petschki
8 changes: 4 additions & 4 deletions src/plone/scale/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,10 @@ def scale_svg_image(
logger.exception(
f"Can not convert source dimensions: '{source_width}':'{source_height}'"
)
data = image.read()
if isinstance(data, str):
return data.encode("utf-8"), (int(target_width), int(target_height))
return data, (int(target_width), int(target_height))
return etree.tostring(tree, encoding="utf-8", xml_declaration=True), (
int(target_width),
int(target_height),
)

source_aspectratio = source_width / source_height
target_aspectratio = target_width / target_height
Expand Down
48 changes: 48 additions & 0 deletions src/plone/scale/tests/data/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions src/plone/scale/tests/data/logo_no_width_height.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/plone/scale/tests/test_scale.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from io import BytesIO as StringIO
from plone.scale.scale import calculate_scaled_dimensions
from plone.scale.scale import scale_svg_image
from plone.scale.scale import scaleImage
from plone.scale.scale import scalePILImage
from plone.scale.tests import TEST_DATA_LOCATION
Expand All @@ -21,6 +22,8 @@
ANIGIF2 = (TEST_DATA_LOCATION / "animated2.gif").read_bytes()
GREYSCALE_IMG = (TEST_DATA_LOCATION / "greyscale_image.png").read_bytes()
ANIWEBP = (TEST_DATA_LOCATION / "animated.webp").read_bytes()
SVG = (TEST_DATA_LOCATION / "logo.svg").read_bytes()
SVG_NO_WIDTH_HEIGHT = (TEST_DATA_LOCATION / "logo_no_width_height.svg").read_bytes()


class ScalingTests(TestCase):
Expand Down Expand Up @@ -517,6 +520,18 @@ def testCalculateMinimumDimensions(self):
self.assertGreaterEqual(dimensions.target_width, 1)
self.assertGreaterEqual(dimensions.target_height, 1)

def testScaleSVGImage(self):
# Basic scaling test
scaled_svg = scale_svg_image(StringIO(SVG), 200, 100)
self.assertIn(b'width="200"', scaled_svg[0])
self.assertIn(b'height="100"', scaled_svg[0])

# Scale SVG without width and height attributes
scaled_svg = scale_svg_image(StringIO(SVG_NO_WIDTH_HEIGHT), 100, 100)
self.assertIn(b"<svg", scaled_svg[0])
self.assertNotIn(b'width="100"', scaled_svg[0])
self.assertNotIn(b'height="100"', scaled_svg[0])


def test_suite():
from unittest import defaultTestLoader
Expand Down