Download Images Using cURL

public function download_image($url){
	//logg($url);
	$curl = curl_init();

	//$header[0]  = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
	//$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

	$header = [];
	$header[] = "Cache-Control: max-age=0"; 
	$header[] = "Connection: keep-alive"; 
	$header[] = "Keep-Alive: 300"; 
	$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
	$header[] = "Accept-Language: en-us,en;q=0.5"; 
	$header[] = "Pragma: "; // browsers = blank
	//$header[] = "X_FORWARDED_FOR: " . $ip;
	//$header[] = "REMOTE_ADDR: " . $ip;
	//$header[] = "Host: www.example.pro";//加上Host抓到的圖片都是壞的
	//logg($header);

	curl_setopt_array($curl, array(
		CURLOPT_URL => $url,
		//User Agent內容不同會造成抓圖錯誤,試了好幾個目前這個才能正常抓圖
		CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 
		CURLOPT_HTTPHEADER => $header,
		CURLOPT_REFERER => 'www.example.pro', 
		CURLOPT_ENCODING => 'gzip,deflate', 
		CURLOPT_AUTOREFERER => true, 
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_TIMEOUT => 300,//seconds, execution
		CURLOPT_CONNECTTIMEOUT => 10, //seconds, 0 to wait indefinitely.
		CURLOPT_VERBOSE => 1, 
		//CURLOPT_HEADER => false, 
		CURLOPT_CUSTOMREQUEST => 'GET',
		CURLOPT_POST => false, 
		CURLOPT_BINARYTRANSFER => true, 
	));

	// 回傳的是JSON String, 所以需要json_decode()
	$image = curl_exec($curl);
	//logg(curl_error($curl));
	//curl_close($curl) ;


	//連domain後的路徑一併存下來
	$parse = parse_url($url);
	//logg($parse);

	$dest_dir = storage_path('app/' . $this->user_id . dirname($parse['path']));
	$file_name = basename($parse['path']);
	$file = $dest_dir . '/' . $file_name;
	//logg($dest_dir);

	//$old = umask(0); 
	if(file_exists($dest_dir) == false){
		mkdir($dest_dir, 0755, true);
	}
	//umask($old);
	//logg($dest_dir);

	//Storage::put($dest_dir . '/' . $file_name, $image);
	if(file_exists($file) == false){
		file_put_contents($dest_dir . '/' . $file_name, $image);
	}
}