'Create a thumbnail for a given image.
Returns a tuple of (StaticContent, AssetKey)
`content` is the StaticContent representing the image you want to make a
thumbnail out of.
`tempfile_path` is a string path to the location of a file to read from
in order to grab the image data, instead of relying on `content.data`
`dimensions` is an optional param that represents (width, height) in
pixels. It defaults to None.'

def generate_thumbnail(self, content, tempfile_path=None, dimensions=None):

    thumbnail_content = None
    is_svg = (content.content_type == 'image/svg+xml')
    thumbnail_name = StaticContent.generate_thumbnail_name(content.location.name, dimensions=dimensions, extension=('.svg' if is_svg else None))
    thumbnail_file_location = StaticContent.compute_location(content.location.course_key, thumbnail_name, is_thumbnail=True)
    try:
        if is_svg:
            if (tempfile_path is None):
                thumbnail_file = StringIO.StringIO(content.data)
            else:
                with open(tempfile_path) as f:
                    thumbnail_file = StringIO.StringIO(f.read())
            thumbnail_content = StaticContent(thumbnail_file_location, thumbnail_name, 'image/svg+xml', thumbnail_file)
            self.save(thumbnail_content)
        elif ((content.content_type is not None) and (content.content_type.split('/')[0] == 'image')):
            if (tempfile_path is None):
                source = StringIO.StringIO(content.data)
            else:
                source = tempfile_path
            thumbnail_file = StringIO.StringIO()
            with Image.open(source) as image:
                thumbnail_image = image.convert('RGB')
                if (not dimensions):
                    dimensions = (128, 128)
                thumbnail_image.thumbnail(dimensions, Image.ANTIALIAS)
                thumbnail_image.save(thumbnail_file, 'JPEG')
                thumbnail_file.seek(0)
            thumbnail_content = StaticContent(thumbnail_file_location, thumbnail_name, 'image/jpeg', thumbnail_file)
            self.save(thumbnail_content)
    except Exception as exc:
        logging.exception(u'Failed    to    generate    thumbnail    for    {0}.    Exception:    {1}'.format(content.location, str(exc)))
    return (thumbnail_content, thumbnail_file_location)
