Archive

Posts Tagged ‘ascii’

PHP Script To Turn Image Into ASCII Text

October 6th, 2008 No comments

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a <span> element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &nbsp; character instead.

$img = ImageCreateFromJpeg('logo.jpg');
 
// get height and with of the image.
$width = imagesx($img);
$height = imagesy($img);
 
echo '<pre style="font-size:1px;">';
for($h=0;$h<$height;$h++){
 for($w=0;$w<=$width;$w++){
  if($w == $width){
   echo '<br>';
   continue;
  }
  // get image at pixel location
  $rgb = ImageColorAt($img, $w, $h);
  // convert colour into usable format
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8 ) & 0xFF;
  $b = $rgb & 0xFF;
  // check for white/off-white colour
  if($r > 200 && $g > 200 && $b > 200){
   echo '&nbsp';
  }else{
   echo '<span  style="color:rgb('.$r.','.$g.','.$b.');">#</span>';
  }
 }
}
echo '</pre>';

As an example, take the following, quite recognisable, image.

This is transformed into the text on the following page.

The Google logo in ASCII format

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

Categories: PHP Tags: , , , , ,

Mask Email With ASCII Character Codes In PHP

April 28th, 2008 No comments

Hiding your email address in an image is the best way of encrypting your email, but if your server doesn’t support the GD2 library, or if you don’t want to use it, then you might want to look at a different way of doing this.

The easiest way to encrypt your email address is to turn every character into the ASCII code equivalent and use this to display the text in HTML by putting a &# in front of each character. Here is a function that takes a string and turns it into HTML encoded text.

function maskEmail($email){
 $hiddenEmail = '';
 $length = strlen($email);
 
 for($i = 0;$i<$length;$i++){
  $hiddenEmail .= "&#".ord($email[$i]).";";
 }
 
 return $hiddenEmail;
}

To use this just feed an email address into it.

$hiddenEmail = maskEmail("example@example.com");
echo $hiddenEmail;

This produces the following output in the source code (the browser will display this string as the email address you put in.

&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#64;&#101;&#120;&#97;&#109;&#112;&#108;&#101;&#46;&#99;&#111;&#109;

This can be easily added to a anchor tag to make an email link.

echo '<a href="mailto:'.$hiddenEmail.'" title="Email me">'.$hiddenEmail.'</a>';

Categories: PHP Strings Tags: , , , ,

Convert HTML To ASCII With PHP

April 26th, 2008 2 comments

The reverse of turning ASCII text into HTML is to convert HTML into ASCII. And to this end here is a little function that does this.

function html2ascii($s){
 // convert links
 $s = preg_replace('/<a\s+.*?href="?([^\" >]*)"?[^>]*>(.*?)<\/a>/i','$2 ($1)',$s);
 
 // convert p, br and hr tags
 $s = preg_replace('@<(b|h)r[^>]*>@i',"\n",$s);
 $s = preg_replace('@<p[^>]*>@i',"\n\n",$s);
 $s = preg_replace('@<div[^>]*>(.*)</div>@i',"\n".'$1'."\n",$s);
 
 // convert bold and italic tags
 $s = preg_replace('@<b[^>]*>(.*?)</b>@i','*$1*',$s);
 $s = preg_replace('@<strong[^>]*>(.*?)</strong>@i','*$1*',$s);
 $s = preg_replace('@<i[^>]*>(.*?)</i>@i','_$1_',$s);
 $s = preg_replace('@<em[^>]*>(.*?)</em>@i','_$1_',$s);
 
 // decode any entities
 $s = strtr($s,array_flip(get_html_translation_table(HTML_ENTITIES)));
 
 // decode numbered entities
 $s = preg_replace('/&#(\d+);/e','chr(str_replace(";","",str_replace("&#","","$0")))',$s);
 
 // strip any remaining HTML tags
 $s = strip_tags($s);
 
 // return the string
 return $s;
}

To use this function just pass it a string. Here is an example of it at work.

$htmlString = '<p>This is some <strong>XHTML</strong> markup that <em>will</em> be<br />turned <a href="http://www.talkincode.com/" title="Talk in code">into</a> an ascii string</p>';
 
echo html2ascii($htmlString);

Produces the following output.

This is some *XHTML* markup that _will_ be
turned into (http://www.talkincode.com/) an ascii string

Categories: PHP Strings Tags: , , , ,

Turning ASCII Text Into HTML With PHP

April 25th, 2008 No comments

Providing a text box for users to type information in is very common, but usually people want to include line breaks and links with the text and they expect the site to lay it out just as they had intended it. The following function will turn any ASCII text string into the approximate HTML equivalent.

function ascii2html($s){
 $s = htmlentities($s);
 // try and split the text by a double line break
 $paragraphs = split("\n\n",$s);
 if(count($paragraphs)<2){
  // if there isn't enough array there then try and split it by single
  $paragraphs = split("\n",$s);
 };
 for($i = 0,$j = count($paragraphs);$i < $j;$i++){
  // create links around URLs
  $paragraphs[$i] = preg_replace('/((ht|f)tp:\/\/[^\s&]+)/','<a href="$1">$1</a>',$paragraphs[$i]);
  // create links around email addresses
  $paragraphs[$i] = preg_replace('/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i','<a href="mailto:$0">$0</a>',$paragraphs[$i]);
  // make paragraph
  $paragraphs[$i] = '<p>'.$paragraphs[$i].'</p>';
 };
 // join all paragraphs and return
 return join("\n",$paragraphs);
}

To test this use the following text sample.

$text = "this is some text
that splits across several
lines and
has some links like this
one here http://www.talkincode.com which
will be used to create a bunch of html";

And call the ascii2html() function like this.

echo ascii2html($text);

This will produce the following output.

<p>this is some text</p>
<p>that splits across several</p>
<p>lines and</p>
<p>has some links like this </p>
<p>one here <a href="http://www.talkincode.com">http://www.talkincode.com</a> which</p>
<p>will be used to create a bunch of html</p>

It might be prudent to use the strip_tags() function to clean the ASCII text before you use this function as it might lead to invalid HTML.

Categories: PHP Strings Tags: , , , ,