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主题也是被各种大佬开发出来,十分钦佩,向大佬仰望,大声称赞:流批!!! 我在翻看各种主 ...
随机推荐
- 2.1 CPU 上下文切换(上)
cpu上下文与切换 进程在竞争 CPU 的时候并没有真正运行,为什么还会导致系统的负载升高呢?CPU 上下文切换就是罪魁祸首. 我们都知道,Linux 是一个多任务操作系统,它支持远大于 CPU 数量 ...
- ffmpeg安装之linux编译安装
转发自白狼栈:查看原文 关于ffmpeg的安装,有的人可能要折腾很久,甚至折腾一个礼拜,究其原因,基本都是编译安装惹的祸. 我们提供4种安装方式,最复杂的莫过于centos7上的编译安装. ffmpe ...
- 鸿蒙 Android iOS 应用开发对比02
个人理解,不抬杠 转载请注明原著:博客园老钟 https://www.cnblogs.com/littlecarry/ IOS 把界面抽象成 "控制" Controller:And ...
- python批量向kafka塞数据
python批量向kafka塞数据 from kafka import KafkaClient from kafka.producer import SimpleProducer from kafka ...
- NVIDIA空中导航SDK改造5G通信
NVIDIA空中导航SDK改造5G通信 Transforming Next-Generation Wireless with 5T for 5G and the NVIDIA Aerial SDK N ...
- NVIDIA Turing Architecture架构设计(下)
NVIDIA Turing Architecture架构设计(下) GDDR6 内存子系统 随着显示分辨率不断提高,着色器功能和渲染技术变得更加复杂,内存带宽和大小在 GPU 性能中扮演着更大的角色. ...
- 软件工具将GPU代码迁移到fpga以用于AI应用
软件工具将GPU代码迁移到fpga以用于AI应用 Software tools migrate GPU code to FPGAs for AI applications 人工智能软件初创公司Mips ...
- h265webplayer
h265webplayer https://github.com/ksvc/h265webplayer h265webplayer是金山云的Web端H.265视频播放器,该播放器Web SDK让您可以 ...
- adb安装 mac和Windows
一.mac安装 参考地址https://blog.csdn.net/VSRfind/article/details/79593098 1.首先安装一个软件 在用Mac进行Android开发之前,我们一 ...
- 说说对 Node 中的 fs 模块的理解? 有哪些常用方法?
一.是什么 fs(file system),该模块提供本地文件的读写能力,基本上是POSIX文件操作命令的简单包装 可以说,所有与文件的操作都是通过fs核心模块实现 导入模块如下: const fs ...