All pastes #821033 Raw Edit

function.draw_colours

public text v1 · immutable
·published 2007-12-17 21:46 UTC
rendered paste body
<?PHP

    /*
    Function: draw_colours
    Description: Grabs five colours from the specified image and returns them as an array.
    Notes: $image must be a local image. Supported image types: jpg, gif, png
    */
    
    function draw_colours ($image, $print_errors=False) {
    
        if (!file_exists($image)) {
            if ($print_errors)
                print "draw_colours: \$image doesn't exist!";
            return False;
        } else {
            $extension = array_pop(explode(".", $image));
            switch ($extension) {
                case "jpg":
                case "jpeg":
                    $handle = @imagecreatefromjpeg($image);
                    break;
                case "gif":
                    $handle = @imagecreatefromgif($image);
                    break;
                case "png":
                    $handle = @imagecreatefrompng($image);
                    break;
                default:
                    if ($print_errors)
                        print "draw_colors: non-supported filetype";
                    return False;
            }
            $width  = @imagesx($handle);
            $height = @imagesy($handle);
            $points = array(
                array($width,   $height);   // bottom right pixel
                array(1,        $height);   // bottom left pixel
                array(1,        1);         // top left pixel
                array($width,   1);         // top right pixel
                array($width/2, $height/2); // very center pixel
            );
            $data  = array();
            foreach ($points as $point) {
                $data[] = @imagecolorat($handle, $point[0], $point[1]);
            }
            return $data;
            @imagedestroy($image);
        }
        
    }
    
    ?>