Jump to content

GD and imagefill problems


mfflynn

Recommended Posts

I am working on a project with GD and I need to fill and an image with a color. Sounds easy, but the imagefill doesn't seem to be working. I will create an image, and allocate the color and then I will use imagefill, which returns true, but doesn't actually fill anything with color. Here's the weird part: It will work if the image is smaller than what I want. If I replace the variables that state the size of the image I create with any numbers lower than 250, then the image fills with color fine, any bigger and it stays black.

Any suggestions to what the problem could possibly be?

Here is some sample code I ripped out of my script that behaves the same way:
[code]
<?php
$print = imagecreatetruecolor(100, 100);
$image = imagecreatetruecolor(249, 249); #<-- Make these numbers any bigger and the imagefill won't work.
$mat1 = imagecolorallocate($image, 255, 255, 0);
if(!imagefill($image, 0, 0, $mat1)){
    print("Image not filling!"); exit;
}
imagecopy($image,$print,50,50,0,0,100,100);

header("Content-type: image/jpg");
imagejpeg($image);
imagedestroy($image);  // Free memory
?>
[/code]

UPDATE: Actually the size restrictions aren't so cut and dry. They waiver a bit too. Now even 240x240 pictures aren't working with the exact same code as above.
Link to comment
Share on other sites

You might not be getting responses because this error doesn't seem to duplicate well, the code I used:

<?php
$print = imagecreatetruecolor(300, 500);
$image = imagecreatetruecolor(555, 555);
$mat1 = imagecolorallocate($image, 255, 255, 0);

if ( !imagefill($image, 0, 0, $mat1) ) {
die("Image not filling!");
}

imagecopy($image, $print, 50, 50, 0, 0, 300, 500);

header("Content-type: image/jpg");
imagejpeg($image);
imagedestroy($image);
?>


works like a charm, a 555x555 yellow box with a 300x500 black box offset 50x50 inside of it.
Link to comment
Share on other sites

Awesome. But its not working for me.

What could be causing that weirdness in my php setup?

I'm running PHP version 5.1.2 with the bundled GD compiled into it.

UPDATE: I just copied and pasted your code and it just shows me a 555x555 black square.
Link to comment
Share on other sites

Yes, I know it isn't very encouraging being told the script works fine :x Try this one:

[code]<?php
$image_y = imagecreate(500, 500);
$colourYellow = imagecolorallocate($image_y, 255, 255, 0);
imagefill($image_y, 0, 0, $colourYellow);

$image_b = imagecreate(50, 50);
$colourBlack = imagecolorallocate($image_b, 0, 0, 0);
imagefill($image_b, 0, 0, $colorBlack);

imagecopy($image_y, $image_b, 50, 50, 0, 0, 50, 50);

header("Content-type: image/png");
imagejpeg($image_y);
?>[/code]

It should do the same thing, just a little differently. If that doesn't work, the problem doesn't lie in the PHP.
Link to comment
Share on other sites

Thanks!

By just pasting it in and not playing with it at all yet, it showed what it should.

Any idea why the first block of code you pasted worked for you and not me, and why this new block seems to work and not the others? It is a PHP version differences because I'm running 5 and GD 2.0.28?

The only differences I see in the the "broken" blocks and the new block is that you are assigning the color black to the inside square as opposed to just leaving the initial color black as is and the major difference being you are using regular "imagecreate" instead of "imagecreatetruecolor". If those were the difference makers, will not using a "imagecreatetruecolor" image (As the PHP white paper says you should) effect the overall look of my images I create?

...I'm just trying to understand the weird issue as much as I can, and I can't find ANY information about this anywhere on the net.
Link to comment
Share on other sites

I'm sporting 5.1.1 on PHP and gd 2.0.32 compiled with jpeg and png support. I don't do much work with PHP's image manipulation functionality so I can't really speculate what the troubles may be. If you are going to use imagecreatetruecolor, you may want to use imagecopymerge instead of imagecopy. Try it and see:

[code]<?php
$print = imagecreatetruecolor(300, 500);
$image = imagecreatetruecolor(555, 555);
$mat1 = imagecolorallocate($image, 255, 255, 0);

if ( !imagefill($image, 0, 0, $mat1) ) {
die("Image not filling!");
}

imagecopymerge($image, $print, 50, 50, 0, 0, 300, 500, 100);

header("Content-type: image/jpg");
imagejpeg($image);
imagedestroy($image);
?>[/code]
Link to comment
Share on other sites

Here's something interesting...I found something related to this problem on PHP's page for imagefill().
It was in the discussion section at the bottom.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
glimpse at glimpse-fr dot org
25-Apr-2005 06:42
In response to philip at fcknet dot dk :

It depends on the version of PHP. In some 4.3 releases (but I'm not sure to know the exact version number), you must use the tips given by latortugamorada at yahoo dot com :
"you have to use imagecreate and fill it and then imagecopy that onto the imagecreatetruecolor..."

Thank you very much, latortugamorada at yahoo dot com!
philip at fcknet dot dk
28-Feb-2005 08:31
In response to latortugamorada at yahoo dot com at 21-Jan-2005 07:56.

Imagefill works fine with imagecreatetruecolor. I tested on both Windows XP Home (PHP 5.0.3, Apache 2.x), and FreeBSD v. somethin PHP 5.0.3. Both worked absolutely fine.
latortugamorada at yahoo dot com
21-Jan-2005 10:56
Hey, um, this imagefill stuff doesn't work with imagecreatetruecolor. you have to use imagecreate and fill it and then imagecopy that onto the imagecreatetruecolor... if that makes any sense.
[/quote]

The really weird thing was that discussion above is only available on the Google cache of the page, not on the current page. Someone edited that conversation out, which was beyond dumb because the problem clearly still exists.

Current page:[a href=\"http://us2.php.net/imagefill\" target=\"_blank\"]http://us2.php.net/imagefill[/a]
Cache: [a href=\"http://64.233.179.104/search?q=cache:n28XDhkQxPkJ:www.php.net/imagefill+imagecreatetruecolor+imagefill&hl=en&gl=us&ct=clnk&cd=2&client=safari\" target=\"_blank\"]http://64.233.179.104/search?q=cache:n28XD...2&client=safari[/a]
Link to comment
Share on other sites

With the colors we need to use in this project, not using imagecreatetruecolor() doesn't seem to be an option.

Does anyone kow of anyway that I can fix this bug and be able to use imagefill() on an imagecreatetruecolor() image?

UPDATE:
Though the issue is not resolved. I have found 2 hacks that can make this work with an imagecreatetruecolor() image:

1. though imagefill() doesn't work, I've found that imagefilledrectangle() does, so you just fill a rectangle the total size of your image and it works.
2. Colors don't work with imagefill() but tiled images do.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.