in reply to Adding text to an image for printing

Hi tel2,

As james28909 said the GD module can do what you need.

You can create a GD object from the file:

$image = GD::Image->new($filename);
and then add the text
$image->string($font,$x,$y,$string,$color);
and save as png
$png_data = $image->png; open (OUTFILE,"imagewithtext.png"); binmode OUTFILE; print OUTFILE $png_data; close OUTFILE;
Regards

Replies are listed 'Best First'.
Re^2: Adding text to an image for printing
by tel2 (Pilgrim) on Nov 30, 2014 at 02:30 UTC
    That's great, pme, thanks!

    It's good to have that (rather concise) code, which I might use for something else someday, but I'm not sure I can use it in my exact scenario, because of some requirements that I didn't think to explain before, i.e. the users might be printing say 20 course certificates for students, (which are always 2 per A4 page).  So, if I put all 20 in a 10 page PDF, the users can just download that single PDF, open it, and print it.

    Would there be a quick & easy way to achieve the same thing using PNGs which were created by GD?

      Hi tel2,

      Check out this: http://search.cpan.org/~lds/GD-2.56/lib/GD.pm

      Using GD one can create empty canvas and then draw what you want and save in many different formats when ready.

      Using GD even you can insert a small marker image to a given position into a big picture.

      $myImage = new GD::Image(100,100); ... various drawing stuff ... $srcImage = new GD::Image(50,50); ... more drawing stuff ... # copy a 25x25 pixel region from $srcImage to # the rectangle starting at (10,10) in $myImage $myImage->copy($srcImage,10,10,0,0,25,25);

      Regards

        Thanks pme, but I'm sorry, I don't see how that addresses the last point I made.

        Are you still saying I could use GD to do what I want (i.e. create a single file for users to download & print, which could have 10 pages containing 2 images per page), without putting them in a PDF?  If so, how (I'm not asking for code now, just process and file formats, i.e. what kind of container can the graphics+text be put in which can contain many pages for printing 2 certificates per page)?  Or when you say "different formats", are you including PDF in that list of formats?
        (I guess multipage TIFF is one option, but I'm not sure I want to go that way, because, for one thing, the users might struggle with it.)

        Thanks.
        tel2

      Image::Magick can write PDFs. It looks like you could convert a series of PNGs (or whatever) into a PDF. Also, you might be to skip GD and add the text to the image in Image::Magick.

      Disclaimer: I have not tried this. YMMV.

        OK, thanks Ron.