Commits

Mike Orr committed 2711440

``image()`` can now calculate the width and height automatically.

  • Participants
  • Parent commits cebd83f

Comments (0)

Files changed (2)

   - Fix spelling of Massachusetts.
 * webhelpers.html:
   - New ``HTML.cdata()`` method for producing "<!![CDATA[ ... ]]>" sections.
+* webhelpers.html.tags:
+  - ``image()`` can now calculate the width and height automatically
+    from an image file, using either the PIL algorithm or the pure Python
+    algorithm in ``webhelpers.media``. It also logs the dimensions to the
+    debug log for troubleshooting.
 * webhelpers.html.tools:
   - Fix HTML overescaping and underescaping in auto_link().  Patch by Marius
     Gedminas.

webhelpers/html/tags.py

 """
 
 import datetime
+import logging
 import os
 import re
 import urllib
 
 from webhelpers import containers
 from webhelpers.html import escape, HTML, literal, url_escape
+import webhelpers.media as media
 
 __all__ = [
            # Form tags
            "css_classes", "convert_boolean_attrs",
            ]
 
+log = logging.getLogger(__name__)
+
 NL = literal("\n")
 BR = literal("<br />\n")
 
     return getattr(HTML, tag)(content, **attrs)
     
 
-def image(url, alt, width=None, height=None, **attrs):
+def image(url, alt, width=None, height=None, path=None, use_pil=False, 
+    **attrs):
     """Return an image tag for the specified ``source``.
 
     ``url``
         alt tag completely, pass None.
 
     ``width``
-        The width of the image, default is not included
+        The width of the image, default is not included.
 
     ``height``
-        The height of the image, default is not included
+        The height of the image, default is not included.
+
+    ``path``
+        Calculate the width and height based on the image file at ``path`` if
+        possible. May not be specified if ``width`` or ``height`` is 
+        specified. The results are also written to the debug log for
+        troubleshooting.
+
+    ``use_pil``
+        If true, calcuate the image dimensions using the Python Imaging 
+        Library, which must be installed. Otherwise use a pure Python
+        algorithm which understands fewer image formats and may be less
+        accurate. This flag controls whether
+        ``webhelpers.media.get_dimensions_pil`` or
+        ``webhelpers.media.get_dimensions`` is called. It has no effect if
+        ``path`` is not specified.
         
     Examples::
 
 
         >>> image("/icons/icon.gif", None, width=16)
         literal(u'<img alt="" src="/icons/icon.gif" width="16" />')
-        
     """
     if not alt:
         alt = ""
-    if width is not None:
+    if width is not None or height is not None:
         attrs['width'] = width
-    if height is not None:
         attrs['height'] = height
+        if path:
+            raise TypeError(
+                "can't specify path if width and height are specified")
+    elif path:
+        if use_pil:
+            result = media.get_dimensions_pil(path)
+            msg = "using PIL"
+        else:
+            result = media.get_dimensions(path)
+            msg = "not using PIL"
+        abspath = os.path.abspath(path)
+        log.debug("image size is %s for '%s' (%s)", result, abspath, msg)
+        attrs['width'] = result[0]
+        attrs['height'] = result[1]
     return HTML.img(src=url, alt=alt, **attrs)
 
 #### Non-tag utilities