如何处理php生成的图像(没有扩展名)?

时间:2014-03-22 04:38:59

标签: php curl

是否可以使用以下类型的图片网址?

http://product-images.barneys.com/is/image/Barneys/503230930_product_1

目前,我使用以下代码来确定远程图像格式。但我不知道如何处理上述例子。这就是一个例子。通常他们会为动态图像大小调整而做。

if($source['extension'] == 'png') {
$type = 'image/png';
}

2 个答案:

答案 0 :(得分:1)

<?php
$f = tempnam("./", "TMP0");
file_put_contents($f,file_get_contents("http://product-images.barneys.com/is/image/Barneys/503230930_product_1"));
if(getimagesize($f)){
$type = 'image/png';
}

$f文件现在图片已执行您想要的操作或使用unlink($f);删除它

答案 1 :(得分:1)

您可以使用finfo::file扩展名。您不需要在此上下文中使用cURL

<?php
$remoteImgTempName = 'someimg';
file_put_contents($remoteImgTempName,file_get_contents('http://product-images.barneys.com/is/image/Barneys/503230930_product_1'));
echo finfo_file(finfo_open(FILEINFO_MIME_TYPE), $remoteImgTempName);

<强> OUTPUT :

image/jpeg