4

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') ) }}
sha'an
  • 1,032
  • 1
  • 11
  • 24
Jin
  • 153
  • 2
  • 12

3 Answers3

2

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

sha'an
  • 1,032
  • 1
  • 11
  • 24
  • still too long to load, if I add the `file_get_contents()`. – Jin Jul 16 '20 at 10:09
  • replace it to $get_img = fread(fopen($filepath, 'rb'), filesize($filepath)); and try again – sha'an Jul 16 '20 at 10:19
  • 1
    still same ? Try running the function I wrote as a separate test from the project. Then if it's still the same, pay attention to the file size. Or can be a problem with the configuration settings – sha'an Jul 16 '20 at 10:48
  • I got error on `file_get_contents failed to open stream http request failed` **Answer of Amaan** is working on my live server but still too long to load in my local. – Jin Jul 17 '20 at 01:53
  • maybe this can help https://stackoverflow.com/questions/697472/php-file-get-contents-returns-failed-to-open-stream-http-request-failed when uses with fread() ... it's working in my server – sha'an Jul 17 '20 at 10:21
1

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

Amaan warsi
  • 184
  • 4
  • 18
  • Hi, still too long to load. it might because of `file_get_contents()`? – Jin Jul 16 '20 at 10:08
  • Don't worry I have a solution – Amaan warsi Jul 16 '20 at 13:22
  • Hi again, still taking too much in my local, but it's okay in my server, where view the server-configuration in local? – Jin Jul 17 '20 at 01:44
  • Can you check it on another `Server` or try to change this `user_avatar.png` file to another, Just pickup one transparent image from this page https://www.cleanpng.com/free/avatar-.html – Amaan warsi Jul 17 '20 at 07:33
  • In my local server? what is the name of file? `base64` image is now showing on live server but still not viewing on the PDF, I am using `pdfmake` and `datatables` – Jin Jul 17 '20 at 09:20
  • You can use any server and any image from above URL – Amaan warsi Jul 17 '20 at 09:23
  • And `PHP` code is not responsible for slow load this is because you are using `pdfmake` and `datatables`try to use jquery – Amaan warsi Jul 17 '20 at 09:25
  • If someone is asking Laravel and not PHP, you shouldn't post a PHP answer. – exsnake Nov 26 '22 at 03:25
-1

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.

Radhe Shyam sharma
  • 900
  • 15
  • 21