Jump to content
新域网络技术论坛

base64图片编码集成在页面-PHP代码实现


Jamers
 Share

Recommended Posts

有的时候我们在网页源码中发现,有一大串编码的图片,比如下面的编码,我们能不能自己这样做呢?注意图片尽量小一点,否则太长可能会有反效果,因为这种类型的图片浏览器是不会缓存的,显示一次加载一次,太大的话增加自己网站的流出量也增加访问者的流量支出,一般情况下尽量不要超过20K,当然这个本身没有限制,即使你拿20M的图片编码也是可以的,土豪或者无视访客体验的可以无视之, :D

<img width="20" height="20" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAA81BMVEX///9VPnxWPXxWPXxWPXxWPXxWPXxWPXz///9hSYT6+vuFc6BXPn37+vz8+/z9/f2LeqWMe6aOfqiTg6uXiK5bQ4BZQX9iS4VdRYFdRYJfSINuWI5vWY9xXJF0YJR3Y5Z4ZZd5ZZd6Z5h9apq0qcW1qsW1q8a6sMqpnLyrn76tocCvpMGwpMJoUoprVYxeRoJjS4abjLGilLemmbrDutDFvdLPx9nX0eDa1OLb1uPd1+Td2OXe2eXh3Ofj3+nk4Orl4evp5u7u7PLv7fPx7/T08vb08/f19Pf29Pj39vn6+fuEcZ9YP35aQn/8/P1ZQH5fR4PINAOdAAAAB3RSTlMAIWWOw/P002ipnAAAAPhJREFUeF6NldWOhEAUBRvtRsfdfd3d3e3/v2ZPmGSWZNPDqScqqaSBSy4CGJbtSi2ubRkiwXRkBo6ZdJIApeEwoWMIS1JYwuZCW7hc6ApJkgrr+T/eW1V9uKXS5I5GXAjW2VAV9KFfSfgJpk+w4yXhwoqwl5AIGwp4RPgdK3XNHD2ETYiwe6nUa18f5jYSxle4vulw7/EtoCdzvqkPv3bn7M0eYbc7xFPXzqCrRCgH0Hsm/IjgTSb04W0i7EGjz+xw+wR6oZ1MnJ9TWrtToEx+4QfcZJ5X6tnhw+nhvqebdVhZUJX/oFcKvaTotUcvUnY188ue/n38AunzPPE8yg7bAAAAAElFTkSuQmCC" alt="Brand">
function base64_encode_image ($filename) {
    $type = array('jpg','png','gif','jpeg');
    if (file_exists($filename)) {
        $a = pathinfo($filename);
        $ft = strtolower($a['extension']);
        if (in_array($ft,$type)) {
            $imgbinary = fread(fopen($filename, "r"), filesize($filename));
            return 'data:image/' . $ft . ';base64,' . base64_encode($imgbinary);
        }
    }
    return '';
}

echo '<img src="'.base64_encode_image ("a1.png").'"/>';
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...