xachen has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use Getopt::Std; use GD; use GD::Simple; use GD::Text; use GD::Text::Wrap; use GD::Text::Align; use Image::Size; use Data::Dumper; my %opt; my $img_width = 300; my $img_height = 300; my $start_height = 1; getopt('tfscoipj', \%opt); if ($opt{'p'} =~ /^(top|bottom)$/ && $opt{'j'} =~ /^(left|center|right +)$/ && -f $opt{'i'} && $opt{'i'} =~ /\.(jpg|JPG|jpeg|JPEG|gif|GIF|png +|PNG)$/) { my ($embed_width, $embed_height) = imgsize($opt{'i'}); if ($embed_width > 300) { $img_width = $embed_width; } $img_height = $embed_height + 300; $start_height += (4 + $embed_height); } else { $opt{'j'} = "left"; } #GD::Image->trueColor(1); my $gd = GD::Image->new($img_width, $img_height); my %colors = ( 'white' => $gd->colorAllocate(255,255,255), 'black' => $gd->colorAllocate(0,0,0), 'red' => $gd->colorAllocate(255,0,0), 'blue' => $gd->colorAllocate(0,0,255), 'grey' => $gd->colorAllocate(127,127,127), ); if ($opt{'t'} !~ /^[a-zA-Z0-9\.\-\_\s\!\#\:]{1,20}$/) { print "ERROR: Must provide text string be between 1 and 20 charact +ers long.\n"; exit -1; } elsif ($opt{'f'} !~ /^[a-zA-Z0-9]{1,20}$/) { print "ERROR: Must provide font name\n"; exit -1; } elsif ($opt{'s'} !~ /^[\d]{1,2}$/) { print "ERROR: Must provide valid font size\n"; exit -1; } elsif ($opt{'s'} < 12 || $opt{'s'} > 30) { print "ERROR: Font size must be 12-30 points\n"; exit -1; } elsif (!defined($colors{$opt{'c'}})){ print "ERROR: Must provide a valid color\n"; exit -1; } elsif (!-f "/usr/ttfonts/".$opt{'f'}.".ttf") { print "ERROR: $opt{'f'} font not available!\n"; exit -1; } elsif (length($opt{'o'}) < 1) { print "ERROR: must provide file name to save image to\n"; exit -1; } #elsif (-f $opt{'o'}) { # print "ERROR: file $opt{'o'} already exists\n"; # exit -1; #} print "$opt{'o'}: $opt{'c'} $opt{'f'} $opt{'s'}: $opt{'t'}\n"; $gd->transparent($colors{'white'}); $gd->setAntiAliased($colors{$opt{'c'}}); my $wrapbox = GD::Text::Wrap->new($gd, line_space => 3, color => $colors{$opt{'c'}}, text => $opt{'t'}, ); $wrapbox->set_font("/usr/ttfonts/".$opt{'f'}.".ttf", $opt{'s'}); $wrapbox->set(align => $opt{'j'}); $wrapbox->draw(1, $start_height); #$gd->rectangle($wrapbox->get_bounds(1, 300), $colors{'white'}); if ($start_height > 1 && $embed_height > 1 && $embed_width > 1) { my $source_img; if ($opt{'i'} =~ /\.(jpg|jpeg|JPEG|JPG)$/) { $source_img = GD::Image->newFromJpeg($opt{'i'}); } print "$img_width $img_height\n"; $gd->copy($source_img, 1, 1, 0, 0, $img_width, $img_height); } open(FILE, ">".$opt{'o'}); binmode STDOUT; print FILE $gd->jpeg; close(FILE); sub rgb2n { unpack 'N', pack 'CCCC', 0, @_ }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Add text to image
by BrowserUk (Patriarch) on Jan 06, 2010 at 08:17 UTC | |
|
Re: Add text to image
by CountZero (Bishop) on Jan 06, 2010 at 07:08 UTC | |
|
Re: Add text to image
by stefbv (Priest) on Jan 06, 2010 at 08:23 UTC | |
by gmargo (Hermit) on Jan 06, 2010 at 17:28 UTC | |
|
Re: Add text to image
by gmargo (Hermit) on Jan 06, 2010 at 14:12 UTC | |
|
Re: Add text to image
by zentara (Cardinal) on Jan 06, 2010 at 16:02 UTC | |
|
Re: Add text to image
by gmargo (Hermit) on Jan 06, 2010 at 17:11 UTC |