php圖片轉換成ASCII碼

y3c5 10年前發布 | 1K 次閱讀 PHP

php圖片轉換成ASCII碼,轉換后可以直接通過字符串顯示圖片

<html>
    <head>
        <title>Ascii</title>
        <style>
            body{
                line-height:0;
                font-size:1px;
            }
        </style>
    </head>
    <body>
         <?php
        $image = 'image.jpg';
        // Supports http if allow_url_fopen is enabled
        $image = file_get_contents($image);
        $img = imagecreatefromstring($image);

    $width = imagesx($img);
    $height = imagesy($img);

    for($h=0;$h<$height;$h++){
        for($w=0;$w<=$width;$w++){
            $rgb = imagecolorat($img, $w, $h);
            $a = ($rgb >> 24) & 0xFF;
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $a = abs(($a / 127) - 1);
            if($w == $width){
                echo '<br>';
            }else{
               echo '<span style="color:rgba('.$r.','.$g.','.$b.','.$a.');">#</span>';
            }
        }
    }
    ?>
</body>

</html></pre>

 本文由用戶 y3c5 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!