# imagefilledpolygon() with a pattern

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Good morning,

I would like to draw a polygon and then fill it with a pattern instead
of a solid colour.

Is there any functions available which will accomplish what I am
attempting or do I have to write my own function?

Albert

## Re: imagefilledpolygon() with a pattern

By pure chance I stumbled across the imagesettile() method. This can be
used to fill a polygon (or any other shape) with a specific pattern.

So I wrote the following function to create a crosshash fill:

[code]
function imageCrossHashFill(\$image, \$polygonPoints, \$colour) {
\$r = hexdec(substr(\$colour, 0, 2));
\$g = hexdec(substr(\$colour, 2, 2));
\$b = hexdec(substr(\$colour, 4, 2));

\$crossHash = imagecreate(20, 20);
\$bg2 = imagecolorallocate(\$crossHash, 255, 255, 255);
\$red2 = imagecolorallocate(\$crossHash, \$r, \$g, \$b);
imageline(\$crossHash, 0, 0, 20, 20, \$red2);
imageline(\$crossHash, 0, 10, 10, 20, \$red2);
imageline(\$crossHash, 10, 0, 20, 10, \$red2);
imageline(\$crossHash, 20, 0, 0, 20, \$red2);
imageline(\$crossHash, 10, 0, 0, 10, \$red2);
imageline(\$crossHash, 20, 10, 10, 20, \$red2);
imagecolortransparent(\$crossHash, \$bg2);

imagesettile(\$image, \$crossHash);
imagefilledpolygon(\$image, \$polygonPoints, count(\$polygonPoints) / 2,
IMG_COLOR_TILED);

imagedestroy(\$crossHash);
}
[/code]

You can fill the polygon with anything by changing the \$crossHash
image.

Have a good day