簡體   English   中英

Google Places Photo API 返回一個圖像,但是,我不知道如何將它發送到我的前端

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

如果我將此 URL 粘貼到瀏覽器中,API 確實會返回一個圖像:

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

請記住,我已經刪除了 photoreference 因為它太長,並且因為它應該是私有的而刪除了密鑰。

使用 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發送到前端,它會返回 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 為其提供服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM