dougan6402 2015-11-07 13:50
浏览 49
已采纳

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

I'm storing small images in database (MySQL, blob), so when I retrieve any image it's in variable.

My plan is to display image with CSS as base64 encoded in background image: background: url(data:image/….

To perform that I need to detect MIME type of image which is in variable. There's exif_imagetype, but it works only with files.

Is there any way to detect MIME type of image which isn't stored as file, but in variable?

  • 写回答

1条回答 默认 最新

  • duanji1610 2015-11-07 14:37
    关注

    You can use getimagesizefromstring to get information about an image blob, including the image’s MIME type. It is available since PHP 5.4.

    However, I would also suggest to store the MIME type in the database. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?