Jump to content

Manipulating an image


sloth456

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

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.