# # $Id: Unicolor.pm,v 0.1 2007/03/10 17:20:14 dankogai Exp dankogai $ # package Unicolor; use strict; use warnings; our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g; use GD; use List::Util qw/min/; use base qw/Exporter/; our @EXPORT = qw/txt2gd gd2txt/; our $MAX_WIDTH = 1024; sub txt2gd{ my ($str, $width) = @_; my $len = length($str); my @pix = unpack "U*", $str; my $w ||= min(sqrt($len), $MAX_WIDTH); my $h = int($len / $w); $h += 1 if $len % $w; my $img = GD::Image->new($w, $h, 1); Loop: for my $y ( 0 .. $h - 1){ for my $x ( 0 .. $w - 1){ my $pix = shift @pix || 0; $img->setPixel($x, $y, $pix); } } return $img; } sub gd2txt{ my $img = shift; my %color; my ($w, $h) = $img->getBounds(); my $counter = 0; my @pix; for my $y (0 .. $h-1){ for my $x (0 .. $w-1){ my $color = $img->getPixel($x, $y); push @pix, $color; } } pack "U*", grep {$_} @pix; } __END__