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

  • SEARCH
  • Home
  • 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 750051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:30:21+00:00 2026-05-14T14:30:21+00:00

Just for fun I’ve been looking at how to use the GD library to

  • 0

Just for fun I’ve been looking at how to use the GD library to create a colour palette from an image. So far I’ve used GD to resize a user uploaded image to an appropriate size for displaying on a webpage.

Now I’d like to be able to get about five or so different colours from the image that represent the range of colours present in it. Once I’ve done that I’d like to generate a complementary palette based upon those colours, which I can then use to colour different elements on the page.

Any help I can get about how I would find the initial colour palette would be much appreciated!

EDIT:
I’ve come to my own solution which you can see below.

  • 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-14T14:30:22+00:00Added an answer on May 14, 2026 at 2:30 pm

    Well I’ve spent a couple of days fiddling around and this is how I managed to build my colour palette. Its worked fairly well for me and you can change the size of the colour palette to return more or less colours from the image.

    // The function takes in an image resource (the result from one
    // of the GD imagecreate... functions) as well as a width and
    // height for the size of colour palette you wish to create.
    // This defaults to a 3x3, 9 block palette.
    function build_palette($img_resource, $palette_w = 3, $palette_h = 3) {
        $width = imagesx($img_resource);
        $height = imagesy($img_resource);
    
        // Calculate the width and height of each palette block
        // based upon the size of the input image and the number
        // of blocks.
        $block_w = round($width / $palette_w);
        $block_h = round($height / $palette_h);
    
        for($y = 0; $y < $palette_h; $y++) {
            for($x = 0; $x < $palette_w; $x++) {
                // Calculate where to take an image sample from the soruce image.
                $block_start_x = ($x * $block_w);
                $block_start_y = ($y * $block_h);
    
                // Create a blank 1x1 image into which we will copy
                // the image sample.
                $block = imagecreatetruecolor(1, 1);
    
                imagecopyresampled($block, $img_resource, 0, 0, $block_start_x, $block_start_y, 1, 1, $block_w, $block_h);
    
                // Convert the block to a palette image of just one colour.
                imagetruecolortopalette($block, true, 1);
    
    
                // Find the RGB value of the block's colour and save it
                // to an array.
                $colour_index = imagecolorat($block, 0, 0);
                $rgb = imagecolorsforindex($block, $colour_index);
    
                $colour_array[$x][$y]['r'] = $rgb['red'];
                $colour_array[$x][$y]['g'] = $rgb['green'];
                $colour_array[$x][$y]['b'] = $rgb['blue'];
    
                imagedestroy($block);
            }
        }
    
        imagedestroy($img_resource);
        return $colour_array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been developing a Smalltalk variant for just the fun of it and I
just changed from Netbeans to Eclipse just for fun (Only developer on the company,
I'm someone who writes code just for fun and haven't really delved into it
I want to develop simple Serverless LAN Chat program just for fun. How can
I've read the whole Dragon Book recently (just for fun, I'm not really planning
I'm doing a personal, just for fun, project that is using screen scraping to
I'm writing a basic planet viewer OpenGL Perl application, just for fun. I have
Just looking for the first step basic solution here that keeps the honest people
Just for fun, I develop a download manager and I'd like to know if
Just for fun I'm developing a native Win32 port of Mozilla XUL. XUL allows

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.