Thursday, June 02, 2011

php Image Magik - how to force Text Wap without cropping

After several hours of trying to find the best way to add 2 sets of text to an image using ImageMagik without overlapping. The image is split into two natural text blocks. I felt it was easier to add the two sections of text to the one image instead of combining two images.

The simplest way I could find is to figure out the font point size and approximate the dimensions by the number of characters in your string. Then use the php wordwrap function to add newline characters every 35 characters.

Note: I only needed to force word wrapping on message one because it shows on the left side of the image. ImageMagik will automatically wrap the annotation if it runs into the right edge of the image. Well it appears that way and its working so far, but I may change it later just cause :)

//force text wrapping every 35 characters
$message = wordwrap($message, 35, "\n", true);
$filepath = '/home/test.com/';
$newfile = $filepath.'uploads/files/'.$newfilename;
//image we want to write text on
$blankfile = $filepath.'images/blank_image.png';

$imageCMD = "convert  '$blankfile'    ".
                 " -fill '#000000' -pointsize 16   -annotate +40+80 '$message1'   ".  
                 " -fill '#000000' -pointsize 20   -annotate +340+150 '$message2' ".
                 " '$newfile'" ;
//calls the linux commandline to process the command    
$cr = system( $imageCMD  , $retval);



Stumble Upon CodePyro

No comments:

Post a Comment