Reputation: 299
Hi I want to display private image in my project by sending API with jwt token. I use postman to test. Postman return an image but How can I display image like post man.
Here Is my code Controller sendApi
public function getIdCard($name) {
$token = $this->getToken();
$client = new Client(['base_uri' => env('URL_FRONTEND')]);
$headers = [
'Authorization' => 'Bearer ' . $token,
];
$response = $client->request('GET', 'storage/getIdCard/icQE5nwU1jQl1lrIZJmGxVs2GDIIsDjbuQo541VlwzhNTl7iEe1555385649.png', [
'headers' => $headers
]);
$response = $response->getBody()->getContents();
return $response;
}
Here is my API/ Controller [ from another project]
public function getIdCard($file_name){;
$storagePath = storage_path('app/id_card/'. $file_name);
return Image::make($storagePath)->response();
}
Here is what I get
PNG IHDR��B5 pHYs���+ IDATx��yx�U�?��Nw�@B�o�Ha"ʦ� ����(.����� ��l^t��늨�D�EYe5Q�d5l��,$d�������'U眷�~��ɓ~�R���T�yC���t� ��b���`�$���z�Á�� ->W� ]�+��*������L�����"y��Hn��yj��H�Ma�ɗ�"��x��ǹϺxt�7�8���H��M�_0�ҿs�/<��o�X����h*,��,5e�Ή��1{�e��pS�,�M����z��)�R8fd5����\����SƗ�r�:J�j�ǸO�UfK�d�U��vR:��ƛ7*��v2�������dS�T礱����=c/j��\�kus��:������,�DE�Z��E���R)Y�AT���!~��(�s�m@ͻ��M:"&n�����d���/�8��]�1�d���[� +5�s.tyǩ)��lF��ĬJ���us����[c�F:�U����%����/���*]���B�'Q��2]:X������q���R_�5N���"n�j� �[����}W}��#�7��{Va?��íQ���]5�����_J���¤��6NG�-͜5n?#�8Ug�z��esoT��]+����!]�E]�x����D=[�N�K���8��*5�3Ήrd�p�ps:�#%��O��-�\E^:gNg^��rq��O��iPT�\q�(�_�3{84�����kWH�o��Zw��/�Y�+��r��
In Postman It's return Image How can I display in like postman
UPDATE I add function in Api/controller still not working
public function getIdCard($file_name){;
$storagePath = storage_path('app/id_card/'. $file_name);
$headers = [
"Content-type: image/png",
];
return response()->file($storagePath, $headers);
}
**UPDATE 2 **
I'm working on 2 project - The first project I return API return response()->file($storagePath, $headers); -Then I use post man and it's return image - The second project I want to get image from API and I use
$response = $response->getBody()->getContents();
It's return text/html How to display it into image
Upvotes: 1
Views: 2753
Reputation: 71
Here is the solution:
$data = $response->getBody()->getContents();
$base64 = 'data:image/png;base64,' . base64_encode($data);
echo '<img src="'.$base64.'">';
Upvotes: 4
Reputation: 1707
I don't really know what's your Image class, so I would say how to do it with built in functions.
return response()->file($pathToFile, $headers);
Most probably with calling the file method you won't need any headers (as second param) but in case you need one, try to use something like ["Content-type: image/png"]
Upvotes: 0