sloth456 Posted March 17, 2009 Share Posted March 17, 2009 I'm having a little trouble, it's difficult to explain so I'll start with my code. <?php $file = 'test.jpg'; // Get the dimensions list($width, $height) = getimagesize($file); // Define our source image $source = imagecreatefromjpeg($file); // Creating the Canvas $bwimage= imagecreate($width, $height); //Create our 2 colours $white = imagecolorallocate($bwimage, 255, 255, 255); $black = imagecolorallocate($bwimage, 0, 0, 0); //Reads the origonal colors pixel by pixel for ($y=0;$y<$height;$y++) { for ($x=0;$x<$width;$x++) { $rgb = imagecolorat($source,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; //if red is 255 and green is over 197 then colour in black, else colour in white if($r=255 and $g>197) { imagesetpixel($bwimage,$x,$y,$black); } else { imagesetpixel($bwimage,$x,$y,$white); } ?> As you can see I've created a black and white image from the original jpeg. Now I want to read the new image pixel by pixel. I assumed I could read straight from $bwimage as follows: <?php $rgb = imagecolorat($bwimage,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; ?> But it tells me it's an invalid resource. I'm not very familiar with the GD library and am so close to finishing my script! Can someone shine any light on this? Quote Link to comment Share on other sites More sharing options...
mrdamien Posted March 18, 2009 Share Posted March 18, 2009 Your script works for me. Minus the 2 missing curly-braces, your script works. What version of PHP/GD are you using? <?php $file = 'test.jpg'; // Get the dimensions list($width, $height) = getimagesize($file); // Define our source image $source = imagecreatefromjpeg($file); // Creating the Canvas $bwimage= imagecreate($width, $height); //Create our 2 colours $white = imagecolorallocate($bwimage, 255, 255, 255); $black = imagecolorallocate($bwimage, 0, 0, 0); //Reads the origonal colors pixel by pixel for ($y=0;$y<$height;$y++) { for ($x=0;$x<$width;$x++) { $rgb = imagecolorat($source,$x,$y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> & 0xFF; $b = $rgb & 0xFF; //if red is 255 and green is over 197 then colour in black, else colour in white if($r=255 and $g>197) { imagesetpixel($bwimage,$x,$y,$black); } else { imagesetpixel($bwimage,$x,$y,$white); } } // This was missing } // This was missing ?> Quote Link to comment Share on other sites More sharing options...
sloth456 Posted March 18, 2009 Author Share Posted March 18, 2009 yeah, sorry. I forgot to include them in the code I posted here, in my actual script the curly brackets aren't missing. I know the first bit of code works. It's the second bit that doesn't. It says imagecolorat(): supplied argument is not a valid Image resource I'm using php version 5.2.8 and gd 2.0.34 Quote Link to comment Share on other sites More sharing options...
sloth456 Posted March 18, 2009 Author Share Posted March 18, 2009 bump... I'm willing to pay a bounty of $10 to paypal for anyone who can solve this one. Quote Link to comment Share on other sites More sharing options...
mrdamien Posted March 19, 2009 Share Posted March 19, 2009 Can you post the whole script? There's nothing I see that could be the culprit. 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.