Feature #400 - Resize images to fit on page
authorJoar Wandborg <git@wandborg.com>
Sat, 2 Jul 2011 23:30:07 +0000 (01:30 +0200)
committerJoar Wandborg <git@wandborg.com>
Sat, 2 Jul 2011 23:30:07 +0000 (01:30 +0200)
*   `mediagoblin.process_media.__init__`
    *   Added `medium` size image conversion
    *   Updated `thumbnail` conversion to use `queued_filename` instead
        of `queued_file`
*   `media.html`
    *   If there exists a `medium` size for the `MediaEntry`, it will
        display instead of the original `main` image.

mediagoblin/process_media/__init__.py
mediagoblin/templates/mediagoblin/user_pages/media.html

index 0dce1418cdec2577fdfeb8506e1c77b824040507..0d1abcb3b3420fe9a7448da1932ffb6a26d480b7 100644 (file)
@@ -22,6 +22,7 @@ from mediagoblin import mg_globals as mgg
 
 
 THUMB_SIZE = 200, 200
+MEDIUM_SIZE = 640, 640
 
 
 def create_pub_filepath(entry, filename):
@@ -43,20 +44,32 @@ def process_media_initial(media_id):
         mgg.queue_store, queued_filepath,
         'source')
 
-    queued_file = file(queued_filename, 'r')
+    thumb = Image.open(queued_filename)
+    thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
+    # ensure color mode is compatible with jpg
+    if thumb.mode != "RGB":
+        thumb = thumb.convert("RGB")
 
-    with queued_file:
-        thumb = Image.open(queued_file)
-        thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
-        # ensure color mode is compatible with jpg
-        if thumb.mode != "RGB":
-            thumb = thumb.convert("RGB")
+    thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
+
+    thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
+    with thumb_file:
+        thumb.save(thumb_file, "JPEG")
+
+    """
+    Create medium file, used in `media.html`
+    """
+    medium = Image.open(queued_filename)
+    medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
+
+    if medium.mode != "RGB":
+        medium = medium.convert("RGB")
 
-        thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
+    medium_filepath = create_pub_filepath(entry, 'medium.jpg')
 
-        thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
-        with thumb_file:
-            thumb.save(thumb_file, "JPEG")
+    medium_file = mgg.public_store.get_file(medium_filepath, 'w')
+    with medium_file:
+        medium.save(medium_file, "JPEG")
 
     # we have to re-read because unlike PIL, not everything reads
     # things in string representation :)
@@ -73,6 +86,7 @@ def process_media_initial(media_id):
     media_files_dict = entry.setdefault('media_files', {})
     media_files_dict['thumb'] = thumb_filepath
     media_files_dict['main'] = main_filepath
+    media_files_dict['medium'] = medium_filepath
     entry['state'] = u'processed'
     entry.save()
 
index 3cebe2f9a27a4a9acf6373d2e959c6d400646b8a..e16f1e002808c9e598453ec316313ac373e984f3 100644 (file)
       <h1>
         {{media.title}}
       </h1>
-      <img class="media_image" src="{{ request.app.public_store.file_url(
-                    media.media_files.main) }}" />
+      {% if media.media_files.medium %}
+        <img class="media_image" src="{{ request.app.public_store.file_url(
+                                     media.media_files.medium) }}" />
+      {% else %}
+        <img class="media_image" src="{{ request.app.public_store.file_url(
+                  media.media_files.main) }}" />
+      {% endif %}
       <p>
         Uploaded on
         {{ "%4d-%02d-%02d"|format(media.created.year,