imagefilledpolygon() with a pattern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • albert

    #1

    imagefilledpolygon() with a pattern

    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?

    Thanks in advance

    Albert

  • albert

    #2
    Re: imagefilledpoly gon() 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);
    }
    You can fill the polygon with anything by changing the $crossHash
    image.

    Have a good day

    Comment

    Working...