Share on Google+Share on Google+

PHP GD image grayscale

Advertisement
This example shows how to display that the particular image is grayscale or not in php gd.

<?php

$source_file = "images.jpg";

$im = ImageCreateFromJpeg($source_file);

$imgw = imagesx($im);

$imgh = imagesy($im);

$r = array();

$g = array();

$b = array();

$c = 0;

for ($i=0; $i<$imgw; $i++)

{

for ($j=0; $j<$imgh; $j++)

{

$rgb = ImageColorAt($im, $i, $j);

$r[$i][$j] = ($rgb >> 16) & 0xFF;

$g[$i][$j] = ($rgb >> 8) & 0xFF;

$b[$i][$j] = $rgb & 0xFF;

if ($r[$i][$j] == $g[$i][$j] && $r[$i][$j] == $b[$i][$j])

{

$c++;

}

}

}

if ($c == ($imgw*$imgh))

{

echo "The image is grayscale.";

}

else

{

echo "The image is NOT grayscale.";

}

?>

After running the program you will get the following output

OUTPUT:The image is NOT grayscale

Advertisements

Advertisement

Posted on: November 7, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.