无法使用PHP在HTML标记中显示base64解码图像

问题描述 投票:0回答:1

为什么我不能在HTML标签内显示图像?。

<?php
session_start();

$ch = curl_init( 'https://mighty-inlet-78383.herokuapp.com/api/hotels/imagedata');
curl_setopt_array($ch, array(
    CURLOPT_RETURNTRANSFER => TRUE
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
    echo 'No responce';
}

// Decode the response
$responseData = json_decode($response, true);

// get data from api to json
foreach($responseData AS $response) {

    $hotelImage = $response['hotelImage'];

    $imgdecord = base64_decode($hotelImage). "\n"; 

    if(empty($email)){
        echo "No responce from server";
    }
    else{
        echo '<br>';
        echo'
            <html>
                <head></head>
                <body>
                <img src="data:image/png;base64,' . $imgdecord . '" />
                </body>
            </html>    
        ';
        }
}

?>

它在浏览器中显示以下结果

��z�C�ZM�]y�eb��3K������5��\mr��8�Ps|�C|�u��W〜l��t�l-��������9��%% E。 �ַ�5�]z�O�����Tl�GOGOoQ�E����yool。,児

我的API返回这样的值

[
    {
        "_id": "5e8f2e81c15e30001791379e",
        "name": "harlyRox",
        "email": "[email protected]",
        "phone_number": 713677319,
        "hotelImage": "iVBORw0KGgoAAAANSUhEUgAABVYAAAMACAYAAADPPjzCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAP+6SURBVHhe7J0FYB3Htf6P0GLJkm1JlkySGWLG2I7DDG3StEnhFd//vcJrX5k5hddX5vaVU06TtkmaNE2TNszMTHYSc8ys
},..
]
php curl base64 php-curl
1个回答
0
投票

当我删除此行时有效

$imgdecord = base64_decode($hotelImage). "\n"; 

并将此HTML标记更改为

<img src="data:image/png;base64,' . $hotelImage . '" />

如上述@LawrenceCherone

© www.soinside.com 2019 - 2024. All rights reserved.