Frage

Just for fun Ich habe mich, wie die GD-Bibliothek verwenden, um eine Farbpalette aus einem Bild zu erstellen. Bisher habe ich GD verwendet, um einen Benutzer hochgeladene Bild auf eine geeignete Größe, um die Größe auf einer Webseite zum Anzeigen.

Nun möchte Ich mag Lage sein, etwa fünf oder so verschiedene Farben aus dem Bild zu erhalten, die den Bereich der Farben präsentiert darin vertreten. Sobald ich getan habe, dass ich eine ergänzende Palette auf diesen Farben generieren mag, die ich dann verschiedene Elemente auf der Seite färben verwenden können.

Jede Hilfe ich bekommen kann, wie ich die ursprüngliche Farbpalette finden würde, würde sehr geschätzt werden!

EDIT: Ich bin gekommen, um meine eigene Lösung, die Sie unten sehen können.

War es hilfreich?

Lösung

Nun, ich habe ein paar Tage verbracht Hantieren und das ist, wie ich es geschafft, meine Farbpalette zu bauen. Es ist ziemlich gut für mich gearbeitet, und Sie können die Größe der Farbpalette ändern, um mehr oder weniger Farben aus dem Bild zurückzukehren.

// 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;
}

Andere Tipps

Dies kann Ihnen helfen,

<?php
$im = ImageCreateFromJpeg($source_file);

$imgw = imagesx($im);
$imgh = imagesy($im);

// n = total number or pixels

$n = $imgw*$imgh;

$colors = array();

for ($i=0; $i<$imgw; $i++)
{
        for ($j=0; $j<$imgh; $j++)
        {

                $rgb = ImageColorAt($im, $i, $j);

                if (isset($colors[$rgb])) {
                    $colors[$rgb]++;
                }
                else {
                    $colors[$rgb] = 1;
                }

        }
}
asort($colors);
print_r($colors);

Die imagecolorat Funktion gibt Ihnen den Farbwert zu einem Pixel, das Sie das Bild verwenden kann, scannen und ein Farb Histogramm erstellen:

http://www.php.net/manual/en/function .imagecolorat.php

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top