0

I am trying to find background color of png images having grey color or not.

Do you have any ideas?

Matthias
  • 4,481
  • 12
  • 45
  • 84

1 Answers1

0

If you speak about background-color of images in html, jquery can do the trick:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img src="image.png" id='image_id' style="background-color:Gray;" />

<script type="text/javascript">
console.log($('#image_id').css('background-color'));
if( $('#image_id').css('background-color') == 'rgb(128, 128, 128)' ) {
   console.log('Gray!');
} else {
   console.log('Not gray');
} 
</script>
Anton
  • 919
  • 7
  • 22
  • If possible can you help me with PHP as i have 50,000 images to check which i cannot use jquery – Sreedhar Kurur Sep 28 '18 at 11:01
  • You need to check files on disk, am i right? – Anton Sep 28 '18 at 11:03
  • I need to run the script in backend for optimization – Sreedhar Kurur Sep 28 '18 at 11:06
  • So, these images are stored on the disk as separate files? You need to check background-color property of images in html pages? – Anton Sep 28 '18 at 11:10
  • I am not storing the images, i am having the URL's of images – Sreedhar Kurur Sep 28 '18 at 11:11
  • That doesn't really get the background of *images* but the CSS background property assigned to image *tags*. The two are different - the image would be the actual file fetched and displayed by the browser. On a different note `'rgb(127, 127, 127)' would also be gray. As would many other variations, like `'rgb(127, 128, 129)'` – VLAZ Sep 28 '18 at 11:20
  • Thus you will need to get every image using file_get_contents and apply method described here https://stackoverflow.com/questions/1746530/get-image-color. Don't forget about max_execution_time in ini file. And the last - PHP isn't suited for long running tasks. – Anton Sep 28 '18 at 12:21