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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog