duannaikuang1301 2016-01-27 09:39
浏览 81
已采纳

PHP - 从String创建base64编码图像[关闭]

I have array contain strings and I want to display them in image format (each image contain a string). I need display image in format: <img src="data:image/png;base64,...<data>..."/>, all images in a HTML page. How to create base64 encoded image from String?

I tried to search from google, but there are tutorials for "create base64 encoded image from image file".

Example: If my text is "600153957", the output will be:

<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=">

Live example: http://goo.gl/mYBUI . I want protect my numbers like this website. By this way, the spam software which scan text to find this numbers can't read them.

Please help me! Sorry for my bad English.

  • 写回答

2条回答 默认 最新

  • dpa89292 2016-01-27 10:43
    关注

    This is just a rough example of how you could do what you want, you need to generate the image with php using whatever data you want and then get the file contents as a string

    You'll get the idea if you run this code as it is.

    <?php
    
        function b64img( $str, $fs=10, $w=250, $h=200, $b=array( 'r'=>255, 'g'=>255, 'b'=>255 ), $t=array('r'=>0, 'g'=>0, 'b'=>0) ){
            $tmp=tempnam( sys_get_temp_dir(), 'img' );
    
            $image = imagecreate( $w, $h );
            $bck = imagecolorallocate( $image, $b['r'], $b['g'], $b['b'] );
            $txt = imagecolorallocate( $image, $t['r'], $t['g'], $t['b'] );
    
            imagestring( $image, $fs, 0, 0, $str, $txt );
            imagepng( $image, $tmp );
            imagedestroy( $image );
    
            $data=base64_encode( file_get_contents( $tmp ) );
            @unlink( $tmp );
            return $data;
        }
    
    
        $img=b64img( 'Some text to make into base64 image', 6, 400, 100 );
        echo "<img src='data:image/png;base64, {$img}' />";
    
        $img=b64img( 'Another image', 10, 200, 200, array('r'=>0, 'g'=>255, 'b'=>0), array('r'=>255, 'g'=>0, 'b'=>0) );
        echo "<img src='data:image/png;base64, {$img}' />";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里