PHP GD image grayscale


 

PHP GD image grayscale

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

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.";

0

}

?>

After running the program you will get the following output

OUTPUT:The image is NOT grayscale

Ads