PHP Graphics Drawing with Patterned Lines - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Graphics Drawing with Patterned Lines - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, June 23, 2019

PHP Graphics Drawing with Patterned Lines

PHP Graphics


Drawing with Patterned Lines


Problem

You want to draw shapes using line styles other than the default, a solid line.

Solution

To draw shapes with a patterned line, use ImageSetStyle() and pass in IMG_COLOR_STYLED as the image color:

       // make a two-pixel thick black and white dashed line
       $black = 0x000000;
       $white = 0xFFFFFF;

       $style = array($black, $black, $white, $white);
       ImageSetStyle($image, $style);

       ImageLine($image, 0, 0, 50, 50, IMG_COLOR_STYLED);
       ImageFilledRectangle($image, 50, 50, 100, 100, IMG_COLOR_STYLED);

Discussion

The line pattern is defined by an array of colors. Each element in the array is another pixel in the brush. It’s often useful to repeat the same color in successive elements because this increases the size of the stripes in the pattern.

For instance, here is code for a square drawn with alternating white and black pixels, as shown on the left side of figure:

       // make a two-pixel thick black and white dashed line
       $style = array($white, $black);
       ImageSetStyle($image, $style);
       ImageFilledRectangle($image, 0, 0, 49, 49, IMG_COLOR_STYLED);

This is the same square, but drawn with a style of five white pixels followed by five black ones, as shown on the right side of figure:

       // make a five-pixel thick black and white dashed line
       $style = array($white, $white, $white, $white, $white,
                                  $black, $black, $black, $black, $black);
       ImageSetStyle($image, $style);
       ImageFilledRectangle($image, 0, 0, 49, 49, IMG_COLOR_STYLED);


Figure Two squares with alternating white and black pixels


The patterns look completely different, even though both styles are just white and black pixels.


No comments:

Post a Comment

Post Top Ad