'Gets an image from the PMS and saves it to the image cache directory.
Required parameters:
img (str):              /library/metadata/153037/thumb/1462175060
or
rating_key (str):       54321
Optional parameters:
width (str):            150
height (str):           255
fallback (str):         "poster", "cover", "art"
refresh (bool):         True or False whether to refresh the image cache
Returns:
None'

@addtoapi('pms_image_proxy')
def real_pms_image_proxy(self, img='', rating_key=None, width='0', height='0', fallback=None, refresh=False, **kwargs):

    if ((not img) and (not rating_key)):
        logger.error('No    image    input    received.')
        return
    if (rating_key and (not img)):
        img = ('/library/metadata/%s/thumb/1337' % rating_key)
    img_string = (img.rsplit('/', 1)[0] if ('/library/metadata' in img) else img)
    img_string += ('%s%s' % (width, height))
    fp = hashlib.md5(img_string).hexdigest()
    fp += '.jpg'
    c_dir = os.path.join(plexpy.CONFIG.CACHE_DIR, 'images')
    ffp = os.path.join(c_dir, fp)
    if (not os.path.exists(c_dir)):
        os.mkdir(c_dir)
    try:
        if ((not plexpy.CONFIG.CACHE_IMAGES) or refresh or ('indexes' in img)):
            raise NotFound
        return serve_file(path=ffp, content_type='image/jpeg')
    except NotFound:
        try:
            pms_connect = pmsconnect.PmsConnect()
            result = pms_connect.get_image(img, width, height)
            if (result and result[0]):
                cherrypy.response.headers['Content-type'] = result[1]
                if (plexpy.CONFIG.CACHE_IMAGES and ('indexes' not in img)):
                    with open(ffp, 'wb') as f:
                        f.write(result[0])
                return result[0]
            else:
                raise Exception(u'PMS    image    request    failed')
        except Exception as e:
            logger.warn((u'Failed    to    get    image    %s,    falling    back    to    %s.' % (img, fallback)))
            fbi = None
            if (fallback == 'poster'):
                fbi = common.DEFAULT_POSTER_THUMB
            elif (fallback == 'cover'):
                fbi = common.DEFAULT_COVER_THUMB
            elif (fallback == 'art'):
                fbi = common.DEFAULT_ART
            if fbi:
                fp = os.path.join(plexpy.PROG_DIR, 'data', fbi)
                return serve_file(path=fp, content_type='image/png')
