日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

php 检查图片重复度,php – 检测图片的“整体平均”颜色

發(fā)布時(shí)間:2025/3/21 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 检查图片重复度,php – 检测图片的“整体平均”颜色 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

你可以使用PHP獲得一個(gè)調(diào)色板數(shù)組,如下所示:

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";

它給出一個(gè)數(shù)組,其值高于使用該顏色的頻率。

編輯

評(píng)論者問如何使用這一目錄中的所有文件,這里是:

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);

}

可能不想對(duì)太多的文件執(zhí)行此操作,但它是您的服務(wù)器。

或者,如果你寧愿使用Javascript Lokesh’s Color-Theif庫(kù)確實(shí)正在尋找什么。

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的php 检查图片重复度,php – 检测图片的“整体平均”颜色的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。