Facebook SDK return profile picture as JFIF, how to convert to JPG/PNG in PHP?

1.3k Views Asked by At

I'm using cURL to get an image from the Facebook SDK, but the image provided is in JFIF format. I tried to use some functions to convert it, as imagejpeg() and imagepng(), but my host don't support these. I also tried to decode the file as described here, but no success either. Any help or suggestions will be appreciated.

//Image handler
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $jsonresponse['url']);
$data = curl_exec($ch);
curl_close($ch);

$dataDecoded = base64_decode($data); //Last try with base64 decoder
$fb_image = 'fb_picture.jpg';
file_put_contents($fb_image, $dataDecoded); 

I got no errors, but the saved image (fb_picture.jpg) isn't readable and have 0KB.

1

There are 1 best solutions below

4
On

JFIF is already JPEG with aditional metadata about the person in the photo. See https://www.rfc-editor.org/rfc/rfc2798#page-5 for more info. JFIF is used specifically for pictures of persons. Your host needs imagejpeg() and bundle to work with jpeg. Install it as a PHP module.