我正在尝试从提供的链接中下载GooglePlay应用的图标。
例如。https://lh5.ggpht.com/j0y0xf18PF8iZ_qyKekah11Gg7fteqhqm_VC0SQg7oMsIyMPato7Z_zBsGmOtTf2Fw=w300
现在,当我使用以下代码使用cURL下载图像时
function scaleImageAndSaveIt($appName,$imageURL){
$format="_%d_%m_%Y_%H_%M_%S";
$strf=strftime($format);
$imageLocnName = $appName . $strf ;
$ch = curl_init($imageURL);
$fp = fopen($imageLocnName, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, true);
$resp = curl_exec($ch);
echo $resp;
curl_close($ch);
fclose($fp);
return $imageLocnName; }
现在,当我尝试保存图像时,我没有任何文件扩展名。(.png或.jpg或其他任何格式)。但是,当我手动保存图像时,我将图像扩展名转换为PNG。
如何下载具有默认图像名称和扩展名的图像,或者如何找到扩展名。任何解决方案都对我有帮助。
尝试这样的事情,并根据返回值确定要使用的扩展名:
function GetMimeType($path)
{
//$type = mime_content_type($file); //deprecated
/* //file info -> normal method, but returns wrong values for ics files..
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
$type = $filename.":".finfo_file($finfo, $filename);
finfo_close($finfo);
*/
$forbiddenChars = array('?', '*', ':', '|', ';', '<', '>');
if(strlen(str_replace($forbiddenChars, '', $path)) < strlen($path))
throw new \Exception("Forbidden characters!");
$path = escapeshellarg($path);
ob_start();
$type = system("file --mime-type -b ".$path);
ob_clean();
return $type;
}
function GuessExtension($path)
{
$type = GetMimType($path);
$extension = "";
switch($type)
{
case "image/png":
$extension = ".png";
break;
case "image/jpeg":
default:
$extension = ".jpg";
break;
}
}
//use it like this:
var_dump(GuessExtension("/path/to/your/saved/file"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句