Google 放置照片 API returns 一张图片,但是,我不确定如何将它发送到我的前端

Google Places Photo API returns an image, however, I am not sure how to send it to my frontend

如果我将此 URL 粘贴到浏览器中,API 确实是 returns 图像:

https://maps.googleapis.com/maps/api/place/photo?
maxwidth=400&photoreference=examplereference&key=examplekey

请记住,我删除了照片参考,因为它太长了,还删除了密钥,因为它应该是私有的。

使用 PHP 获取图像后,我似乎无法将任何我可以使用的东西发送到我的前端。

$getImage = file_get_contents("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=examplereference&key=examplekey");
$image = json_decode($getImage);
    return response()->json([
        'message' => 'Fetched sight!',
        'image' => $image
    ], 201);

如果我尝试将 $image 发送到前端它 returns NULL,如果我尝试发送 $getImages,我会得到一个错误 "Malformed UTF-8 characters, possibly incorrectly encoded"

即使阅读了文档 - https://developers.google.com/places/web-service/photos,我也无法弄清楚我希望从该请求中收到什么。

如果你想给一个图像内容一个JSON API端点,你可以base64它的内容。

<?php

$getImage = file_get_contents("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=examplereference&key=examplekey");
$image = base64_encode($getImage);
    return response()->json([
        'message' => 'Fetched sight!',
        'image' => $image
    ], 201);

在客户端,您可以像这样创建图像:

<img src="data:image/png;base64, <image content here>" />

请注意客户端将执行一些额外的操作来创建图像(未针对大图像内容进行优化)。

另一种解决方案可能是使用您的后端下载图像并使用来自您的服务器的经典 URL 提供它。