简体   繁体   English

如何动态设置数据:图像/文件类型?

[英]How do I set data:image/ filetype dynamically?

I have a database which contains images saved as BLOB s.我有一个数据库,其中包含保存为BLOB的图像。 I can successfully use the image on a page like so:我可以像这样在页面上成功使用图像:

<img src="<?php echo 'data:image/jpeg;base64,'.base64_encode($image)?>" alt="Landing" width="150px">

However this requires setting the file extension manually in the actual statement at data:image/jpeg;但是,这需要在data:image/jpeg; . . The problem is that I have lots of various images in various formats.问题是我有很多不同格式的图像。 I want to make sure that the filetype is set properly based on the actual file extension of the specific file for each image.我想确保根据每个图像的特定文件的实际文件扩展名正确设置文件类型。 I already have a nested array which contains all the file extensions for those files.我已经有一个嵌套数组,其中包含这些文件的所有文件扩展名。

Nonetheless I am having trouble setting the extension dynamically.尽管如此,我在动态设置扩展时遇到了麻烦。 I've tried simply replacing the '' single quotes with "" to allow me to easily use a variable inside of the statement like so:我尝试简单地将''单引号替换为""以允许我轻松地在语句中使用变量,如下所示:

<img src="<?php echo "data:image/$images['monitor']['extension'];base64,".base64_encode($image)?>" alt="Landing" width="150px">

This doesn't work because the src tag itself contains double-quotes already I believe.这不起作用,因为我相信src标记本身已经包含双引号。 My IDE tells me an error Cannot use '[]' for reading .我的 IDE 告诉我一个错误Cannot use '[]' for reading I've also tried using concatinated single quotes instead:我也尝试过使用连接单引号:

<img src="<?php echo 'data:image/' . $images['monitor']['extension'] . ';base64,'.base64_encode($image)?>" alt="Monitor" width="150px">

Which also did not work.这也没有奏效。 I was unable to find any solution to this online myself.我自己无法在网上找到任何解决方案。 Is there any way to dynamically set the file extension?有没有办法动态设置文件扩展名? Although setting jpeg for each image mostly works for instance doing so for the image/x-ico tab icon renders the image unable to load properly.尽管为每个图像设置jpeg大多是有效的,例如为image/x-ico选项卡图标这样做会导致图像无法正确加载。

Assuming that the BLOB contains the actual binary data of the image.假设 BLOB 包含图像的实际二进制数据。

Just make sure that the extensions match with the required syntax只需确保扩展名与所需的语法匹配

jpg file: <img src="data:image/jpeg;base64,[base64_encoded_data] jpg 文件:<img src="data:image/jpeg;base64,[base64_encoded_data]

png file: <img src="data:image/png;base64,[base64_encoded_data] png 文件:<img src="data:image/png;base64,[base64_encoded_data]

ico file: <img src="data:image/icon;base64,[base64_encoded_data] ico 文件:<img src="data:image/icon;base64,[base64_encoded_data]

So a sample example is like the following:因此,示例示例如下:

<?php
$image=file_get_contents("http://www.createchhk.com/SO/sample1.png");

$file_ext = 'png';
?>

Test for PNG<br>
<img src="data:image/<?php echo $file_ext; ?>;base64,<?php echo base64_encode($image)?>" alt="Landing" width="50px"><br>


<?php
$image2=file_get_contents("http://www.createchhk.com/SO/sample1.jpg");
$file_ext2 = 'jpeg';
?>


Test for JPG<br>
<img src="data:image/<?php echo $file_ext2; ?>;base64,<?php echo base64_encode($image2)?>" alt="Landing2" width="50px"><br>

<?php
$image3=file_get_contents("http://www.createchhk.com/SO/sample1.ico");
$file_ext3 = 'icon';
?>


Test for JPG<br>
<img src="data:image/<?php echo $file_ext3; ?>;base64,<?php echo base64_encode($image3)?>" alt="Landing3" width="50px"><br>

The result can be seen here:结果可以在这里看到:

http://www.createchhk.com/SO/testSO_18Nov2021.php http://www.createchhk.com/SO/testSO_18Nov2021.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM