如何在 Laravel 中从公共文件夹检索图像并转换为 base64?

4
我想从我的公共文件夹中获取图像文件,我把所有的图片存储在里面,然后将其转换为base64。我将使用pdfmake在dataTables中查看base64图像,因为我的dataTable单元格具有名为avatar的图像,根据我的搜索,需要将图像转换为base64才能在PDF中查看。
现在我使用file_get_contents来检索我的图像,但我的页面加载时间太长,我想大约需要5分钟,之后会抛出错误执行时间超过60秒的最大执行时间
{{ file_get_contents( asset('files/user_avatar.png') ) }}
3个回答

2

在将图像编码为base64时,SVG和(JPG-PNG-JPEG)之间存在差异。当您使用png图像时,可以基本上使用png扩展名。但是,您不能基本上使用svg。在使用SVG时,需要使用svg + xml。

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 );
    }
}

现在,所以
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

文件路径示例:pics/my_book.png

如果我添加 file_get_contents(),加载仍然太长。 - Jin
将 $get_img = fread(fopen($filepath, 'rb'), filesize($filepath)); 替换为新的代码并重试。 - sha'an
1
还是一样吗?尝试将我编写的函数作为项目之外的单独测试运行。如果仍然相同,请注意文件大小。或者可能是配置设置的问题。 - sha'an
我在file_get_contents无法打开流http请求失败上遇到了错误。Amaan的答案在我的实际服务器上运行良好,但在我的本地加载时间仍然太长。 - Jin
也许这可以帮助 https://dev59.com/S3RB5IYBdhLWcg3wLUu3 当与fread()一起使用时... 在我的服务器上它是工作的。 - sha'an

1

我对 Laravel 不太熟悉,但我有一份经过测试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">';
?>

编辑:

根据Jin所说, 上面的代码片段不起作用只是因为file_get_contents函数。

解决方案:

使用curl_get_contents()代替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;
}

file_get_contents替换为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">';

仍然加载时间太长? 尝试检查您的文件大小或尝试检查服务器配置

希望对您有所帮助


你好,加载时间仍然太长。可能是因为file_get_contents()造成的吗? - Jin
别担心,我有解决方案。 - Amaan warsi
再次问候,我的本地环境仍然需要太多的时间,但在我的服务器上却没有问题。我应该在哪里查看本地服务器配置? - Jin
PHP 代码并不是导致加载缓慢的原因,这是因为你正在使用 pdfmakedatatables。尝试使用 jQuery - Amaan warsi
如果有人询问 Laravel 而不是 PHP,你不应该发布一个 PHP 的答案。 - exsnake
显示剩余3条评论

-1

我认为应该是:

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

请记住,这将使数据增加33%,并且您将在文件大小超过memory_limit的文件中遇到问题。

嗨,加载时间仍然太长了。可能是file_get_contents()引起的吗? - Jin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接