此源码有个阈值可以自己调节,精确度等自测
class
Colorimg
{
public
$image
;
private
$cs
;
public
function
IMGaction(
$imgurl
,
$if_url
=1,
$if_deflate
=0,
$cs
=
'50'
) {
if
(
$if_url
==1) {
$image
=
$this
->ImgcolorCRRATE(
$imgurl
);
}
else
{
$image
=
$imgurl
;
}
if
(
$if_deflate
==1) {
$image
=
$this
->ImgDEFLATE(
$image
);
}
$sample
=
$this
->ColorGETMEANrgb(
$image
);
$image
=
$this
->ImgsetPIXEL(
$image
,
$sample
,
$cs
);
return
$image
;
}
public
function
ImgcolorCRRATE(
$image
)
{
list(
$width
,
$height
) =
getimagesize
(
$image
);
$img_info
=
getimagesize
(
$image
);
switch
(
$img_info
[2]) {
case
1:
$img
= imagecreatefromgif(
$image
);
break
;
case
2:
$img
= imagecreatefromjpeg(
$image
);
break
;
case
3:
$img
= imagecreatefrompng(
$image
);
break
;
}
return
$img
;
}
public
function
ImgDEFLATE(
$image
,
$rate
=
'800'
)
{
$w
= imagesx(
$image
);
$h
= imagesy(
$image
);
$max
=
$rate
;
if
(
$w
>
$h
) {
$w
=
$max
;
$h
=
$h
* (
$max
/ imagesx(
$image
));
}
else
{
$h
=
$max
;
$w
=
$w
* (
$max
/ imagesy(
$image
));
}
$i
= imagecreatetruecolor(
$w
,
$h
);
imagecopyresampled(
$i
,
$image
, 0, 0, 0, 0,
$w
,
$h
, imagesx(
$image
), imagesy(
$image
));
return
$i
;
}
public
function
ColorRECKmean(
$rgbarrays
)
{
$sum
=
count
(
$rgbarrays
);
$mean1
[
'r'
] =
''
;
$mean1
[
'g'
] =
''
;
$mean1
[
'b'
] =
''
;
foreach
(
$rgbarrays
as
$rbg
) {
$mean1
[
'r'
] +=
$rbg
[
'r'
];
$mean1
[
'g'
] +=
$rbg
[
'g'
];
$mean1
[
'b'
] +=
$rbg
[
'b'
];
}
$mean
[
'r'
] =
intval
(
$mean1
[
'r'
] /
$sum
);
$mean
[
'g'
] =
intval
(
$mean1
[
'g'
] /
$sum
);
$mean
[
'b'
] =
intval
(
$mean1
[
'b'
] /
$sum
);
return
$mean
;
}
public
function
ColorGETMEANrgb(
$image
)
{
$rgb1
= imagecolorat(
$image
, 0, 0);
$rgb2
= imagecolorat(
$image
, 0, imagesy(
$image
) - 1);
$rgb3
= imagecolorat(
$image
, imagesx(
$image
) - 1, 0);
$rgb4
= imagecolorat(
$image
, imagesx(
$image
) - 1, imagesy(
$image
) - 1);
$sample
=
$this
->ColorRECKmean(
array
(
$this
->ColorRGBresolved(
$rgb1
)),
$this
->ColorRGBresolved(
$rgb2
),
$this
->ColorRGBresolved(
$rgb3
),
$this
->ColorRGBresolved(
$rgb4
));
return
$sample
;
}
public
function
ImgsetPIXEL(
$image
,
$sample
,
$cs
){
for
(
$x
= 0;
$x
< imagesx(
$image
);
$x
++) {
for
(
$y
= 0;
$y
< imagesy(
$image
);
$y
++) {
$rgb
= imagecolorat(
$image
,
$x
,
$y
);
$than
=
$this
->ColorTHANrgb(
$this
->ColorRGBComp(
$this
->ColorRGBresolved(
$rgb
),
$sample
),
$cs
);
if
(
$than
) {
$color
= imagecolorallocate(
$image
, 255, 255, 255);
imagesetpixel(
$image
,
$x
,
$y
,
$color
);
}
}
}
return
$image
;
}
public
function
ColorRGBComp(
$rgb1
,
$rgb2
)
{
$tbsr
=
abs
(
$rgb1
[
'r'
] -
$rgb2
[
'r'
]);
$tbsg
=
abs
(
$rgb1
[
'g'
] -
$rgb2
[
'g'
]);
$tbsb
=
abs
(
$rgb1
[
'b'
] -
$rgb2
[
'b'
]);
$cv
= sqrt(pow(
$tbsr
, 2) + pow(
$tbsg
, 2) + pow(
$tbsb
, 2));
return
$cv
;
}
function
ColorRGBresolved(
$rgb
)
{
$img
[
'r'
] =
intval
((
$rgb
>> 16) & 0xFF);
$img
[
'g'
] =
intval
((
$rgb
>> 8) & 0xFF);
$img
[
'b'
] =
intval
((
$rgb
) & 0xFF);
return
$img
;
}
public
function
ColorTHANrgb(
$cv
,
$cs
)
{
if
(
$cv
<=
$cs
) {
return
true;
}
else
{
return
false;
}
}
}
友情提示:垃圾评论一律封号...