简体   繁体   English

PHP-猜测存储在变量中的图像的MIME类型

[英]PHP - guess MIME type of image stored in variable

I'm storing small images in database (MySQL, blob ), so when I retrieve any image it's in variable. 我正在数据库中存储小型图像(MySQL, blob ),所以当我检索任何图像时,它都在变量中。

My plan is to display image with CSS as base64 encoded in background image: background: url(data:image/… . 我的计划是显示以CSS为base64的图像,并在背景图像中进行编码: background: url(data:image/…

To perform that I need to detect MIME type of image which is in variable. 为此,我需要检测变量中图像的MIME类型。 There's exif_imagetype , but it works only with files. exif_imagetype ,但仅适用于文件。

Is there any way to detect MIME type of image which isn't stored as file, but in variable? 有什么方法可以检测不是存储为文件而是存储在变量中的图像的MIME类型?

You can use getimagesizefromstring to get information about an image blob, including the image's MIME type. 您可以使用getimagesizefromstring获取有关图像斑点的信息,包括图像的MIME类型。 It is available since PHP 5.4. 自PHP 5.4起可用。

However, I would also suggest to store the MIME type in the database. 但是,我也建议将MIME类型存储在数据库中。 Heuristic approaches are “expensive” and storing a few bytes in the database is much more efficient than guessing the image type over and over again. 启发式方法是“昂贵的”,并且在数据库中存储几个字节比一遍又一遍地猜测图像类型要有效得多。

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

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