Permalink
Browse files

Losslessly optimize JPEGs.

Disabled by default in example.ini. Install jpegoptim
or an equivalent and set the configuration properly.
  • Loading branch information...
1 parent 78ee90c commit f5b6839a78b27511aba1fbef74c5372e2af4a9db @spladug spladug committed Jun 1, 2012
Showing with 13 additions and 0 deletions.
  1. +2 −0 r2/example.ini
  2. +11 −0 r2/r2/lib/media.py
View
@@ -349,6 +349,8 @@ translator_password =
takedown_sr = _takedowns
# png compressor
png_optimizer = /usr/bin/env optipng
+# jpeg compressor
+jpeg_optimizer =
# -- search --
# where is solor?
View
@@ -20,6 +20,8 @@
# CondeNet, Inc. All Rights Reserved.
################################################################################
+import subprocess
+
from pylons import g, config
from r2.models.link import Link
@@ -50,6 +52,13 @@
MEDIA_FILENAME_LENGTH = 12
+def optimize_jpeg(filename, optimizer):
+ if optimizer:
+ with open(os.path.devnull, 'w') as devnull:
+ subprocess.check_call((optimizer, filename),
+ stdout=devnull)
+
+
def thumbnail_url(link):
"""Given a link, returns the url for its thumbnail based on its fullname"""
if link.has_thumbnail:
@@ -116,6 +125,8 @@ def upload_media(image, never_expire=True, file_type='.jpg'):
if file_type == ".png":
optimize_png(f.name, g.png_optimizer)
+ elif file_type == ".jpg":
+ optimize_jpeg(f.name, g.jpeg_optimizer)
contents = open(f.name).read()
file_name = get_filename_from_content(contents)
if g.media_store == "s3":

0 comments on commit f5b6839

Please sign in to comment.