in reply to How to use Imager for text

Hi, I tried your script and found the problem to be in the color specification for the font. If I used your 'color=>444444' I would get an error "cl is not of type Imager::Color. This worked for me
#!/usr/bin/perl use warnings; use strict; use Imager; my $img = Imager->new(xsize=>400,ysize=>50,channels=>4); my $blue = Imager::Color->new("#0000FF"); my $font = Imager::Font->new( file => 'Generic.ttf', color => $blue, size => 30); $img->string(font => $font, text => "Model-XYZ", x => 15, y => 40, size => 40, color => $blue, aa => 1); $img->write(file=>$0.'.png', type=>'png') or die "Cannot write: ",$img->errstr; my $img_mirror = $img->copy(); $img_mirror->flip( dir => "h" ); $img_mirror->write(file=>$0.'-mirror.png', type=>'png') or die "Cannot write: ",$img_mirror->errstr;

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: How to use Imager for text
by bradcathey (Prior) on Oct 28, 2005 at 22:03 UTC

    Thanks zentara, I thought we were golden. But when I cut and paste your code and run it from my host (pair networks), the .png files are totally blank, though they contain bytes.

    Very weird.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      Oops, I made a mistake in cut'n'pasting and got the wrong code. Try this, and make sure you have a "Generic.ttf"(just rename Arial.ttf")
      #!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use Imager; my $img = Imager->new(xsize=>400,ysize=>300); $img->box(filled=>1, color=>"ffffff"); #fill the background color my $blue = Imager::Color->new("#0000FF"); my $font = Imager::Font->new( file => 'Generic.ttf', index => 0, color => $blue, size => 30, aa => 1); $img->string( font=>$font, text=>'This is a test string', x=>20, y=>70); $img->write(file=>"$0.jpg", type=>"jpeg") or die "Cannot write file: +", Imager->errstr;

      I'm not really a human, but I play one on earth. flash japh

        Perfect zentara, thanks for correcting it. BTW, got it to work with .ttf fonts, but not the .dfont

        my $font = Imager::Font->new( file => 'Copperplate.dfont', type => 'ft2', index => 1, color => $blue, size => 30, aa => 1);

        did not work. Oh, well.


        —Brad
        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot