ephramd
ephramd

Reputation: 380

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. ..

Upvotes: 1

Views: 186

Answers (4)

Bora
Bora

Reputation: 10717

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"
}

Upvotes: 0

Lewis
Lewis

Reputation: 14906

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.

Upvotes: 0

Samuil Banti
Samuil Banti

Reputation: 1795

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

Upvotes: 0

ephramd
ephramd

Reputation: 380

Ok.

The solution is simple .. just use the php method exif_imagetype.

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

Upvotes: 1

Related Questions