Hello, I'm trying to add text to an existing image. The code below works for generating a new GD image, putting text on it and exporting, however when I try to copy a source image it doesn't properly "copy" to the main GD image.
#!/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, @_ }

In reply to Add text to image by xachen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.