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主题也是被各种大佬开发出来,十分钦佩,向大佬仰望,大声称赞:流批!!! 我在翻看各种主 ...
随机推荐
- 2020-1-19 2.港股打新、REITs和分拆
1.港股打新介绍 港股打新升级版 财属目由尽握手中 港股中签率较高 A股提高中签率 港股提高中签之后的收益率 有可能破发 2.第一版港股打新 港股打新第一版 ■第一种策略:只选择低于1.5港币的 配售 ...
- Linux中级之keepalived配置
hacmp: ibm的高可用集群软件,并且是商业的(收费),一般用于非x86架构机器当中 AIX,Unix 去IOE:ibm,oracle,emckeepalived: 一款高可用集群软件,利用vrr ...
- python文件处理(对比和筛选)
#!/user/bin/python #!coding=utf-8 # -*- coding: utf-8 -*- # 2017-9-25 #author:jingwenshuai import sy ...
- Java反射机制详情
1.运行环境 JDK8+lntellij IDEA 2018.3 2.反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个 ...
- 解决1字节的UTF-8序列的字节1无效问题
学习路上碰到了这个异常 解决方法如下: 1.手动将< ? xml version="1.0" encoding="UTF-8"?>中的UTF-8更改 ...
- 使用goland调试远程代码
前言 很多时候我们都在window上使用goland,并直接使用goland调试go代码. 但是很多时候我们的程序运行在Linux服务器上,虽然可以通过dlv命令行进行手动打断点调试,但是太麻烦了. ...
- MegEngine基本概念
MegEngine基本概念 基本概念 MegEngine 是基于计算图的深度神经网络学习框架. 本文内容会简要介绍计算图及其相关基本概念,以及在 MegEngine 中的实现. 计算图 结合一个简单的 ...
- JVM中的堆的新生代、老年代、永久代详解
JVM中的堆一般分为三大部分:新生代.老年代.永久代,其大致的占比如下: 一.新生代 新生代主要用来存放新生的对象.一般占据堆空间的1/3.在新生代中,保存着大量的刚刚创建的对象,但是大部分的对象都 ...
- 使用BootstrapVue相关组件,构建Vue项目界面
基于Vue的前端框架有很多,Element算一个,而BootstrapVue也可以非常不错的一个,毕竟Bootstrap也是CSS中的大佬级别的,它和Vue的整合,使得开发起来更加方便了.Bootst ...
- 题解 P3232 [HNOI2013]游走
洛谷P3232[NOI2013]游走 题目描述 给定一个 n 个点 m 条边的无向连通图,顶点从 1 编号到 n,边从 1 编号到 m. 小 Z 在该图上进行随机游走,初始时小 Z 在 1 号顶点,每 ...