Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

EasterRock: your custom ascii art maker

by DutchCoder (Scribe)
on Sep 19, 2007 at 20:55 UTC ( #639998=CUFP: print w/ replies, xml ) Need Help??

This is a little module that makes a page with any piece of text or code, coloring it in using a logo or image. I started programming this because I was too lazy to make it character by character. When it was done, it wasn't that hard to make it work for any image or logo.

Feel free to test or download the EasterRock module. If anyone has any improvements or suggestions, please let me know!

EasterRock page

package EasterRock; use strict; use LWP::Simple; use GD; use HTML::Entities; # How to use this module: # # my $rock = EasterRock->new(); # $rock->setTextSource($textSource); # pass a URL # $rock->setImageSource($imageSource); # pass a URL (jpg or png) # print $rock->getRock('html'); # html, style, div or config # variables that need a default value my $_textSource = 'http://www.nu.nl'; my $_imageSource = 'http://juerd.nl/jn/onion.png'; my $_rockHeight = 60; # variables you could fill youself my $_backgroundColor = ''; my $_characterAspectratio = 1.8; # 2.0 = (line)heigth is twice the +(character)width # variables the program will fill my $_textBuffer = ''; my $_rockWidth = 60; my $_sourceImageObject; my $_patternImageObject; my $_imageAspectratio = 1; sub new { my $class = shift; my $self = {}; return bless $self, $class; } sub setTextSource { my $self = shift; $_textSource = shift; } sub setImageSource { my $self = shift; $_imageSource = shift; } sub loadText { my $self = shift; $_textBuffer = shift || get($_textSource); # read a URL using LPW $_textBuffer =~ s/[\n\t\s]//g; # get rid of spaces and stuff.. +. } sub getLine { $_textBuffer =~ m/^(.{$_rockWidth})(.*)$/; $_textBuffer = $2; return $1; } sub loadImage { my $self = shift; if ($_imageSource =~ m/^http.*/i ) { my $image_web = get($_imageSource); $_sourceImageObject = GD::Image->newFromJpegData($image_web) i +f ($_imageSource =~ m/.*jpg$/i ); $_sourceImageObject = GD::Image->newFromGifData($image_web) if + ($_imageSource =~ m/.*gif$/i ); $_sourceImageObject = GD::Image->newFromPngData($image_web) if + ($_imageSource =~ m/.*png$/i ); } else { $_sourceImageObject = GD::Image->newFromJpeg($_imageSource) if + ($_imageSource =~ m/.*jpg$/i ); $_sourceImageObject = GD::Image->newFromGif($_imageSource) if + ($_imageSource =~ m/.*gif$/i ); $_sourceImageObject = GD::Image->newFromPng($_imageSource) if + ($_imageSource =~ m/.*png$/i ); } if (!$_sourceImageObject) { die("loadImage() did not work out... ( +gif could be a problem)\n"); } my ($width,$height) = $_sourceImageObject->getBounds(); $_imageAspectratio = $height / $width; } sub getPixelColor { my $self = shift; my ($x, $y) = @_; my $index = $_patternImageObject->getPixel($x,$y); my ($r,$g,$b) = $_patternImageObject->rgb($index); return "rgb($r,$g,$b)"; } sub adjustRockWidth { # here we use the aspectratio of the characters and # the aspectratio of the image to calculate the character width $_rockWidth = int(($_rockHeight / $_imageAspectratio) * $_characte +rAspectratio); if ($_rockWidth > 4*$_rockHeight) { $_rockHeight = int($_rockHeight / 2 ); $_rockWidth = int(($_rockHeight / $_imageAspectratio) * $_char +acterAspectratio); } } sub resizeImage { # resize The image to match columns and rows $_patternImageObject = new GD::Image($_rockWidth,$_rockHeight); my ($width,$height) = $_sourceImageObject->getBounds(); $_patternImageObject->copyResized($_sourceImageObject,0,0,0,0,$_ro +ckWidth,$_rockHeight,$width,$height); } sub getRockHtml { my $self = shift; my $_style = shift; my $_code = shift; return qq|<html><head><meta http-equiv="Content-type" content="tex +t/html; charset=utf-8"><title>Easter Rock</title>$_style</head><body +id="thePage">$_code</body></html>|; } sub getRockStyle { my $self = shift; if ($_backgroundColor eq '') { $_backgroundColor = $self->getPixelColor(1,1); } my $output = qq|<style type="text/css" media="screen">body { margi +n: 0; padding: 0; background-color: $_backgroundColor; } #theText { m +argin: 0; padding: 0; overflow: hidden; font-family: monospace; font- +size: 10px; line-height: 10px; color: black; } </style>|; return $output; } sub getRock { my $self = shift; my $format = shift || 'html'; # html, style, div or config $self->loadText; $self->loadImage; $self->adjustRockWidth; $self->resizeImage; my $debuglog = ''; $debuglog .= qq|We are going to build for $_rockWidth x $_rockHeig +ht characters. \n|; $debuglog .= qq|The aspectratio of a character is $_characterAspec +tratio. \n|; $debuglog .= qq|The aspectratio of the source image is $_imageAspe +ctratio. \n|; my ($width,$height) = $_patternImageObject->getBounds(); $debuglog .= qq|The pattern image is $width x $height pixels. \n|; + if ($format eq 'config') { return $debuglog; } my $_style = $self->getRockStyle; if ($format eq 'style') { return $_style; } my $_code = ''; for (my $i=0; $i<$_rockHeight; $i++) { my @letters = split(//, $self->getLine); for (my $j=0; $j<$_rockWidth; $j++) { my $color = $self->getPixelColor($j,$i); my $letter = ($_backgroundColor eq $color) ? '&nbsp;' : en +code_entities($letters[$j]); while (($j+1)<$_rockWidth && $self->getPixelColor($j,$i) e +q $color) { $j++; $letter .= ($_backgroundColor eq $color) ? '&nbsp;' : +encode_entities($letters[$j]); } $_code .= qq|<span style="color: $color">$letter</span>|; } $_code .= qq|<br />|; } $_code = qq|<div id="theText">$_code</div>|; if ($format eq 'div') { return $_code; } return $self->getRockHtml($_style, $_code); } 1;

Comment on EasterRock: your custom ascii art maker
Download Code
Re: EasterRock: your custom ascii art maker
by pmonk4ever (Friar) on Sep 19, 2007 at 21:26 UTC
    Check your spelling, "suggestions" without the "d"... :D

    Other than that little nit, this is a pretty cool piece of code!!! Can't wait to try it out!

    Cheers! 8)

    ki6jux

    No trees were harmed in the creation of this post, however, a large number of electrons were severely inconvenienced...

      I have fixed that, thank you for your quick feedback!
Re: EasterRock: your custom ascii art maker
by Discipulus (Curate) on Sep 20, 2007 at 07:50 UTC
    for me u are a genius!

    ++ for u!!
    PS: on your site the link to download the tar is broken

    Lor*
      Thank you for your support. The link has been fixed.
Re: EasterRock: your custom ascii art maker
by goibhniu (Hermit) on Sep 20, 2007 at 14:14 UTC
Re: EasterRock: your custom ascii art maker
by b10m (Vicar) on Sep 20, 2007 at 14:45 UTC

    Impressing and fun! Goed zo :-)

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: EasterRock: your custom ascii art maker
by chanio (Priest) on Oct 15, 2007 at 03:59 UTC
    Very clear and simple!

    Nice job!

    You should add this to round it up...What if the text file is shorter than what you need to fill the image with? (just fill the rest with '=' or whatever you prefer)======

    ===============================================================

    sub getLine { $_textBuffer =~ m/^(.{$_rockWidth})(.*)$/; $_textBuffer = $2; my $rtn = $1; $_textBuffer.= '=' x ($_rockWidth - length($_textBuffer)) if (leng +th($_textBuffer) < $_rockWidth); ## JUST IN CASE... return $rtn; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://639998]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-30 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls