0

I have a third-party API that returns a JSON response when fetching an image file. The response has a key named content. This key contains the binary data of the image asked for.

I get this response using cURL in PHP, then json_decode the response.

I get the content key, get the contents out of that and save it in a variable.

Then, I pass this string to imagecreatefromstring but it always returns false.

What's wrong here? How can I convert that binary data into an image resource?

If anyone has a shortcut, ultimately, I need to store that image at a specific location on the File System.

The response that I get is here: https://gist.github.com/abhisheksoni27/2949f42eab5851ad75e77026fe53be87

<?php
$file_name = 'FILE_NAME_HERE';
$url = API_URL_HERE . $file_name;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$response = curl_exec($ch);

$resJson = json_decode($response, true);

$image_content = $resJson["content"];

$image = imagecreatefromstring($image_content);

var_dump($image); // this is always false
code
  • 2,115
  • 1
  • 22
  • 46
  • Can you show the `$image_content` you are getting? JSON? Using Base64 encoding? – ficuscr Jun 07 '19 at 16:25
  • The response I am getting is here: https://gist.github.com/abhisheksoni27/2949f42eab5851ad75e77026fe53be87 – code Jun 07 '19 at 16:26
  • It's not base64, by the looks of it. – code Jun 07 '19 at 16:28
  • 1
    [You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software). It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Jun 07 '19 at 16:31
  • 1
    @Dharman Not really solving the problem here, but thanks for your suggestion. This is a test script, and I am running it locally? – code Jun 07 '19 at 16:32
  • imagecreatefromstring() - An image resource will be returned on success. FALSE is returned if the image type is unsupported, the data is not in a recognised format, or the image is corrupt and cannot be loaded. I am getting the message `the data is not in a recognised format` – A l w a y s S u n n y Jun 07 '19 at 16:41
  • 1
    Why are you using `$resJson[$file_uuid]["content"]`? That response is a flat object: `$resJson["content"]` – miken32 Jun 07 '19 at 16:43
  • Can't make sense of the data. Would look at pngcheck or something on the original source. – ficuscr Jun 07 '19 at 17:29
  • 2 problems, **1.** This is not a valid PNG file, first byte of a valid PNG ffile should be 0x89. **2.** it contains binary data and encoded into a string, but php's `json_decode` function will recognize it as a UTF-8 string, it cannot handle it as a binary data. you may decode it manually or ask the provider to return base64-encoded string. – shingo Jun 08 '19 at 06:35
  • @shingo How do I decode it manually? – code Jun 08 '19 at 09:37
  • Write some code to parse the data, eg: `\u00c2 -> chr(0xc2)` – shingo Jun 08 '19 at 10:02

0 Answers0