#!/usr/bin/perl -w # # Image in HTML # by f roque # #Reads in an image and an html file, outputs an html file which will (should) #render the same as the original, but the source will now resemble the image. # #Currently only works with png's. The image should be 2-bit, only black and #white. Additionally, parsing html is kinda tough and still needs tweaking. # #for more information: http://www.blackant.net/code/oth/img-html-src.html use GD; use strict; $|=1; unless ($#ARGV == 1) { print "usage: $0 htmlfile imagefile\n\n"; exit 0; } my $file = shift; my $imgfile = shift; my $lines; my @img; my $img = GD::Image->newFromPng("$imgfile"); my ($mx, $my) = $img->getBounds; open (I, $file) or die "Error opening $file: $!\n"; while () { s/\n//s; $lines .= $_; } close(I); my @chars = split(//, $lines); my $s = 0; my $d = 0; for my $y (0..$my-1) { for my $x (0..$mx-1) {$d++ if !$img->getPixel($x, $y)}} my $side = $d == 0 ? 1 : int( sqrt( $#chars/$d ) ); RENDER: for my $y (0..$my-1) { for my $n ($side*$y..$side*$y+$side-1) { for my $x (0..$mx-1) { for my $m ($side*$x..$side*$x+$side-1) { last RENDER if $s > $#chars; if (!$img->getPixel($x, $y)) { print $chars[$s++]; } else { if ($chars[$s] =~ m/[<>=\s]/) { print ' '; } else { print $chars[$s++]; } } } } while($chars[$s] !~ m/[<>=\s]/ && $s <= $#chars) {print $chars[$s++];} print "\n"; } } #place the remaining characters if ($s <= $#chars) { for (0..10) { print "\n"; } for my $n ($s..$#chars) { print $chars[$n]; } }