Someone on comp.lang.perl.tk wanted to know how to make a text, with a "mirror" of itself. Well I found a way with Imager, and it used so many commonly needed Imager methods, that it is actually a good general purpose demo of Imager capabilities. I post it for that purpose....a small script to illustrate alot of Imager's syntax. The Generic.tff font, can be any font you want, it is the path to the font file.

This code will take a quoted line of text as first commandline argument. It will then make an image of the text, crop it to size, mirror it, and combine it all into 1 file.

#!/usr/bin/perl use warnings; use strict; use Imager; my $text = shift || 'Perlmonks Rule'; #make the xsize huge to accept alot of text #it will be cropped to text size later my $img = Imager->new(xsize=>900,ysize=>50,channels=>4); my $blue = Imager::Color->new("#0000FF"); my $font = Imager::Font->new( file => 'Generic.ttf', #path to any font file color => $blue, size => 40); $img->string(font => $font, text => $text, x => 0, y => 40, size => 40, color => $blue, aa => 1); my ($neg_width, $global_descent, $pos_width, $global_ascent, $descent, $ascent) = $font->bounding_box(string => "$text"); print "$neg_width, $global_descent, $pos_width, $global_ascent,$descen +t, $ascent\n"; my $img1 = $img->crop(left=>$neg_width, right=>$pos_width, top=> -$global_descent, bottom=>$global_ascent + + 5); #fudge factor $img1->write(file=>$0.'.png', type=>'png') or die "Cannot write: ",$img1->errstr; my $img_mirror = $img1->copy(); $img_mirror->flip( dir => "h" ); $img_mirror->write(file=>$0.'-mirror.png', type=>'png') or die "Cannot write: ",$img_mirror->errstr; #make a unified image my $img_uni = Imager->new(xsize=>$pos_width * 2,ysize=>$global_ascent +,channels=>4); $img_uni->paste(left=>$pos_width,top=>0,img=>$img1); $img_uni->paste(left=>0,top=>0,img=>$img_mirror); $img_uni->write(file=>$0.'-uni.png', type=>'png') or die "Cannot write: ",$img_uni->errstr;

In reply to Mirrored text with Imager by zentara

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.