内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Determine image type from base 64 content - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Determine image type from base 64 content - Stack Overflow

programmeradmin0浏览0评论

I have different types of image content in base64 format in javascript, like:

a png file: "iVBORw0KGgoAAAANSUhEUgAABQAAAAL4CAYAAAAkgloNAAAgA......."

a jpeg file: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA......"

similarly, there can be other images like GIF, BMP, etc.

I need a way to determine the image type based on the content. I understand there are headers for each file type and I can refer it to determine the image content type. Example Reference: .

But does anyone know of a library or other technique that will do the trick in a more smarter way? I can use any of the HTML5 features if required.

The main intent is to render the image on the screen. For that I use dataURI pattern, . I have image content, all I need is to put the right value in the "data:image/png" part of the url.

I have different types of image content in base64 format in javascript, like:

a png file: "iVBORw0KGgoAAAANSUhEUgAABQAAAAL4CAYAAAAkgloNAAAgA......."

a jpeg file: "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA......"

similarly, there can be other images like GIF, BMP, etc.

I need a way to determine the image type based on the content. I understand there are headers for each file type and I can refer it to determine the image content type. Example Reference: http://en.wikipedia/wiki/Portable_Network_Graphics#Technical_details.

But does anyone know of a library or other technique that will do the trick in a more smarter way? I can use any of the HTML5 features if required.

The main intent is to render the image on the screen. For that I use dataURI pattern, . I have image content, all I need is to put the right value in the "data:image/png" part of the url.

Share Improve this question edited Apr 16, 2021 at 18:54 Machavity 31.7k27 gold badges95 silver badges105 bronze badges asked Apr 24, 2012 at 17:10 sbrsbr 4,8435 gold badges45 silver badges51 bronze badges 3
  • 2 Like, a base 64 decoder ostermiller/calc/encode.html and a tool to give you the type from binary data htmlgoodies./html5/tutorials/… ? – Jamie Treworgy Commented Apr 24, 2012 at 17:13
  • 2 Modern browsers will figure out what type an image is on their own, they don't rely on file extensions or mime types. Try adding data:image/png to a jpeg or data:image/gif to a png, it should still render fine. As for checking the header for the filetype, that's the accepted way of doing it for any file in scenarios where the mime type or file name is not available (and sometimes even when it is available), nothing wrong with it at all. – Dagg Nabbit Commented Apr 24, 2012 at 20:45
  • Just checked FF,IE,Chrome with data:image/png for 3 types of image files- jpg,gif and bmp; and they render fine with mime type="image/png", used latest versions of all these browsers. – sbr Commented Apr 25, 2012 at 19:54
Add a ment  | 

1 Answer 1

Reset to default 3

There's lots of javascript base64 decoders, (related question), just decode the first 4 or 5 letters and they should contain the filetype.

发布评论

评论列表(0)

  1. 暂无评论