Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

php - How to get and display an image from a guzzle request

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(GuzzleHttpPsr7Response)#427 (6) { ["reasonPhrase":"GuzzleHttpPsr7Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttpPsr7Response":private]=> int(200) ["headers":"GuzzleHttpPsr7Response":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":"GuzzleHttpPsr7Response":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":"GuzzleHttpPsr7Response":private]=> string(3) "1.1" ["stream":"GuzzleHttpPsr7Response":private]=> object(GuzzleHttpPsr7Stream)#425 (7) { ["stream":"GuzzleHttpPsr7Stream":private]=> resource(350) of type (stream) ["size":"GuzzleHttpPsr7Stream":private]=> NULL ["seekable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["readable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["writable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["uri":"GuzzleHttpPsr7Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttpPsr7Stream":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);
}

I am expecting an image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Once you obtain the response for the image from guzzle,

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

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

'data:image/jpeg;base64,your-base-64-encodedstring'
example : 'data:image/gif;base64,R0lGODlhAAEwAMQAAJ2M5Me98GRK1DoYyYBr3PHv++Pe99XO81Y50auc6PBkZEgpzbmt7HJa2I57fS3CqU7XGYgE+GqHvrLJ8Tr6qXmqiwAF9CffgnMNqmWHAWNBwwGsKpKsrmJqltOOV69nuYxSkqpoTata18rWtrr1rTIIAQA7'

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

Extra

you can obtain mime type($mime in above code snippet) from response using, $response->getHeader('Content-Type')[0];


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...