瀏覽代碼

Merge pull request #757 from jar1karp/better-figures-and-images-default-to-100pct-width

[better_figures_and_images] Default to 100% width if opening image with PIL fails (e.g. for SVG images)
Justin Mayer 7 年之前
父節點
當前提交
b2c93be183
共有 1 個文件被更改,包括 6 次插入2 次删除
  1. 6 2
      better_figures_and_images/better_figures_and_images.py

+ 6 - 2
better_figures_and_images/better_figures_and_images.py

@@ -77,8 +77,12 @@ def content_object_init(instance):
                 logger.debug('Better Fig. src: %s', src)
 
                 # Open the source image and query dimensions; build style string
-                im = Image.open(src)
-                extra_style = 'width: {}px; height: auto;'.format(im.size[0])
+                try:
+                    im = Image.open(src)
+                    extra_style = 'width: {}px; height: auto;'.format(im.size[0])
+                except IOError as e:
+                    logger.debug('Better Fig. Failed to open: %s', src)
+                    extra_style = 'width: 100%; height: auto;'
 
                 if 'RESPONSIVE_IMAGES' in instance.settings and instance.settings['RESPONSIVE_IMAGES']:
                     extra_style += ' max-width: 100%;'