How to retrieve image from public folder and convert to base64 in Laravel?

Viewed 2831

I want to get the image file from my public folder which I stored all of my images, then convert to base64. I will use the base64 image to view it on PDF using pdfmake in dataTables, because my dataTable cell have an Image called avatar, and as I've searched it needs to convert image to base64 to able to view in PDF.

Now I use file_get_contents to retrieve my image, but my page is too much load I guess it's around 5mins, and after throws and error Maximum execution time of 60 seconds exceeded.

{{ file_get_contents( asset('files/user_avatar.png') ) }}
3 Answers

There is difference between svg and (jpg-png-jpeg) when you encoding to base64. You can use basically png extension when you working with png image. But you cant use svg basically. You need svg+xml when you working with svg.

function img_enc_base64 ($filepath){   // img_enc_base64() is manual function you can change the name what you want.

    if (file_exists($filepath)){

        $filetype = pathinfo($filepath, PATHINFO_EXTENSION);

        if ($filetype==='svg'){
            $filetype .= '+xml';
        }

        $get_img = file_get_contents($filepath);
        return 'data:image/' . $filetype . ';base64,' . base64_encode($get_img );
    }
}

so now

echo img_enc_base64('file_path');  // is your base64 code of image

<img src="<?php echo img_enc_base64('file_path');?>" alt="">  // is your image

file_path example: pics/my_book.png

I'm not friendly with Laraval but I have a tested answer written in PHP

<?php
    $path = "files/user_avatar.png";
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    # print to make sure that it is working or not
    echo $base64."<br>";
    # Or, show it as a clean image
    echo '<img scr="'.$base64.'" height="150" width="150">';
?>

Edited:

According to Jin, Above snippet not working just because of file_get_contents function.

Solution:

Use curl_get_contents() instead of file_get_contents

curl_get_contents()

function curl_get_contents($url){
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

After replacing file_get_contents into curl_get_contents

$path = "files/user_avatar.png";
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = curl_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
# print to make sure that it is working or not
echo $base64."<br>";
# Or, show it as a clean image
echo '<img scr="'.$base64.'" height="150" width="150">';

Still taking too much time to load? Try to check your file size or try to check Server-Configuration

Hope it will helpful for you

I think that it should be:

$path = 'files/user_avatar.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

bear in mind that this will enlarge the data by 33%, and you'll have problems with files whose size exceed your memory_limit.

Related