Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 417849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:37:15+00:00 2026-05-12T18:37:15+00:00

I’ve used GD before, but only ever for resizing/generating images on the fly –

  • 0

I’ve used GD before, but only ever for resizing/generating images on the fly – though I’m pretty positive it has the capabilities to do what I’m after.

As simply as possible, I need to check an image to find out whether it has a light background or a dark background. I.e. if the background is predominately ‘light’ I’m returned a value of ‘1’, and if it is predominately ‘dark’ it returns ‘0’.

There’s only going to be 5 images iterated through in this process at a time, but I’m very concious here of processing time – the page is going to be called often.

Can anyone point me in the right direction on where to go with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:37:15+00:00Added an answer on May 12, 2026 at 6:37 pm

    First see if there are any patterns you can take advantage of – for instance, is the top-left or top-right corner (for example) always going to be of the background colour? If so, just look at the colour of that pixel.

    Maybe you can get a “good enough” idea by looking at some key pixels and averaging them.

    Failing something simple like that, the work you need to do starts to rise by orders of magnitude.

    One nice idea I had would be to take the strip of pixels going diagonally across from the top-left corner to the bottom-right corner (maybe have a look at Bresenham’s line algorithm). Look for runs of dark and light colour, and probably take the longest run; if that doesn’t work, maybe you should “score” runs based on how light and dark they are.

    If your image is unnecessarily large (say 1000×1000 or more) then use imagecopyresized to cheaply scale it down to something reasonable (say 80×80).

    Something that will work if MOST of the image is background-colour is to resample the image to 1 pixel and check the colour of that pixel (or maybe something small, 4×4 or so, after which you count up pixels to see if the image is predominantly light or dark).

    Note that imagecopyresampled is considerably more expensive than imagecopyresized, since ‘resized just takes individual pixels from the original whereas ‘resampled actually blends the pixels together.

    If you want a measure of “lightness” you could simply add the R, G and B values together. Or you could go for the formula for luma used in YCbCr:

    Y' = 0.299 * R + 0.587 * G + 0.114 * B
    

    This gives a more “human-centric” measure of lightness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have text I am displaying in SIlverlight that is coming from a CMS
I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.