Timber\ImageHelper::sideload_image PHP Method

sideload_image() public static method

downloads an external image to the server and stores it on the server
public static sideload_image ( string $file ) : string
$file string the URL to the original file
return string the URL to the downloaded file
    public static function sideload_image($file)
    {
        $loc = self::get_sideloaded_file_loc($file);
        if (file_exists($loc)) {
            return URLHelper::file_system_to_url($loc);
        }
        // Download file to temp location
        if (!function_exists('download_url')) {
            require_once ABSPATH . '/wp-admin/includes/file.php';
        }
        $tmp = download_url($file);
        preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
        $file_array = array();
        $file_array['name'] = basename($matches[0]);
        $file_array['tmp_name'] = $tmp;
        // If error storing temporarily, unlink
        if (is_wp_error($tmp)) {
            @unlink($file_array['tmp_name']);
            $file_array['tmp_name'] = '';
        }
        // do the validation and storage stuff
        $locinfo = pathinfo($loc);
        $file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name']));
        return $file['url'];
    }