2010-04-12 9 views
5

बस मज़े के लिए मैं एक छवि से रंग पैलेट बनाने के लिए जीडी लाइब्रेरी का उपयोग कैसे कर रहा हूं। अब तक मैंने एक वेबपृष्ठ पर प्रदर्शित करने के लिए एक उचित आकार में उपयोगकर्ता अपलोड छवि का आकार बदलने के लिए जीडी का उपयोग किया है।किसी छवि से रंग पैलेट जेनरेट करें

अब मैं उस छवि से लगभग पांच या बहुत अलग रंग प्राप्त करने में सक्षम होना चाहता हूं जो इसमें मौजूद रंगों की श्रृंखला का प्रतिनिधित्व करता है। एक बार मैंने ऐसा किया है कि मैं उन रंगों के आधार पर एक पूरक पैलेट उत्पन्न करना चाहता हूं, जिसे मैं पृष्ठ पर विभिन्न तत्वों को रंगाने के लिए उपयोग कर सकता हूं।

मुझे कोई भी मदद मिल सकती है कि मुझे प्रारंभिक रंग पैलेट कैसे मिलेगा, इसकी बहुत सराहना की जाएगी!

संपादित करें: मैं अपने स्वयं के समाधान में आया हूं जो आप नीचे देख सकते हैं।

उत्तर

8

मदद मिल सकती है खैर मैंने कुछ दिनों बिताए हैं और इस तरह मैं अपना रंग पैलेट बनाने में कामयाब रहा हूं। यह मेरे लिए काफी अच्छा काम करता है और आप छवि से कम या कम रंग लौटने के लिए रंग पैलेट के आकार को बदल सकते हैं।

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

imagecolorat समारोह एक पिक्सेल जो आप छवि को स्कैन और एक रंग हिस्टोग्राम बनाने के लिए उपयोग कर सकते हैं पर आप रंग मूल्य दे देंगे:

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

2

यह आप

<?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); 
संबंधित मुद्दे