Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    235
    Thanks
    6
    Thanked 21 Times in 20 Posts

    Detecting colors in an image

    I have already made this short function:

    PHP Code:
    function fetch_palette($img)
    {
        
    $imgDetails getimagesize($img);
        
        
    $x 1;
        
    $y 1;
        
        while (
    $y <= intval($imgDetails[1]))
        {
            while (
    $x <= intval($imgDetails[0]))
            {
                
    $color imagecolorat(imagecreatefromjpeg($img), $x$y);
                if (
    $colors[$color])
                {
                    
    $tmp $colors[$color];
                    
    $tmp intval($tmp);
                    
    $colors[$color] = $tmp;
                }
                else
                {
                    
    $colors[$color] = 1;
                }
                
                
    $x++;
            }
            
            
    $x 1;
            
    $y++;    
        }
        return 
    $colors;

    but as you can tell, running this on an image thats bigger than 50x50 and it starts really messing with the server, and over 300x300 it will actually timeout due to it taking over 60 seconds to load.

    Is there something I can do to make it more efficient, or is there a "get_palette" already existing in PHP?

    The overall purpose is that I find the most popular used colours in the image palette.
    Last edited by udjamaflip; 05-11-2008 at 12:36 PM.
    Andrew Sharman
    Web designer, developer and programmer.

    If you found my post helpful, why not give thanks! :)

  • #2
    Regular Coder Stooshie's Avatar
    Join Date
    Mar 2008
    Location
    Dundee, Scotland
    Posts
    378
    Thanks
    9
    Thanked 39 Times in 39 Posts
    Are you doing this when someone is loadin a page, or when the image is uploaded?

    You would be better to do the latter, if you are not already, and save the palette details to a database and pull these figures when someone views the page.
    Regards, Stooshie
    O


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •