显示从耗时请求获得的BLOB文件(图像)

时间:2019-04-28 21:45:00

标签: laravel blob guzzle

我已经能够从guzzle的API端点成功获取图像(存储为BLOB文件),但是在将此BLOB文件显示为图像时遇到了问题

我已经尝试了几种PHP函数,用于将BLOB文件转换为图像,但是所有这些函数均已中止。

after sending the request to my guzzle trait, I decided to var_dump the result to be sure that it was successful   

    $userid = $_COOKIE['id'];

    $url = 'users/image/'.$userid;

    $requestResult = $this->sendGetWithHeader('users/image/'.$userid);


    $result = $requestResult->getBody()->read(1024);


    //$res = file_get_contents($result);



    // fopen($result, 0);

    var_dump($result);

    echo "<hr>";

    var_dump($requestResult);

    // $code = $requestResult->getStatusCode();
    // var_dump($code);

我的预期结果是一个图像文件,但实际仍然是从我的Guzzle请求中获得的原始BLOB文件

string(1024)“����JFIF��� ��V�''��6���UG_ks1��r�[%��JMd�CL,�� 8�Q�B�yoA�g�p�a�f�D�)8 @�。)ɠ�yea@4��ԃ(��t��Ew��մPV'J�� R��u������Ju��U��ԥ��zѩaė��ya$�2�C�Sa�R$�4�����2t��$��L�D。 �R�$�ґ-&RVdm��IDq)Jc�'9'; /�sb{��Z�r�̮��Q_MR=5�Gt���ddA�6�Fa�L� ��JGikh�d�FA�%(�$������.��H�)��-BL������h|#��{�WW)݋r�"V[V2˚:��j��˩�>��-$��I�Y�@%dRL(K�(�-�m�I�t��R�JV@�I&JR��0����O2��d�-e@5�2}�2�D'��,Bע��{��!�k�Uw�2ʶ�]cAD�WPw���$��!�Ғu�X!��(�D%I0i�����Lye XI9) �[Nc����@!�miY,Rkiq (���$����mƬ��y��a����崔͒�t7“����͌ΊJgkQ��Ah�Iq$���A�FmJ���7��AJ a /rB.``02A��@@d��J%D�P�yH���m\ż>�i$��I。(�3jm�542k,��t#。 �@ D @F�TJDP''�Z(��7�IZ���h9)$��$�Y�m�D�I�֐4���Da�%DX oo.fhh..c��aQhcΉ�٠。 �)��''

2 个答案:

答案 0 :(得分:0)

看这个例子-https://github.com/andriichuk/curl-examples#download-file

使用“接收器”选项保存请求中的文件:

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

$imageFilePath = __DIR__ . '/resource/image.jpeg';
$imageFileResource = fopen($imageFilePath, 'w+');

$httpClient = new Client();
$response = $httpClient->get(
    'https://httpbin.org/image/jpeg',
    [
        RequestOptions::SINK => $imageFileResource,
    ]
);

if ($response->getStatusCode() === 200) {
    echo 'The image has been successfully downloaded: ' . $imageFilePath;
}

答案 1 :(得分:0)

将您的 blob 响应保存到文件中,如下所示

$stream = \GuzzleHttp\Psr7\Utils::streamFor($requestResult->getBody());  

file_put_contents('path/to/file.extension',$stream->getContents())

Convert blog image to file