首页 > php框架 > php 图片转换成ascii 输出图像

php 图片转换成ascii 输出图像

php 图片转换成ascii 输出图像

php 图片转换成ascii 输出图像

这个PHP函数创建了一个简单的JPG图像ASCII艺术渲染。它使用广东做繁重的工作,和一些简单的移位分配色彩。颜色,然后转移到十六进制值和生成的文本输出到浏览器。 ASCII码从影像艺术与PHP现在,很容易。如果你觉得你有什么改进,请随时与我们联系,您的想法PHPRO。


<?php

function image2ascii( $image )
{
    // return value
    $ret = ;

    // open the image
    $img = ImageCreateFromJpeg($image); 

    // get width and height
    $width = imagesx($img); 
    $height = imagesy($img); 

    // loop for height
    for($h=0;$h<$height;$h++)
    {
        // loop for height
        for($w=0;$w<=$width;$w++)
        {
            // add color
            $rgb = ImageColorAt($img, $w, $h); 
            $r = ($rgb >> 16) & 0xFF; 
            $g = ($rgb >> 8) & 0xFF; 
            $b = $rgb & 0xFF;
            // create a hex value from the rgb
            $hex = #.str_pad(dechex($r), 2, 0, STR_PAD_LEFT).str_pad(dechex($g), 2, 0, STR_PAD_LEFT).str_pad(dechex($b), 2, 0, STR_PAD_LEFT);

            // now add to the return string and we are done
            if($w == $width)
            { 
                $ret .= <br>; 
            }
            else
            { 
                $ret .= <span style="color:.$hex.;">#</span>; 
            } 
        } 
    } 
    return $ret;
}
?>

Example Usage

<?php

// an image to convert
$image = test.jpg;

// do the conversion
$ascii = image2ascii( $image );

// and show the world
echo $ascii;
?>


本文链接:http://www.phprm.com/frame/php1004989.html

收藏随意^^请保留教程地址.

标签:none

发表留言