
- Drop me a line! Contact me here.
Black and White in GD
An alternative method of creating a black ad white image is to use the variance, or brightness, of the colours. The brightness channel will have the highest value, so all we need do is find the highest value and return it.
This method is better than the first function as it produces a result nearer to the original image. In the first method high values in a channel that we were not using didn't affect the final image at all, while in this version all three channels are used, so high values in any channel can have an affect.
Of course, this still isn't quite perfect.
The next method of colour to black and white conversion takes an average value of all the colours in the source image and uses this as the output value.
This is a small improvement for some images, but in images with wildly varying colour the result tends to appear washed out and dull.
When the human eye looks at things it doesn't see all colours in the same way. The eye is less sensitive to green hues then it is to red, and less to red than it is to blue. Therefore to covert to black and white in a way that properly maintains the difference between colours in the original image as it does in the destination image we need to use a function that considers the values of the input colours.
The final method of greyscale conversion is based around the NTSC RGB YIQ colour space conversion algorithm. Using a set of ratios we take part of the red, green and blue channels and combine them to use as our value.
These ratio values are predefined as part of the YIQ algorithm.

<?php
function greyscale($r,$g,$b) {
return max($r,$g,$b);
}
?>
This method is better than the first function as it produces a result nearer to the original image. In the first method high values in a channel that we were not using didn't affect the final image at all, while in this version all three channels are used, so high values in any channel can have an affect.
Of course, this still isn't quite perfect.
The next method of colour to black and white conversion takes an average value of all the colours in the source image and uses this as the output value.
<?php
function greyscale($r,$g,$b) {
return ($r+$g+$b)/3;
}
?>
This is a small improvement for some images, but in images with wildly varying colour the result tends to appear washed out and dull.
When the human eye looks at things it doesn't see all colours in the same way. The eye is less sensitive to green hues then it is to red, and less to red than it is to blue. Therefore to covert to black and white in a way that properly maintains the difference between colours in the original image as it does in the destination image we need to use a function that considers the values of the input colours.
The final method of greyscale conversion is based around the NTSC RGB YIQ colour space conversion algorithm. Using a set of ratios we take part of the red, green and blue channels and combine them to use as our value.
These ratio values are predefined as part of the YIQ algorithm.
<?php
function greyscale($r,$g,$b) {
return (($r*0.299)+($g*0.587)+($b*0.114));
}
?>

Comments
Comments are not currently being accepted for this article.
Sidebar
Published:
30/01/2007
Views:
2905
Author:
Chris Neale
Labels:
Print:
Sidebar:
PHP image function reference
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.0582 seconds
