Jump to content

PHP (GD) - Applying image changes in order not working, imagefill won't fill


cerulean

Recommended Posts

I have a black and white image dividing a city into zip codes. So a bunch of shapes and that's it.

I load it (imagecreatefrompng) and I'm filling the zipcodes with color based on coordinates of a spot in the zipcode and a color relevant to information about it.

 

If I use imagefill (or imagefilltoborder) to fill a zip code it works great.

 

But - I also need to apply text and lines to that image on top of those filled shapes.

My problem is it's always doing the fill last no matter what order I put it in the code - so anywhere a line crosses a zipcode it only gets half filled in.

For example I attached an image - if i turn off the lines the white box is filled in with a shade of red. Not a great example since I think the line is right on top of the coordinate of the fill but it does it all over the image.


What am I missing? Is there not a way to choose the order your draw to the image object? Is there a way to apply a change before doign the next one short of writing and reloading the file?

 

post-169006-0-24107300-1400897241_thumb.png

These two samples draw a square within an outer square. A fill is applied from just inside the top left corner of each square.

gd1 - the fills are applied then the lines are added

gd2 - the lines are added before the fills are applied.

 

Not surprisingly, events occur in the order they are coded

 

gd1.php

<?php
$im = imagecreate(200,200);
$bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$bk = imagecolorallocate($im, 0, 0, 0);
$rd = imagecolorallocate($im, 0xCC, 0, 0);
$bl = imagecolorallocate($im, 0, 0, 0xCC);

imagerectangle($im, 0, 0, 199, 199, $bk);
imagesetthickness($im, 2);
imagerectangle($im, 50, 50, 150, 150, $bk);

// add fills
imagefill($im, 2, 2, $rd);
imagefill($im, 52, 52, $bl);

// add lines
imageline($im, 100, 0, 100, 199, $bk);
imageline($im, 0, 100, 199, 100, $bk);

// output
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

gd2.php

<?php
$im = imagecreate(200,200);
$bg = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$bk = imagecolorallocate($im, 0, 0, 0);
$rd = imagecolorallocate($im, 0xCC, 0, 0);
$bl = imagecolorallocate($im, 0, 0, 0xCC);

imagerectangle($im, 0, 0, 199, 199, $bk);
imagesetthickness($im, 2);
imagerectangle($im, 50, 50, 150, 150, $bk);

// add lines
imageline($im, 100, 0, 100, 199, $bk);
imageline($im, 0, 100, 199, 100, $bk);

// add fills
imagefill($im, 2, 2, $rd);
imagefill($im, 52, 52, $bl);

// output
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);

post-3105-0-85988400-1400913439_thumb.png

post-3105-0-73726400-1400913455_thumb.png

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.