0
votes

Currently, I'm trying to pass an image in the response, however, I keep getting "Malformed UTF-8 characters, possibly incorrectly encoded" error. When I check the value of $image, I get something like this:

└äÐ`>░Ç╚I#F­GkT§5tëSNiÍ-rÜ╩Uþ4¯_B¼Ê\x05 \x00î·éj\x1C÷▓Ñ▄\e╦\v\x01┌(Í\x0Fb~"òæIµ
└¤u\x7FÕ}ºD§ÁtÅ\x08\x12²W*y|Ñój╔ý­â¿:@ì#êü©\x12‗ıTÂÿÐC·YfР\x00\x02T┴-Aê═╚êòS#▒/
\x18U@J¦L═\x1F0│Gx§n\x0E£W\x1CW¶┤ñå«YÜ\x07é\x00iär[╩\\x02Z)\x127P\x1Cy@\rc▀Åb\x
07xÚ\vÇ{Zâ9╦bö\x00ÆF‗F╔ IyR$m╩,ÑJ╝rº,ò*C+\x15╣(°êevÊÒ═£ıcÝkË{2/\x18╔\x1FZÃıR\x7F
Goú,ÈÔ×óÂ║òi¦ÆXÞfrÞ‗28þ┤N\x1EØøò¿ñ▒$£ð\fh└\x03"╬i(Ô¬I*ÒºY`ûIÒ¯5M3┼\tûÉñ┤

I've read that all I have to do to display an image, is put what is inside $image into the src of an image tag on the front end, however, I can't pass it since I'm getting the aformentioned error

public function getSight(Request $request, $placeid) {
    $get = file_get_contents("https://maps.googleapis.com/maps/api/place/details/json?placeid=" . $placeid . "&key=");
    $result = json_decode($get);

    $image = file_get_contents("https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=");

    return response()->json([
        'message' => 'Fetched sight!',
        'result' => $result,
        'image' => $image
    ], 201);
}
1
Why are you doing file_get_contents on the actual image? Are you sure you just don't want to return a url to the image in your response? - Mozammil
Because I need to make a GET request to that URL first with certain parameters in order to receive the image. - Bobimaru

1 Answers

1
votes

I've read that all I have to do to display an image, is put what is inside $image into the src of an image tag on the front end, however, I can't pass it since I'm getting the aformentioned error

This is actually wrong, you should not put the contents of $image into the src attribute. The src attribute takes a URL, meaning you should put the string https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key= there.