Skip to content

Commit

Permalink
Merge pull request #676 from znerol/feature/master/svg-dpi-setting
Browse files Browse the repository at this point in the history
Add SVG_DPI configuration directive
  • Loading branch information
guilhermef committed Feb 25, 2016
2 parents 2844ac2 + c4b5b7e commit 95ff6b3
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions debian/thumbor.conf
Expand Up @@ -30,6 +30,10 @@
#QUALITY = 85
#WEBP_QUALITY = 80

# Specify the ratio between 1in and 1px for SVG images. This is only used when
# rasterizing SVG images having their size units in cm or inches.
# SVG_DPI = 150

# Preserves exif information in generated images. Increases image size in kbytes, use with caution.
PRESERVE_EXIF_INFO = False

Expand Down
25 changes: 25 additions & 0 deletions tests/fixtures/images/escudo-in.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion tests/handlers/test_base_handler.py
Expand Up @@ -172,6 +172,7 @@ def get_context(self):
cfg.STORAGE = "thumbor.storages.file_storage"
cfg.FILE_STORAGE_ROOT_PATH = self.root_path
cfg.QUALITY = 'keep'
cfg.SVG_DPI = 200

importer = Importer(cfg)
importer.import_modules()
Expand Down Expand Up @@ -255,11 +256,24 @@ def test_can_read_cmyk_jpeg_as_png(self):
expect(response.code).to_equal(200)
expect(response.body).to_be_png()

def test_can_read_image_svg_and_convert_png(self):
def test_can_read_image_svg_with_px_units_and_convert_png(self):
response = self.fetch('/unsafe/escudo.svg')
expect(response.code).to_equal(200)
expect(response.body).to_be_png()

engine = Engine(self.context)
engine.load(response.body, '.png')
expect(engine.size).to_equal((1080, 1080))

def test_can_read_image_svg_with_inch_units_and_convert_png(self):
response = self.fetch('/unsafe/escudo-in.svg')
expect(response.code).to_equal(200)
expect(response.body).to_be_png()

engine = Engine(self.context)
engine.load(response.body, '.png')
expect(engine.size).to_equal((2400, 2400))


class ImageOperationsWithoutUnsafeTestCase(BaseImagingTestCase):
def get_context(self):
Expand Down
3 changes: 3 additions & 0 deletions thumbor/config.py
Expand Up @@ -49,6 +49,9 @@
'JPEG quality will be used.', 'Imaging')
Config.define('AUTO_WEBP', False, 'Specifies whether WebP format should be used automatically if the request accepts it '
'(via Accept header)', 'Imaging')
Config.define('SVG_DPI', 150,
'Specify the ratio between 1in and 1px for SVG images. This is only used when'
'rasterizing SVG images having their size units in cm or inches.', 'Imaging')
Config.define('MAX_AGE', 24 * 60 * 60, 'Max AGE sent as a header for the image served by thumbor in seconds', 'Imaging')
Config.define(
'MAX_AGE_TEMP_IMAGE', 0,
Expand Down
2 changes: 1 addition & 1 deletion thumbor/engines/__init__.py
Expand Up @@ -118,7 +118,7 @@ def convert_svg_to_png(self, buffer):
logger.error(msg)
return buffer

buffer = cairosvg.svg2png(bytestring=buffer)
buffer = cairosvg.svg2png(bytestring=buffer, dpi=self.context.config.SVG_DPI)
mime = self.get_mimetype(buffer)
self.extension = EXTENSION.get(mime, '.jpg')
self.transformed_body = buffer
Expand Down
4 changes: 4 additions & 0 deletions thumbor/thumbor.conf
Expand Up @@ -33,6 +33,10 @@
# Automatically converts images to WebP if Accepts header present
#AUTO_WEBP = False

# Specify the ratio between 1in and 1px for SVG images. This is only used when
# rasterizing SVG images having their size units in cm or inches.
# SVG_DPI = 150

# Preserves exif information in generated images. Increases image size in kbytes, use with caution.
PRESERVE_EXIF_INFO = False

Expand Down

0 comments on commit 95ff6b3

Please sign in to comment.