mfflynn Posted March 9, 2006 Share Posted March 9, 2006 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. Quote Link to comment Share on other sites More sharing options...
k.soule Posted March 10, 2006 Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
k.soule Posted March 10, 2006 Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
k.soule Posted March 10, 2006 Share Posted March 10, 2006 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] Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 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 org25-Apr-2005 06:42In 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 dk28-Feb-2005 08:31In 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 com21-Jan-2005 10:56Hey, 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] Quote Link to comment Share on other sites More sharing options...
k.soule Posted March 10, 2006 Share Posted March 10, 2006 Could it even have something to do with what operating system? Are you on linux? Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 We are running Fedora. Not exactly an uncommon setup. Quote Link to comment Share on other sites More sharing options...
mfflynn Posted March 10, 2006 Author Share Posted March 10, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.