""" A template filter which wraps imagemagick's `convert` command. The filter acts upon a source image path, and returns the filtered image path. usage: {{ source_path|convert:"-resize 64x64\!" }} Example setting: IMAGE_CACHE_PATH = "image_cache/" I have tried to eliminate command injection from a template by escaping the parameters, but am not an expert in such things - if anyone spots any holes, please leave a comment or email to greg AT interactionconsortium DOT com. """ import os from django import template from django.conf import settings import commands register = template.Library() """ A convert filter. Takes the file name (relative to MEDIA_ROOT), and a specification of the conversion Returns a file name (relative to MEDIA_ROOT) of the converted file. Pseudocode for filter: 1. generate the result filename. 2. does it exist? Yes = return it. No = create it. 3. do the conversion; save the file as the result filename. """ @register.filter def convert(original_image_path, arg): if not original_image_path: return '' if arg == "": return original_image_path basename, format = original_image_path.rsplit(".", 1) basename, name = basename.rsplit(os.path.sep, 1) dest_folder = os.path.join(settings.IMAGE_CACHE_PATH, basename) abs_dest_folder = os.path.join(settings.MEDIA_ROOT, dest_folder) arghash = str(hash(arg)) dest_path = os.path.join(dest_folder, name+"_"+arghash+"."+format) abs_dest_path = os.path.join(settings.MEDIA_ROOT, dest_path) if os.path.exists(abs_dest_path): return dest_path if not os.path.exists(abs_dest_folder): os.makedirs(abs_dest_folder) abs_source_path = os.path.join(settings.MEDIA_ROOT, original_image_path) if not os.path.exists(abs_source_path): return "%s<-NOT FOUND" % original_image_path #escape strings so as to avoid injections c_source = str(abs_source_path).encode('string-escape') c_arg = str(arg).encode('string-escape') cmd = "convert %s %s %s" % (c_source, c_arg, abs_dest_path) status, result = commands.getstatusoutput(cmd) if status == 0: return dest_path else: return "%s<-IMAGEMAGICK RETURNED STATUS %s" % (original_image_path, status)