一聚教程网:一个值得你收藏的教程网站

热门教程

php 图片转换成ascii 输出图像

时间:2022-07-02 09:26:34 编辑:袖梨 来源:一聚教程网

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 .= '
'; 
            }
            else
            { 
                $ret .= '#'; 
            } 
        } 
    } 
    return $ret;
}
?>

Example Usage

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

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

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

 

热门栏目