Friday, January 6, 2012

Convert image to code image by php

[php]
<?php
function image2ascii( $image ){
$ret = '';
$img = ImageCreateFromJpeg($image);
$width = imagesx($img);
$height = imagesy($img);

for($h=0; $h<$height; $h++) // loop for height of image code
{
for($w=0; $w<=$width; $w++) // loop for width of image code
{
$rgb = ImageColorAt($img, $w, $h); // add color in code
$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);

if($w == $width)
{
$ret .= '<br>';
}
else
{
$ret .= '<span style="color:'.$hex.';">*</span>';
}
}
}
return $ret;
}


$image = '123.jpg'; // an image to convert in code
echo image2ascii( $image ); // do the conversion by calling function
?>
[/php]
Click here for Demo

1 comment:

  1. I just want to tell you that I am just beginner to blogging and absolutely loved you're page. Probably I’m planning to bookmark your website . You actually come with impressive article content. Bless you for revealing your web-site.

    ReplyDelete

Note: Only a member of this blog may post a comment.