PHP基于原生GD库, 获取图片中文字颜色, 匹配稀有度
一,获取文字颜色部分
二, 匹配对应的稀有度数据
public function getTextColor(Request $request)
{
$file = $request->file('img');
$extension = $file->extension();
$imageSize = getimagesize($file);
$image = null;
if ($extension === 'png') {
$image = imagecreatefrompng($file);
}
if ($extension === 'webp') {
$image = imagecreatefromwebp($file);
}
if ($extension === 'jpg' || $extension === 'jpeg') {
$image = imagecreatefromjpeg($file);
}
if (is_null($image)) {
exit(json_encode(['code' => 400, 'message' => '未知图片格式']));
}
imagefilter($image, IMG_FILTER_CONTRAST, -50);
$result = $this->getTextColors($image, $imageSize[0], $imageSize[1]);
return $this->getRarity($result);
}
public function getTextColors($image, $imageWidth, $imageHeight)
{
$textRegionX = 0;
$textRegionY = 0;
$textRegionWidth = $imageWidth;
$textRegionHeight = $imageHeight;
$colors = array();
for ($x = $textRegionX; $x < $textRegionX + $textRegionWidth; $x++) {
for ($y = $textRegionY; $y < $textRegionY + $textRegionHeight; $y++) {
$color = @imagecolorat($image, $x, $y);
$rgb = imagecolorsforindex($image, $color);
$colors[$color] = isset($colors[$color]) ? $colors[$color] + 1 : 1;
}
}
arsort($colors);
$topColors = array_slice($colors, 0, 2, true);
$colorArr = [];
foreach ($topColors as $color => $count) {
$rgb = imagecolorsforindex($image, $color);
array_push($colorArr, "RGB({$rgb['red']},{$rgb['green']},{$rgb['blue']})");
}
return $colorArr[1];
}
public function getRarity(string $rgb)
{
$colors = [
[
'color' => '灰色',
'rarity' => '粗糙',
'rgb' => 'RGB(146,146,146)',
],
[
'color' => '白色',
'rarity' => '普通',
'rgb' => 'RGB(255,255,255)',
],
[
'color' => '绿色',
'rarity' => '优秀',
'rgb' => 'RGB(160,255,0)',
],
[
'color' => '蓝色',
'rarity' => '稀有',
'rgb' => 'RGB(0,171,255)',
],
[
'color' => '紫色',
'rarity' => '史诗',
'rgb' => 'RGB(255,72,255)',
],
[
'color' => '橙色',
'rarity' => '传说',
'rgb' => 'RGB(255,196,0)',
],
[
'color' => '黄色',
'rarity' => '暗金',
'rgb' => 'RGB(255,255,187)',
],
];
$result = collect($colors)->filter(function ($item) use ($rgb) {
return $item['rgb'] === $rgb;
})->values()->all();
if (empty($result)) {
return null;
}
return array_values($result)[0];
}
封装类
class GDTextColor
{
public function getTextRegion($image, $ext, $x, $y)
{
$image = self::load($image, $ext);
return self::crop($image, $x, $y);
}
public function getImageSize($file): array
{
$size = getimagesize($file);
return [
'width' => $size[0],
'height' => $size[1],
];
}
private static function load($file)
{
$extension = $file->extension();
$image = null;
if ($extension === 'png') {
$image = imagecreatefrompng($file);
}
if ($extension === 'webp') {
$image = imagecreatefromwebp($file);
}
if ($extension === 'jpg' || $extension === 'jpeg') {
$image = imagecreatefromjpeg($file);
}
if (is_null($image)) {
exit(json_encode(['code' => 400, 'message' => '未知图片格式']));
}
return $image;
}
private static function crop($image, int $x, int $y, int $width = 140, int $height = 30)
{
$config = [
'x' => $x,
'y' => $y,
'width' => $width,
'height' => $height,
];
return imagecrop($image, $config);
}
public function getTextColors($image, int $imageWidth = 140, int $imageHeight = 30)
{
imagefilter($image, IMG_FILTER_CONTRAST, -50);
$textRegionX = 0;
$textRegionY = 0;
$textRegionWidth = $imageWidth;
$textRegionHeight = $imageHeight;
$colors = array();
for ($x = $textRegionX; $x < $textRegionX + $textRegionWidth; $x++) {
for ($y = $textRegionY; $y < $textRegionY + $textRegionHeight; $y++) {
$color = @imagecolorat($image, $x, $y);
$rgb = imagecolorsforindex($image, $color);
$colors[$color] = isset($colors[$color]) ? $colors[$color] + 1 : 1;
}
}
arsort($colors);
$topColors = array_slice($colors, 0, 2, true);
$colorArr = [];
foreach ($topColors as $color => $count) {
$rgb = imagecolorsforindex($image, $color);
array_push($colorArr, "RGB({$rgb['red']},{$rgb['green']},{$rgb['blue']})");
}
return $colorArr[1];
}
public function adjustContrast($color, $contrast): string
{
$contrast = max(0, min(100, $contrast));
$contrast = $contrast / 100;
$color = str_replace('rgb(', '', $color);
$color = str_replace(')', '', $color);
$color = explode(',', $color);
$r = intval($color[0]);
$g = intval($color[1]);
$b = intval($color[2]);
$r /= 255;
$g /= 255;
$b /= 255;
$brightness = (2 * $contrast) - 1;
$saturation = $contrast;
$r = $r + $brightness;
$g = $g + $brightness;
$b = $b + $brightness;
if ($r > 1) {
$r = 1;
}
if ($g > 1) {
$g = 1;
}
if ($b > 1) {
$b = 1;
}
$r = $r * $saturation;
$g = $g * $saturation;
$b = $b * $saturation;
$r = round($r * 255);
$g = round($g * 255);
$b = round($b * 255);
$newColor = ($r << 16) | ($g << 8) | $b;
return dechex($newColor);
}
public function getRarity($rgb)
{
$colors = [
[
'color' => '灰色',
'rarity' => '粗糙',
'min' => [90, 90, 90],
'max' => [220, 220, 220]
],
[
'color' => '白色',
'rarity' => '普通',
'min' => [245, 245, 245],
'max' => [255, 255, 255]
],
[
'color' => '绿色',
'rarity' => '优秀',
'min' => [0, 90, 0],
'max' => [185, 255, 165]
],
[
'color' => '蓝色',
'rarity' => '稀有',
'min' => [0, 0, 128],
'max' => [0, 255, 255]
],
[
'color' => '紫色',
'rarity' => '史诗',
'min' => [128, 0, 128],
'max' => [255, 128, 255]
],
[
'color' => '橙色',
'rarity' => '传说',
'min' => [220, 128, 0],
'max' => [255, 200, 128]
],
[
'color' => '黄色',
'rarity' => '暗金',
'min' => [220, 128, 128],
'max' => [255, 255, 200]
]
];
$rgbArr = $this->parseRGB($rgb);
return $this->findMatchingColor($rgbArr, $colors);
}
public function findMatchingColor($rgb, $colors)
{
foreach ($colors as $range) {
$minR = $range['min'][0];
$maxR = $range['max'][0];
$minG = $range['min'][1];
$maxG = $range['max'][1];
$minB = $range['min'][2];
$maxB = $range['max'][2];
if ($rgb[0] >= $minR && $rgb[0] <= $maxR && $rgb[1] >= $minG && $rgb[1] <= $maxG && $rgb[2] >= $minB && $rgb[2] <= $maxB) {
return $range;
}
}
return null;
}
public function parseRGB($rgbString): array
{
$rgbString = str_ireplace(['RGB', '(', ')'], '', $rgbString);
$rgbValues = explode(',', $rgbString);
return array_map('intval', $rgbValues);
}
}