如何从枪口请求中获取和显示图像

如何从枪口请求中获取和显示图像

本文介绍了如何从枪口请求中获取和显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示来自大量需求的图像,一切正常,但是我无法显示该图像.

I am trying to display an image from a guzzle request, everything worked fine but I am failing to display the image.

这是我尝试获取用户数据时得到的结果

This is the result I get, when trying to get users' data

object(GuzzleHttp\Psr7\Response)#427 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(14) { ["Server"]=> array(1) { [0]=> string(6) "Cowboy" } ["Connection"]=> array(1) { [0]=> string(10) "keep-alive" } ["X-Powered-By"]=> array(1) { [0]=> string(7) "Express" } ["X-Timestamp"]=> array(1) { [0]=> string(13) "1556374181931" } ["Content-Disposition"]=> array(1) { [0]=> string(51) "attachment; filename =profile_img-1556366764744.jpg" } ["X-Sent"]=> array(1) { [0]=> string(4) "true" } ["Accept-Ranges"]=> array(1) { [0]=> string(5) "bytes" } ["Cache-Control"]=> array(1) { [0]=> string(17) "public, max-age=0" } ["Last-Modified"]=> array(1) { [0]=> string(29) "Sat, 27 Apr 2019 12:06:07 GMT" } ["Etag"]=> array(1) { [0]=> string(21) "W/"12d37-16a5eb034ec"" } ["Content-Type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["Content-Length"]=> array(1) { [0]=> string(5) "77111" } ["Date"]=> array(1) { [0]=> string(29) "Sat, 27 Apr 2019 14:09:41 GMT" } ["Via"]=> array(1) { [0]=> string(9) "1.1 vegur" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(14) { ["server"]=> string(6) "Server" ["connection"]=> string(10) "Connection" ["x-powered-by"]=> string(12) "X-Powered-By" ["x-timestamp"]=> string(11) "X-Timestamp" ["content-disposition"]=> string(19) "Content-Disposition" ["x-sent"]=> string(6) "X-Sent" ["accept-ranges"]=> string(13) "Accept-Ranges" ["cache-control"]=> string(13) "Cache-Control" ["last-modified"]=> string(13) "Last-Modified" ["etag"]=> string(4) "Etag" ["content-type"]=> string(12) "Content-Type" ["content-length"]=> string(14) "Content-Length" ["date"]=> string(4) "Date" ["via"]=> string(3) "Via" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#425 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(350) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } }
public function getUserProfileImg(Request $request){

    $userid = $_COOKIE['id'];

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

    //$result = $requestResult->getBody()->readfile($filename);

    //$result = $requestResult->getBody();
    //$body = json_decode((string) $requestResult->file($pathToFile, $headers), true);

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

    //var_dump($result);
}

我要一张照片

推荐答案

一旦您从耗材中获得了图像的响应,

Once you obtain the response for the image from guzzle,

$client = new \GuzzleHttp\Client(['base_uri' => 'https://www.example.com']);
$response = $client->request('GET', 'wallpaper/wallpaper-preview.jpg', ['stream' => true]);

然后您可以将其编码为base64并以某种格式发送.您可以使用php提供的 base64_encode()函数./p>

then you can encode it as base64 and send it with a format. You can use base64_encode() function provided by php.

$body = $response->getBody()->getContents();
$base64 = base64_encode($body);
$mime = "image/jpeg";
$img = ('data:' . $mime . ';base64,' . $base64);
return "<img src=$img alt='ok'>";

额外

您可以使用 $ response-> getHeader('Content-Type')[0];

这篇关于如何从枪口请求中获取和显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:54