获取图像颜色 [英] Get image color

查看:142
本文介绍了获取图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在div或表格内显示图像作为背景。如果它们的图像不够大,我将需要找到该图像的最外层的颜色,并将其应用于包含div或表单元格的背景。



有没有人有这方面的经验?在PHP。我是一个noob所以请解释。非常感谢你

解决方案

查看 GD函数



这里是循环遍历像素以找到最常见的颜色。但是,您只需将图片调整为1像素即可



 <?php 
$ filename = $ _GET ['filename'];
$ image = imagecreatefromjpeg($ filename);
$ width = imagesx($ image);
$ height = imagesy($ image);
$ pixel = imagecreatetruecolor(1,1);
imagecopyresampled($ pixel,$ image,0,0,0,0,1,1,$ width,$ height);
$ rgb = imagecolorat($ pixel,0,0);
$ color = imagecolorsforindex($ pixel,$ rgb);
?>
< html>
< head>
< title>测试图像平均颜色< / title>
< / head>
< body style ='background-color:rgb(<?php echo $ color ['red']?>,<?php echo $ color ['green']?> ;,< ?php echo $ color ['blue']?>)'>
< form action =''method ='get'>
< input type ='text'name ='filename'>< input type ='submit'>
< / form>
< img src ='<?php echo $ filename?>'>
< / body>
< / html>

以下是一些查找平均边框颜色的示例代码,类似于第一链接。对于您的使用,这可能会更好(我知道这个代码是低效的,但希望很容易跟踪):

 <?php 
$ filename = $ _GET ['filename'];
$ image = imagecreatefromjpeg($ filename);
$ width = imagesx($ image);
$ height = imagesy($ image);

for($ y = 0; $ y< $ height; $ y ++){
$ rgb = imagecolorat($ image,0,$ y);
$ color = imagecolorsforindex($ image,$ rgb);
$ red + = $ color ['red'];
$ green + = $ color ['green'];
$ blue + = $ color ['blue'];

$ rgb = imagecolorat($ image,$ width -1,$ y);
$ color = imagecolorsforindex($ image,$ rgb);
$ red + = $ color ['red'];
$ green + = $ color ['green'];
$ blue + = $ color ['blue'];
}

for($ x = 0; $ x< $ height; $ x ++){
$ rgb = imagecolorat($ image,$ x,0);
$ color = imagecolorsforindex($ image,$ rgb);
$ red + = $ color ['red'];
$ green + = $ color ['green'];
$ blue + = $ color ['blue'];

$ rgb = imagecolorat($ image,$ x,$ height -1);
$ color = imagecolorsforindex($ image,$ rgb);
$ red + = $ color ['red'];
$ green + = $ color ['green'];
$ blue + = $ color ['blue'];
}

$ borderSize =($ height = $ width)* 2;
$ color ['red'] = intval($ red / $ borderSize);
$ color ['green'] = intval($ green / $ borderSize);
$ color ['blue'] = intval($ blue / $ borderSize);

?>

更新:我放了一些更精致的代码在github 。这包括平均边界和平均整个图像。应该注意,调整大小到1px比扫描每个像素(虽然我没有运行任何实时测试)资源更友好,但代码确实显示了三种不同的方法。


I want to display images inside divs or tables as backgrounds. If they images aren't large enough, I'm going to need to find the outer most color of that image and apply it to the background of the containing div or table cell.

Does anyone have experience with this? In PHP. I'm a noob so please explain. Thank you so much

解决方案

Check out the GD functions.

Here's a solution of looping through the pixels to find the most common color. However, you could just resize the image to 1 pixel - that should be the average color - right?

An example of the 1px method (now including test page):

<?php
  $filename = $_GET['filename'];    
  $image = imagecreatefromjpeg($filename);
  $width = imagesx($image);
  $height = imagesy($image);
  $pixel = imagecreatetruecolor(1, 1);
  imagecopyresampled($pixel, $image, 0, 0, 0, 0, 1, 1, $width, $height);
  $rgb = imagecolorat($pixel, 0, 0);
  $color = imagecolorsforindex($pixel, $rgb);
?>
<html>
  <head>
    <title>Test Image Average Color</title>
  </head>
  <body style='background-color: rgb(<?php echo $color['red'] ?>, <?php echo $color['green'] ?>, <?php echo $color['blue'] ?>)'>
    <form action='' method='get'>
      <input type='text' name='filename'><input type='submit'>
    </form>
    <img src='<?php echo $filename ?>'>
  </body>
</html>

Here's some example code for finding the average border color, similar to the first link. For your use this may work better (I know this code is inefficient, but hopefully it's easy to follow):

<?php
  $filename = $_GET['filename'];    
  $image = imagecreatefromjpeg($filename);
  $width = imagesx($image);
  $height = imagesy($image);

  for($y = 0; $y < $height; $y++){
    $rgb = imagecolorat($image, 0, $y);
    $color = imagecolorsforindex($image, $rgb);
    $red += $color['red'];
    $green += $color['green'];
    $blue += $color['blue'];

    $rgb = imagecolorat($image, $width -1, $y);
    $color = imagecolorsforindex($image, $rgb);
    $red += $color['red'];
    $green += $color['green'];
    $blue += $color['blue'];
  }

  for($x = 0; $x < $height; $x++){
    $rgb = imagecolorat($image, $x, 0);
    $color = imagecolorsforindex($image, $rgb);
    $red += $color['red'];
    $green += $color['green'];
    $blue += $color['blue'];

    $rgb = imagecolorat($image, $x, $height -1);
    $color = imagecolorsforindex($image, $rgb);
    $red += $color['red'];
    $green += $color['green'];
    $blue += $color['blue'];
  }

  $borderSize = ($height=$width)*2;
  $color['red'] = intval($red/$borderSize);
  $color['green'] = intval($green/$borderSize);
  $color['blue'] = intval($blue/$borderSize);

?>

Update: I put some more refined code on github. This includes both averaging the border, and averaging the entire image. It should be noted that resizing to 1px is far more resource friendly than scanning every pixel (although I haven't run any real time tests), but the code does show the three different methods.

这篇关于获取图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆