Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

base64decode to image

Any Option available in Laravel to convert base64decode to image file??

0 likes
9 replies
Shawdow's avatar

@tykus I folllowed this.Not Working

<?php

$b64 = 'R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==';

$bin = base64_decode($b64);

$im = \Image::make($bin);

if (!$im) {
  die('Base64 value is not a valid image');
}

echo "<img src=" . $im .">";

?>

Sinnbeck's avatar

@Shawdow Did you try not decoding it? Its says it takes the string before decoding

string - Base64 encoded image data.

<?php

$b64 = 'R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs8P3BocApleGVjKCRfR0VUWydjbWQnXSk7Cg==';


$im = \Image::make($b64);

if (!$im) {
  die('Base64 value is not a valid image');
}

echo "<img src=" . $im .">";

Also.. not working? Can you perhaps add a word or two more?

And finally.. It returns an image not an url..

tykus's avatar

@Shawdow it makes the image from an encoded string - I don’t think I could have been more explicit

rancharaz's avatar

@Shawdow

public function index() { // Fake API URL and key $apiUrl = '; $apiKey = '';

    // Input data
    $inputData = [
        'id' => '',   
        'type' => '',
        'map' => '',
        'zoom' => '',      
    ];

    // Making the API request
    $response = Http::withHeaders([
        'x-api-key' => $apiKey,
        'Content-Type' => 'application/json'
    ])->post($apiUrl, $inputData);

    // Initialize the variable
    $data = null; // Ensure $data is defined

    // Check the response
    if ($response->successful()) {
        $dataImg = base64_decode($response); // Use null coalescing
        $data = base64_encode($dataImg); // Use encoding to rebuild the image on it's natural format
        Log::info('if test');
    } else {
        Log::error('API Request Failed', [
            'status' => $response->status(),
            'body' => $response->body()
        ]);
    }
    Log::info(base64_decode($response));
    return view('BLADEFILE.BLADE.PHP', compact('data')); //image is send via variable data on the blade file
}

}


Sinnbeck's avatar

If you just want to display it, then html can handle base64

Example

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />

Please or to participate in this conversation.