Jas*_*vis 32 php gd imagemagick image-processing
我试图复制Dribbble.com检测图像中主要颜色的功能.在下图中,您可以看到Dribbble.com的屏幕截图,其中显示了左侧图像中的8种主要颜色.这是图片中的实际页面http://dribbble.com/shots/528033-Fresh-Easy?list=following
我需要能够在PHP中执行此操作,一旦我获得了我需要的颜色,我将它们保存到数据库中,因此不需要在每个页面加载时运行处理.
在对如何从图像中获取这些颜色的一些研究之后,有些人说你只是逐个像素地检查图像,然后保存最多出现的颜色.其他人说它还有更多,并且获得最常见的颜色不会产生预期的效果.他们说你需要量化图像/颜色(此时我迷失了).
在下面的图像中,下面的Dribble镜头是一个Javascript库,可以执行相同的操作,可以在此处查看该页面http://lokeshdhakar.com/projects/color-thief/
查看该页面的来源,我可以看到有一个名为Javascript的文件quantize.js,结果非常好.所以我希望能够使用PHP和GD/ImageMagick来完成Javascript库的功能

我发现这个函数将使用PHP返回颜色和计数,但结果与上面的Javascript版本和Dribble结果不同
/**
* Returns the colors of the image in an array, ordered in descending order, where the keys are the colors, and the values are the count of the color.
*
* @return array
*/
function Get_Color()
{
if (isset($this->image))
{
$PREVIEW_WIDTH = 150; //WE HAVE TO RESIZE THE IMAGE, BECAUSE WE ONLY NEED THE MOST SIGNIFICANT COLORS.
$PREVIEW_HEIGHT = 150;
$size = GetImageSize($this->image);
$scale=1;
if ($size[0]>0)
$scale = min($PREVIEW_WIDTH/$size[0], $PREVIEW_HEIGHT/$size[1]);
if ($scale < 1)
{
$width = floor($scale*$size[0]);
$height = floor($scale*$size[1]);
}
else
{
$width = $size[0];
$height = $size[1];
}
$image_resized = imagecreatetruecolor($width, $height);
if ($size[2]==1)
$image_orig=imagecreatefromgif($this->image);
if ($size[2]==2)
$image_orig=imagecreatefromjpeg($this->image);
if ($size[2]==3)
$image_orig=imagecreatefrompng($this->image);
imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); //WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS
$im = $image_resized;
$imgWidth = imagesx($im);
$imgHeight = imagesy($im);
for ($y=0; $y < $imgHeight; $y++)
{
for ($x=0; $x < $imgWidth; $x++)
{
$index = imagecolorat($im,$x,$y);
$Colors = imagecolorsforindex($im,$index);
$Colors['red']=intval((($Colors['red'])+15)/32)*32; //ROUND THE COLORS, TO REDUCE THE NUMBER OF COLORS, SO THE WON'T BE ANY NEARLY DUPLICATE COLORS!
$Colors['green']=intval((($Colors['green'])+15)/32)*32;
$Colors['blue']=intval((($Colors['blue'])+15)/32)*32;
if ($Colors['red']>=256)
$Colors['red']=240;
if ($Colors['green']>=256)
$Colors['green']=240;
if ($Colors['blue']>=256)
$Colors['blue']=240;
$hexarray[]=substr("0".dechex($Colors['red']),-2).substr("0".dechex($Colors['green']),-2).substr("0".dechex($Colors['blue']),-2);
}
}
$hexarray=array_count_values($hexarray);
natsort($hexarray);
$hexarray=array_reverse($hexarray,true);
return $hexarray;
}
else die("You must enter a filename! (\$image parameter)");
}
Run Code Online (Sandbox Code Playgroud)
所以我问是否有人知道如何用PHP做这样的任务?可能已经存在的东西,你知道或任何提示让我更接近这样做将不胜感激
Epo*_*poc 38
以下是您在PHP中寻找的内容:https://github.com/thephpleague/color-extractor
示例:
use League\ColorExtractor\ColorExtractor;
use League\ColorExtractor\Palette;
$palette = Palette::fromFilename('some/image.png');
$topEightColors = $palette->getMostUsedColors(8);
Run Code Online (Sandbox Code Playgroud)
小智 13
这是获取图像主色的简单方法
$image=imagecreatefromjpeg('image.jpg');
$thumb=imagecreatetruecolor(1,1);
imagecopyresampled($thumb,$image,0,0,0,0,1,1,imagesx($image),imagesy($image));
$mainColor=strtoupper(dechex(imagecolorat($thumb,0,0)));
echo $mainColor;
Run Code Online (Sandbox Code Playgroud)
您需要缩小图片,然后您将获得图片的主要颜色.如果托盘中需要4种颜色,请将其缩小至约8x86种颜色 12x8,以此类推......
imagecopyresized 缩小图像然后检查每个像素并将它们存储在数组中 imagecolorat($image,px,py)
试试吧
<?php
// EXAMPLE PICTURE
$url='https://www.nordoff-robbins.org.uk/sites/default/files/google.jpg';
//var_dump(getColorPallet($url));
echoColors(getColorPallet($url));
function echoColors($pallet){ // OUTPUT COLORSBAR
foreach ($pallet as $key=>$val)
echo '<div style="display:inline-block;width:50px;height:20px;background:#'.$val.'"> </div>';
}
function getColorPallet($imageURL, $palletSize=[16,8]){ // GET PALLET FROM IMAGE PLAY WITH INPUT PALLET SIZE
// SIMPLE CHECK INPUT VALUES
if(!$imageURL) return false;
// IN THIS EXEMPLE WE CREATE PALLET FROM JPG IMAGE
$img = imagecreatefromjpeg($imageURL);
// SCALE DOWN IMAGE
$imgSizes=getimagesize($imageURL);
$resizedImg=imagecreatetruecolor($palletSize[0],$palletSize[1]);
imagecopyresized($resizedImg, $img , 0, 0 , 0, 0, $palletSize[0], $palletSize[1], $imgSizes[0], $imgSizes[1]);
imagedestroy($img);
//CHECK IMAGE
/*header("Content-type: image/png");
imagepng($resizedImg);
die();*/
//GET COLORS IN ARRAY
$colors=[];
for($i=0;$i<$palletSize[1];$i++)
for($j=0;$j<$palletSize[0];$j++)
$colors[]=dechex(imagecolorat($resizedImg,$j,$i));
imagedestroy($resizedImg);
//REMOVE DUPLICATES
$colors= array_unique($colors);
return $colors;
}
?>
Run Code Online (Sandbox Code Playgroud)
适合我的作品.
| 归档时间: |
|
| 查看次数: |
40256 次 |
| 最近记录: |