php 检查图片重复度,php – 检测图片的“整体平均”颜色
你可以使用PHP獲得一個調色板數組,如下所示:
function colorPalette($imageFile, $numColors, $granularity = 5)
{
$granularity = max(1, abs((int)$granularity));
$colors = array();
$size = @getimagesize($imageFile);
if($size === false)
{
user_error("Unable to get image size data");
return false;
}
$img = @imagecreatefromjpeg($imageFile);
// Andres mentioned in the comments the above line only loads jpegs,
// and suggests that to load any file type you can use this:
// $img = @imagecreatefromstring(file_get_contents($imageFile));
if(!$img)
{
user_error("Unable to open image file");
return false;
}
for($x = 0; $x < $size[0]; $x += $granularity)
{
for($y = 0; $y < $size[1]; $y += $granularity)
{
$thisColor = imagecolorat($img, $x, $y);
$rgb = imagecolorsforindex($img, $thisColor);
$red = round(round(($rgb['red'] / 0x33)) * 0x33);
$green = round(round(($rgb['green'] / 0x33)) * 0x33);
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
$thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
if(array_key_exists($thisRGB, $colors))
{
$colors[$thisRGB]++;
}
else
{
$colors[$thisRGB] = 1;
}
}
}
arsort($colors);
return array_slice(array_keys($colors), 0, $numColors);
}
// sample usage:
$palette = colorPalette('rmnp8.jpg', 10, 4);
echo "
foreach($palette as $color)
{
echo "
?#$color\n";}
echo "
\n";它給出一個數組,其值高于使用該顏色的頻率。
編輯
評論者問如何使用這一目錄中的所有文件,這里是:
if ($handle = opendir('./path/to/images')) {
while (false !== ($file = readdir($handle))) {
$palette = colorPalette($file, 10, 4);
echo "
foreach($palette as $color) {
echo "
?#$color\n";}
echo "
\n";}
closedir($handle);
}
可能不想對太多的文件執行此操作,但它是您的服務器。
或者,如果你寧愿使用Javascript Lokesh’s Color-Theif庫確實正在尋找什么。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的php 检查图片重复度,php – 检测图片的“整体平均”颜色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vuejs和php的区别,VueJS全面
- 下一篇: java 对象数据 搜索,java –