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
https://forums.phpfreaks.com/topic/149898-manipulating-an-image/
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
?>

 

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

Archived

This topic is now archived and is closed to further replies.

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