简体   繁体   中英

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

If a user has no photo facebook instead of a jpeg get a gif. How I can check the type?

I have tried the following:

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

This works but replaces all my template (the remaining elements of php) and only shows photo of accused imagejpg method imagegif etc. ..

Ok.

The solution is simple .. just use the php method 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';
}

You can use exif_imagetype

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

$type can be IMAGETYPE_GIF,IMAGETYPE_JPEG,IMAGETYPE_PNG,... . This method is much faster than getimagesize . It will output false if your file is not an image.

You can read headers with get_headers

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

Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM