def serve_crop(request, filename, width, height, root=settings.MEDIA_ROOT): width, height = int(width), int(height) filename = filename.replace("/../", "/") filename = filename.replace("//", "/") while filename.startswith(os.sep): filename = filename[1:] image = Image.open(filename, root=root) image = image.image.transform((width, height), ImageCalc.EXTENT, (0, 0, image.image.size[0], image.image.size[1])) # Return the response response = HttpResponse(mimetype="image/jpg") image.save(response, "JPEG") return response
def serve_image(request, filename, width=None, height=None, root=settings.MEDIA_ROOT): width, height = int(width), int(height) # Sanitize file path filename = filename.replace("/../", "/") filename = filename.replace("//", "/") while filename.startswith(os.sep): filename = filename[1:] # open with PIL image = Image.open(filename, root=root) if width: # Scale height if not height: height = image.scale_height(width) resized = image.resize(width, height) else: resized = image.image # Return the response response = HttpResponse(mimetype="image/jpg") resized.save(response, "JPEG") return response