2010-04-12 63 views
5

Chỉ để cho vui, tôi đã xem xét cách sử dụng thư viện GD để tạo bảng màu từ một hình ảnh. Cho đến nay tôi đã sử dụng GD để thay đổi kích thước hình ảnh do người dùng tải lên thành kích thước phù hợp để hiển thị trên trang web.Tạo bảng màu từ hình ảnh

Bây giờ tôi muốn có thể nhận được khoảng năm hoặc nhiều màu sắc khác nhau từ hình ảnh thể hiện phạm vi màu sắc có trong đó. Một khi tôi đã làm điều đó tôi muốn tạo ra một bảng màu bổ sung dựa trên những màu đó, sau đó tôi có thể sử dụng để tô màu các phần tử khác nhau trên trang.

Bất kỳ trợ giúp nào tôi có thể nhận được về cách tôi sẽ tìm thấy bảng màu ban đầu sẽ được nhiều người đánh giá cao!

EDIT: Tôi đã đến giải pháp của riêng mình mà bạn có thể xem bên dưới.

Trả lời

8

Vâng, tôi đã dành một vài ngày không quan tâm xung quanh và đây là cách tôi quản lý để xây dựng bảng màu của tôi. Nó hoạt động khá tốt với tôi và bạn có thể thay đổi kích thước của bảng màu để trả lại nhiều hay ít màu từ hình ảnh.

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

này có thể giúp bạn

<?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); 
Các vấn đề liên quan