PHP - 从String创建base64编码图像

时间:2016-01-27 09:39:34

标签: php image base64

我有数组包含字符串,我想以图像格式显示它们(每个图像包含一个字符串)。我需要以格式显示图像:<img src="data:image/png;base64,...<data>..."/>,HTML页面中的所有图像。如何从String创建base64编码图像?

我尝试从谷歌搜索,但有“从图像文件创建base64编码图像”的教程。

示例:如果我的文本是“600153957”,则输出将为:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAAAOCAIAAAD42Ti+AAADv0lEQVRIidVWT0gqaxQ/14ZBRYaMSSLEhEKshYREVAwuQkTETRLiooVEtIgWLUQiwqSFyxZuAhmmCJGQFuJCypW4CJEIEZIWFQViIGVCMflMOG/xdeeaXeXxFu9xf6vD7zu/7/yZ7xzmByLCnwnZ/53Av8eX1A8ODtRq9fPz838WfmFhoa+vb3Z29urqqpvPx8fHXz/x5QB/olKpMAyzsrKCbUgkEkql0m63p1Kpf8JzHHd0dISIzWbTbDarVCqZTLa2tvb09OTxeDQaDUVRUgie5wEgEok4HA69Xt9sNr8LEZFhGCnb+fl5Kdav1JeXlxmGqVarEiOKokajsdvt8Xicoqh4PN6bj0ajABAMBhGxXq/TNK1SqRwORzgcbjabbrc7Go0mEgkAODw8RMSZmZnJyUlEzOfzAHB6evpdiIjX19e5XC4WiwGA0+n8FUtqOU3Tfr+/vYWCIMhksnK5jIhWq9Vqtfbm3W63lHo4HFYqlbVaDb9icXERAARBQES5XL66uoqIjUYDAPb29noIw+GwVPNnLGKFQiEAkMlkJpOJVI+IDodjamqK2D6fj2XZ3jwiSqmbTKaJiYm7uzvp6O3tjdRmNptFUURElmW3t7clYSgU+q2QwGazURTVXtLnmCaTSZZlU6mU0+l0uVwPDw8AcHFxYTKZiANFUfV6ndjd+HbQNF0qlQwGw87ODgA8Pj4ajUbywARBUCgUAOD1ekul0svLy/7+PgDo9frvQoLX19dMJmOxWNRqdeeYqlQqMj3k25FHRlHU5uYmcfB6vQzDELsb3951RKzValtbWwCQSqVubm4AQC6XA4BWqyUTJYqizWaTSpXGrF1ImJOTE+lFdXa91WoNDQ0BAGmhKIoA0N/f32q1iMPl5aXRaCR2N74DarXa7/cDQKFQGB0dvb+/f39/LxQK5XKZDLRCoTg7O8vn8wMDAx6PZ3Bw8LuQMOl0GgDsdnv7/Z+pj42NkXTJBpiengaAubm5YrEIALe3t8ViUVJ2478jGAwCAMdxADAyMgIAyWQSAEibCHiebzQau7u73YQAkMvldDrd+Pj4l9ulMTUYDLFYjGVZjuMImc1maZoWBMFiscjlcrJSevDtDyabzS4tLQEA2VrVajWdThPGYrGQFY6Ix8fHABCJRKQbOoQEcrl8fX29Y3A/U2+1Wj6fj2EYjuMqlYp0zPP88PAwy7LS8u7Nu91usjRcLpdWq+V5nvCZTIZhGJ1OFwgEGo2G5B8IBDY2Ntpv6BASmM3m8/PzjtR/4B/7+/U3c4foNnBzZrIAAAAASUVORK5CYII=">

实例http://goo.gl/mYBUI。我想像这个网站一样保护我的号码。通过这种方式,扫描文本以查找此数字的垃圾邮件软件无法读取它们。

请帮帮我!抱歉我的英语不好。

2 个答案:

答案 0 :(得分:2)

这只是你如何做你想做的事情的一个粗略的例子,你需要使用你想要的任何数据生成图像,然后将文件内容作为字符串

如果按原样运行此代码,您就会明白这一点。

public class TestClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new TestClass();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static final GridBagConstraints GBC_CELL = new GridBagConstraints();
    static final GridBagConstraints GBC_ROWEND = new GridBagConstraints();
    static final GridBagConstraints GBC_FILLROW = new GridBagConstraints();
    static {
        GBC_CELL.anchor = GridBagConstraints.NORTHWEST;
        GBC_CELL.weightx = 0.5;
        GBC_CELL.fill = GridBagConstraints.BOTH;

        GBC_ROWEND.gridwidth = GridBagConstraints.REMAINDER;

        GBC_FILLROW.gridwidth = GridBagConstraints.REMAINDER;
        GBC_FILLROW.weightx = 1.0;
        GBC_FILLROW.fill = GridBagConstraints.BOTH;
    }

    public TestClass() {
        JFrame frame = new JFrame();

        JPanel pnlContent = new JPanel(new GridBagLayout());
        pnlContent.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        /*
         * Layout "ruler" panel
         */
        JPanel pnlRuler = new JPanel(new GridBagLayout());

        pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);
        pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);

        pnlRuler.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Layout "correct" panel
         */
        JPanel pnlGoodLayout = new JPanel(new GridBagLayout());

        pnlGoodLayout.add(createButton("JButton1"),GBC_CELL); // <==============
        pnlGoodLayout.add(createButton("JButton2"),GBC_CELL); // <==============

        pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        pnlGoodLayout.add(createButton("JButton3"),GBC_CELL); // <==============
        pnlGoodLayout.add(createButton("JButton4"),GBC_CELL); // <==============

        pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Layout "incorrect" panel
         */
        JPanel pnlBadLayout = new JPanel(new GridBagLayout());

        pnlBadLayout.add(createButton("JButton1"),GBC_CELL); // <==============
        pnlBadLayout.add(createButton("JButton2"),GBC_CELL); // <==============

        pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        pnlBadLayout.add(createButton("JButton number 3 is wide"),GBC_CELL); // <==============
        pnlBadLayout.add(createButton("JButton4"),GBC_CELL); // <==============

        pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);

        /*
         * Add panels to main panel
         */
        pnlContent.add(pnlRuler,GBC_FILLROW);
        pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
        pnlContent.add(pnlGoodLayout,GBC_FILLROW);
        pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
        pnlContent.add(pnlBadLayout,GBC_FILLROW);

        /*
         * Configure frame
         */
        frame.getContentPane().add(pnlContent);
        frame.setTitle("GridBagLayout Weight Bug?");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,200);
        frame.setVisible(true);
    }

    JComponent createRulerCell(Color border, Color background) {
        JPanel glue = new JPanel();
        glue.setBorder(BorderFactory.createLineBorder(border));
        glue.setBackground(background);

        return glue;
    }

    private JButton createButton(String text) {
        JButton button = new JButton(text);
        button.setPreferredSize(new Dimension(0, button.getPreferredSize().height));
        return button;
    }

}

答案 1 :(得分:0)

检查以下代码是否有助于您

Intent

和$ imgPath可以用作src for。