jeudi 19 février 2015

PHP GD draw holes in polygons

I'm making a tile ( image, 256 by 256 ) server with PHP. I need to put some holes in the polygons. The tiles are overlaid onto tile viewer ( like Google maps )


I can make the tile with a transparent background, then after converting the coordinates to the relative coordinates on the tile, draw the polygon.


Here is an example:



<?php

$image = imagecreatetruecolor(256, 256);
$hole_color = imagecolorallocatealpha($image, 0, 0, 0, 127); // What I think I want...
$hole_color = imagecolorallocatealpha($image, 0, 0, 0, 0); // Black to see what's doing.

imagesavealpha($image, true);
imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));

$outer_str = "0,106,0,75,1,73,7,51,8,46,9,42,4,41,1,40,0,40,0,14,0,14,5,15,7,15,15,10,14,0,70,0,66,2,63,6,53,12,52,16,47,15,42,19,43,21,48,30,49,33,50,36,56,31,58,27,63,23,67,25,72,22,77,18,80,20,84,17,90,15,91,9,93,9,94,16,97,28,97,32,100,36,101,33,104,31,105,30,109,29,113,31,112,33,112,38,113,40,111,41,109,41,105,42,102,38,99,39,96,42,94,41,90,42,88,44,90,47,99,60,104,56,109,62,123,52,139,75,181,45,181,42,181,39,183,39,184,41,189,41,193,40,196,35,195,32,194,31,192,30,190,28,188,24,186,21,186,18,183,15,179,10,178,10,176,9,173,6,171,3,170,1,172,0,172,0,236,0,244,0,245,0,256,0,256,49,255,49,251,50,251,52,251,55,251,58,251,61,251,65,252,68,251,70,246,71,245,68,245,65,245,62,245,59,245,58,242,57,239,56,238,53,237,50,232,50,229,52,229,54,231,58,231,60,230,61,227,62,226,63,224,65,225,68,224,69,223,68,221,66,218,63,217,61,214,56,210,57,210,60,212,65,214,68,219,76,223,82,231,90,235,93,239,96,243,97,247,98,247,100,240,101,238,104,237,106,236,109,234,110,231,112,220,118,221,126,230,129,232,131,234,131,237,133,234,134,230,138,235,141,236,143,241,140,246,139,248,141,249,145,248,148,247,150,244,155,243,157,244,160,246,161,249,161,252,160,256,158,256,183,220,183,216,180,213,183,206,184,209,182,211,178,211,174,208,171,203,166,200,164,195,162,193,162,191,158,188,156,185,151,175,157,179,161,182,159,184,161,185,166,182,169,181,173,174,179,167,182,167,184,152,184,152,182,152,179,153,177,154,175,155,173,156,171,158,170,159,169,161,168,164,167,160,160,156,164,155,164,150,168,149,170,148,172,147,173,145,175,136,181,134,182,132,183,130,183,127,184,125,183,117,181,115,181,113,181,110,182,108,181,106,181,103,180,97,177,95,176,93,175,92,173,91,172,90,170,87,169,85,168,83,167,82,166,80,164,79,162,78,161,78,159,77,158,77,156,77,155,77,153,77,152,77,149,76,146,76,144,75,142,75,140,75,138,75,135,76,133,77,131,78,130,79,128,78,127,77,125,77,123,76,121,76,119,76,117,76,115,77,113,78,112,80,107,75,104,73,104,71,105,69,106,67,106,64,106,63,108,62,110,58,114,56,115,54,116,52,117,50,118,48,118,46,119,44,121,42,121,40,122,37,122,35,122,33,121,31,120,28,120,25,120,23,120,21,120,19,119,18,118,16,116,15,115,13,113,12,111,11,109,9,106,8,103,4,101,1,104,0,106,0,106";
$pts = explode(",",$outer_str);
$vertices = count($pts) / 2;
if( $vertices > 2 ){
imagefilledpolygon($image, $pts, $vertices, imagecolorclosestalpha($image, 255, 0, 0, 60));
}

$holes[] = "170,115,169,116,166,117,161,121,161,122,161,124,161,126,161,127,161,129,160,131,160,132,161,133,162,135,164,136,165,138,166,140,166,142,167,144,174,140,173,138,171,131,170,115";
$holes[] = "38,107,41,102,48,103,51,99,53,94,54,92,55,92,57,88,62,90,65,91,67,90,69,88,68,82,65,81,60,84,58,83,54,81,56,78,55,76,51,75,47,74,43,74,39,79,39,76,39,74,34,67,29,68,22,61,18,60,9,86,20,92,23,101,27,100,26,104,26,105,27,105,32,104,36,102,38,107";
for($h=0;$h!=count($holes);++$h){
$pts = explode(",",$holes[$h]);
$vertices = count($pts) / 2;
if( $vertices > 2 ){
imagefilledpolygon($image, $pts, $vertices, $hole_color);
}
}

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);


I found this on Stackoverflow - Triangle Image Crop But it doesn't seem to work when I have gone with the imagesavealpha option and set the imagefill with a transparent color.


Any way I can cut those holes instead of leaving them as black spots?


Aucun commentaire:

Enregistrer un commentaire