Page 275 - Open Soource Technologies 304.indd
P. 275

Unit 11: Graphics



            The $size argument is the font size in pixels; $angle is an angle of rotation, in degrees going   Notes
            counter-clockwise;  and  /path/to/font.ttf  is  the  pathname  to  TrueType  font  file.  Unlike
            ImageString( ), ($x,$y) are the lower left coordinates of the baseline for the first character. (The
            baseline is where the bottom of most characters sits. Characters such as “g” and “j” extend below
            the baseline; “a” and “z” sit on the baseline.)
            Again, the syntax for printing text is similar but not the same:

            $font  =  ImagePSLoadFont(‘/path/to/font.pfb’);  ImagePSText($image,  $text,  $font,  $size,
            $text_color, $background_color, $x, $y); ImagePSFreeFont($font);

            First, PostScript font names cannot be directly passed into ImagePSText( ). Instead, they must
            be loaded using ImagePSLoadFont( ). On success, the function returns a font resource usable
            with ImagePSText( ). In addition, besides specifying a text colour, you also pass a background
            colour to be used in antialiasing calculations. The ($x,$y) positioning is akin to the how the
            TrueType library does it. Last, when you are done with a font, you can release it from memory
            by calling ImagePSFreeFont( ).
            Besides the mandatory arguments listed above, ImagePSText( ) also accepts four optional ones,
            in this order: space , tightness, angle, andantialias_steps. You must include all four or none of
            the four (i.e., you cannot pass one, two, or three of these arguments). The first controls the size
            of a physical space (i.e., what’s generated by hitting the space bar); the second is the tightness
            of the distance between letters; the third is a rotation angle, in degrees, counter-clockwise; and
            the last is an antialiasing value. This number must be either 4 or 16. For better looking, but more
            computationally expensive graphics, use 16 instead of 4.

            By default, space, tightness, and angle are all 0. A positive number adds more space between
            words and letters or rotates the graphic counterclockwise. A negative  number kerns words
            and letters or rotates in the opposite direction. The following example has the output shown
            in Figure 11.9:
            // normal image
            ImagePSText($image, $text, $font, $size, $black, $white, $x, $y, 0, 0, 0, 4);
            // extra space between words

            ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 30, 100, 0, 0, 4);
            // extra space between letters
            ImagePSText($image, $text, $font, $size, $black, $white, $x, $y + 60, 0, 100, 0, 4);


                             Figure 11.9: Words with Extra Space and Tightness

                                            My India

                                           My India

                                         My India


            11.6 Dynamically Generated Buttons


            You want to create an image based on a existing image template and dynamic data (typically
            text). For instance, you want to create a hit counter.





                                             LOVELY PROFESSIONAL UNIVERSITY                                   269
   270   271   272   273   274   275   276   277   278   279   280