/**
 * Generate the cover image file.
 *
 * @since 2.4.0
 *
 * @param  array  $args {
 *     @type string $file            The absolute path to the image. Required.
 *     @type string $component       The component for the object (eg: groups, xprofile). Required.
 *     @type string $cover_image_dir The Cover image dir to write the image into. Required.
 * }
 * @param  BP_Attachment_Cover_Image $cover_image_class The class to use to fit the cover image.
 * @return bool|array          An array containing cover image data on success, false otherwise.
 */
function bp_attachments_cover_image_generate_file($args = array(), $cover_image_class = null)
{
    // Bail if an argument is missing
    if (empty($args['file']) || empty($args['component']) || empty($args['cover_image_dir'])) {
        return false;
    }
    // Get advised dimensions for the cover image
    $dimensions = bp_attachments_get_cover_image_dimensions($args['component']);
    // No dimensions or the file does not match with the cover image dir, stop!
    if (false === $dimensions || $args['file'] !== $args['cover_image_dir'] . '/' . wp_basename($args['file'])) {
        return false;
    }
    if (!is_a($cover_image_class, 'BP_Attachment_Cover_Image')) {
        $cover_image_class = new BP_Attachment_Cover_Image();
    }
    // Make sure the file is inside the Cover Image Upload path.
    if (false === strpos($args['file'], $cover_image_class->upload_path)) {
        return false;
    }
    // Resize the image so that it fit with the cover image dimensions
    $cover_image = $cover_image_class->fit($args['file'], $dimensions);
    $is_too_small = false;
    // Image is too small in width and height
    if (empty($cover_image)) {
        $cover_file = $cover_image_class->generate_filename($args['file']);
        @rename($args['file'], $cover_file);
        // It's too small!
        $is_too_small = true;
    } elseif (!empty($cover_image['path'])) {
        $cover_file = $cover_image['path'];
        // Image is too small in width or height
        if ($cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height']) {
            $is_too_small = true;
        }
    }
    // We were not able to generate the cover image file.
    if (empty($cover_file)) {
        return false;
    }
    // Do some clean up with old cover image, now a new one is set.
    $cover_basename = wp_basename($cover_file);
    if ($att_dir = opendir($args['cover_image_dir'])) {
        while (false !== ($attachment_file = readdir($att_dir))) {
            // skip directories and the new cover image
            if (2 < strlen($attachment_file) && 0 !== strpos($attachment_file, '.') && $cover_basename !== $attachment_file) {
                @unlink($args['cover_image_dir'] . '/' . $attachment_file);
            }
        }
    }
    // Finally return needed data.
    return array('cover_file' => $cover_file, 'cover_basename' => $cover_basename, 'is_too_small' => $is_too_small);
}