php自动识别背景并且把它改为白色
此源码有个阈值可以自己调节,精确度等自测
<?php
/*$Colorimg = new Colorimg();
$image=$Colorimg->IMGaction("G:/www/20161220/demo/5.jpg",1,1,50);
//告诉浏览器以图片形式解析
header('content-type:image/jpeg');
imagejpeg($image, "G:/www/20161220/demo/3.jpg");
*/
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
;
}
/**
* $rate为图片长宽最大值
*/
public
function
ImgDEFLATE(
$image
,
$rate
=
'800'
)
{
$w
= imagesx(
$image
);
$h
= imagesy(
$image
);
//指定缩放出来的最大的宽度(也有可能是高度)
$max
=
$rate
;
//根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
if
(
$w
>
$h
) {
$w
=
$max
;
$h
=
$h
* (
$max
/ imagesx(
$image
));
}
else
{
$h
=
$max
;
$w
=
$w
* (
$max
/ imagesy(
$image
));
}
//声明一个$w宽,$h高的真彩图片资源
$i
= imagecreatetruecolor(
$w
,
$h
);
//关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
imagecopyresampled(
$i
,
$image
, 0, 0, 0, 0,
$w
,
$h
, imagesx(
$image
), imagesy(
$image
));
return
$i
;
}
/**
* 传入多维数组n个点计算平均值
*$rgbarrays=array(
* $rgb1=array(
* 'r'=>255,
* 'g'=>255,
* 'b'=>255
* )
* )
*/
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
;
}
/**
* 取四个点,返回平均点的rgb数组
*/
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
;
}
/**
* 比对颜色相似度
* $rgb1和$rgb2必须数组$rgb['r']....
*/
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
;
}
/**
*把rgb颜色分解成数组
*
*/
function
ColorRGBresolved(
$rgb
)
{
$img
[
'r'
] =
intval
((
$rgb
>> 16) & 0xFF);
$img
[
'g'
] =
intval
((
$rgb
>> 8) & 0xFF);
$img
[
'b'
] =
intval
((
$rgb
) & 0xFF);
return
$img
;
}
/**
* 对比像素是否相似,相似返回true
*/
public
function
ColorTHANrgb(
$cv
,
$cs
)
{
if
(
$cv
<=
$cs
) {
return
true;
}
else
{
return
false;
}
}
}
php自动识别背景并且把它改为白色的更多相关文章
- 关于将电脑背景+chrome等网页改成护眼豆沙绿
常用电脑的人都知道,白色等其他对比度大的颜色对眼伤害大,所以需换成柔和的豆沙绿,可长时间保证眼睛的不疲劳 windows浏览器: >>>>在桌面点右键,依次选属性(proper ...
- iOS8中如何将状态栏的字体颜色改为白色
网上的一些方法在我这行不通, 比如: UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent ...
- ios 将状态栏改为白色方法!
1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...
- IOS 将状态栏改为白色
1.将 View controller-based status bar appearance 删除(默认为 YES),或设置为YES 2.设置rootViewcontroller,如果为viewC ...
- CSS3常用属性(边框、背景、文本效果、2D转换、3D转换、过渡、有过渡效果大图轮播、动画)
CSS3边框: 1.CSS3圆角:border-radius 属性--创建边框线的圆角 <body style="font-size:24px; color:#60F;"& ...
- Swift - 状态栏颜色显示(字体、背景)
ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时间等部分: 背景部分:就是显示黑色或者图片的背景部分: 如下图:前景 ...
- 实现div毛玻璃背景
毛玻璃效果 ios里毛玻璃效果的使用非常多,本文介绍一个实现div毛玻璃背景的方法 CSS3 Filter CSS3的Filter主要用在图像的特效处理上,默认值为none,还有以下备选项: 1. ...
- 【Linux命令】setterm命令修改虚拟机颜色显示(目录及背景颜色)
VMware设置目录及颜色显示 进入linux界面,默认背景为黑色,字体为白色 一.setterm命令 setterm向终端写一个字符串到标准输出,调用终端的特定功能.在虚拟终端上使用,将会改变虚拟终 ...
- Hexo博客美化之蝴蝶(butterfly)主题魔改
Hexo是轻量级的极客博客,因为它简便,轻巧,扩展性强,搭建部署方便深受广大人们的喜爱.各种琳琅满路的Hexo主题也是被各种大佬开发出来,十分钦佩,向大佬仰望,大声称赞:流批!!! 我在翻看各种主 ...
随机推荐
- 【转】Jquery 使用Ajax获取后台返回的Json数据后,页面处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 经典问题--php/go输出n对括号的所有组合
问题 n对括号有多少种合法的组合,写出一个可以执行出该结果的函数: 当n=1时,输出["()"]; 当n=2时,输出["(())","()()&quo ...
- Nextcloud 使用教程
一.简介 Nextcloud是一个网盘式文件管理系统,多用户权限管理,多客户端,使用简单.可在浏览器中运行,也可下客户端,不论使用哪种方式运行,使用教程都是一样的. 只是在客户端中运行时能及时收到相应 ...
- Psexec和wmiexec的原理和区别
PSEXEC 针对远程建立连接的方式有两种,一种先建立IPC通道连接,然后直接使用,操作如下: net use \\192.168.0.1\ipc$ "password" /use ...
- pytest - 失败重运行机制:rerun
失败重运行机制 用例失败的情况下,可以重新运行用例 一旦用例失败,马上重新运行 安装插件:pip install pytest-rerunfailures 使用命令:--reruns 重试次数 如 - ...
- TaskManager任务管理工具类
TaskManager任务管理工具类 public class TaskManager { public static AbstractTask newTask(TaskContext taskIns ...
- deeplearning搜索空间
deeplearning搜索空间 搜索空间是神经网络搜索中的一个概念.搜索空间是一系列模型结构的汇集, SANAS主要是利用模拟退火的思想在搜索空间中搜索到一个比较小的模型结构或者一个精度比较高的模型 ...
- python_appium 之使用Appium Inspector定位工具进行元素识别,编写验证demo
一.前提条件 appium环境搭建完成,模拟器Genymotion 安装完成,且已经下载成功了模拟设备 二.元素识别操作步骤及demo 1.进入appium如下菜单 2.填写Desired Capab ...
- UF_CSYS 坐标系操作
Open C UF_CSYS_ask_csys_info 获取WCS坐标系的原点坐标和矩阵标识UF_CSYS_ask_matrix_of_object 获得对象 ...
- SpringCloud+Docker+Jenkins+GitLab+Maven实现自动化构建与部署实战
1.前言与初衷 本文章会涉及Docker常见命令基础知识点结合不同场景实操一起使用. 本文章会涉及结合工作过程中部署不同环境服务器的项目案例场景为初心进行实际细讲. 本文章主要讲述Docker.Jen ...