Jump to content


Photo

GD and imagefill problems


  • Please log in to reply
10 replies to this topic

#1 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 09 March 2006 - 04:20 PM

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:
<?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
?>

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.

#2 k.soule

k.soule
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 10 March 2006 - 01:37 AM

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.

#3 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 02:20 AM

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.

#4 k.soule

k.soule
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 10 March 2006 - 02:43 AM

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

<?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);
?>

It should do the same thing, just a little differently. If that doesn't work, the problem doesn't lie in the PHP.

#5 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 03:15 AM

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.

#6 k.soule

k.soule
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 10 March 2006 - 03:40 AM

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:

<?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);
?>


#7 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 04:55 AM

Th├ąt block of code just gives me a big black square. It must be something with the "truecolor" which is truly bizarre and unfortunate because that is the preferred function.

#8 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 05:43 AM

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]

#9 k.soule

k.soule
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationIllinois

Posted 10 March 2006 - 01:03 PM

Could it even have something to do with what operating system? Are you on linux?

#10 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 02:43 PM

We are running Fedora. Not exactly an uncommon setup.

#11 mfflynn

mfflynn
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 10 March 2006 - 06:08 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users