Devise\Media\MediaPaths::downloadFromUrl PHP Method

downloadFromUrl() public method

Downloads the image so that we can re-use it later and crop it and resize it accordingly
public downloadFromUrl ( $path, $newFilePath = null ) : string
$path
return string
    public function downloadFromUrl($path, $newFilePath = null)
    {
        $tmpFilePath = tempnam(sys_get_temp_dir(), 'dvs_downloaded_field');
        try {
            $ch = curl_init($path);
            $fp = fopen($tmpFilePath, 'wb');
            curl_setopt($ch, CURLOPT_FILE, $fp);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);
            // add the extension to this temp file
            // and rename to a md5 string so it is unique
            // in case we download this file again
            $ext = image_type_to_extension(exif_imagetype($tmpFilePath));
            $md5 = md5_file($tmpFilePath);
            $newFilePath = $newFilePath ?: sys_get_temp_dir() . '/' . $md5 . $ext;
            rename($tmpFilePath, $newFilePath);
            return $newFilePath;
        } catch (\Exception $e) {
        }
        return $path;
    }