日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2025/3/21 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 – 检测图片的“整体平均”颜色的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。