簡體   English   中英

PHP-GD如何使用Facebook的網址檢查類型(jpeg,gif ..)?

[英]PHP-GD How I can check the type (jpeg, gif ..) with a url of 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"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM