def _resize(im, width, height):
    """
    Resizes the image to fit within the specified height and width; aspect ratio
    is preserved. Images always preserve animation and might even result in a
    better-optimized animated gif.
    """
    # resize only if we need to; return None if we don't
    if im.size.width > width or im.size.height > height:
        im = im.resized(im.size.fit_inside((width, height)))
        return im