我如何检查类型(jpeg, gif .)与facebook的url


PHP-GD How I can check the type (jpeg, gif ..) with a url of facebook?

如果用户没有照片facebook而不是jpeg得到一个gif。我怎么查型号?

我已经试过了:

$PIC_Friend = imagecreatefromstring(file_get_contents('http://graph.facebook.com/'. id .'/picture?width=50&height=50')); 
if(imagejpeg($PIC_Friend)) { 
..
} elseif (imagegif($PIC_Friend)) { 
...

这个工作,但取代了我所有的模板(php的其余元素),只显示被指控的imagejpg方法imagegif等的照片。

好的。

解决办法很简单…只需使用php方法exif_imagetype

http://php.net/manual/en/function.exif-imagetype.php

$img_info = getimagesize($file_path);
if($img_info['mime'] == 'image/pjpeg' || $img_info['mime'] == 'image/jpeg') {
    echo 'It is a .jpeg';
}

您可以使用exif_imagetype

$type = exif_imagetype('path/to/yourImage');

$type可以是IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG,...。这种方法比getimagesize法快得多。如果您的文件不是图像,它将输出false

您可以使用get_headers读取标题

$headers = get_headers('http://graph.facebook.com/'. id .'/picture?width=50&height=50');

array(22) 
{
  [0]=> string(23) "HTTP/1.1 302 forced.302"
  ...
  [14]=> string(24) "Content-Type: image/jpeg"
  ...
  string(17) "Connection: close"
}