此源码有个阈值可以自己调节,精确度等自测

<?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自动识别背景并且把它改为白色的更多相关文章

  1. 关于将电脑背景+chrome等网页改成护眼豆沙绿

    常用电脑的人都知道,白色等其他对比度大的颜色对眼伤害大,所以需换成柔和的豆沙绿,可长时间保证眼睛的不疲劳 windows浏览器: >>>>在桌面点右键,依次选属性(proper ...

  2. iOS8中如何将状态栏的字体颜色改为白色

    网上的一些方法在我这行不通, 比如: UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent ...

  3. ios 将状态栏改为白色方法!

    1在Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO2 在需要改变状态栏颜色的ViewController中在ViewDidLoad方 ...

  4. IOS 将状态栏改为白色

    1.将 View controller-based status bar appearance 删除(默认为 YES),或设置为YES  2.设置rootViewcontroller,如果为viewC ...

  5. CSS3常用属性(边框、背景、文本效果、2D转换、3D转换、过渡、有过渡效果大图轮播、动画)

    CSS3边框: 1.CSS3圆角:border-radius  属性--创建边框线的圆角 <body style="font-size:24px; color:#60F;"& ...

  6. Swift - 状态栏颜色显示(字体、背景)

    ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时间等部分: 背景部分:就是显示黑色或者图片的背景部分: 如下图:前景 ...

  7. 实现div毛玻璃背景

    毛玻璃效果 ios里毛玻璃效果的使用非常多,本文介绍一个实现div毛玻璃背景的方法 CSS3 Filter CSS3的Filter主要用在图像的特效处理上,默认值为none,还有以下备选项:   1. ...

  8. 【Linux命令】setterm命令修改虚拟机颜色显示(目录及背景颜色)

    VMware设置目录及颜色显示 进入linux界面,默认背景为黑色,字体为白色 一.setterm命令 setterm向终端写一个字符串到标准输出,调用终端的特定功能.在虚拟终端上使用,将会改变虚拟终 ...

  9. Hexo博客美化之蝴蝶(butterfly)主题魔改

      Hexo是轻量级的极客博客,因为它简便,轻巧,扩展性强,搭建部署方便深受广大人们的喜爱.各种琳琅满路的Hexo主题也是被各种大佬开发出来,十分钦佩,向大佬仰望,大声称赞:流批!!! 我在翻看各种主 ...

随机推荐

  1. mysql数据库-运维合集

    目录 RDBMS 术语 整删改查操作 库操作 表操作 账号与授权 匹配符(条件查询) MySQL三大类数据类型 函数 其他操作 查看数据库的占用空间大小 开启慢查询 状态查询 字符集设置 忘记密码重置 ...

  2. 韩小韩 API接口

    官方网址:https://api.vvhan.com/ 天气API接口: https://api.vvhan.com/api/weather Bing每日图片API接口: https://api.vv ...

  3. [leetcode] 208. 实现 Trie (前缀树)(Java)

    208. 实现 Trie (前缀树) 实现Trie树,网上教程一大堆,没啥可说的 public class Trie { private class Node { private int dumpli ...

  4. nginx 的访问日志切割

    1. 高级用法–使用 nginx 本身来实现 当 nginx 在容器里,把 nginx 日志挂载出来的时候,我们发现就不适合再使用 kill -USR1 的方式去分割日志这时候当然就需要从 nginx ...

  5. Git如何下载clone指定的tag

    Git如何下载clone指定的tag 如上图,我想下载Tags标签为solution-4 的代码,如何处理呢? 命令如下: git clone --branch solution-4 git@gith ...

  6. C# 移除字符串头尾指定字符

    1 private void button1_Click(object sender, EventArgs e) 2 {//去掉字符串头尾指定字符 3 string MyInfo= "--中 ...

  7. NVIDIA深度架构

    NVIDIA深度架构 本文介绍A100 GPU,NVIDIA Ampere架构GPU的重要新功能. 现代云数据中心中运行的计算密集型应用程序的多样性推动了NVIDIA GPU加速的云计算的爆炸式增长. ...

  8. TensorRT深度学习训练和部署图示

    TensorRT深度学习训练和部署 NVIDIA TensorRT是用于生产环境的高性能深度学习推理库.功率效率和响应速度是部署的深度学习应用程序的两个关键指标,因为它们直接影响用户体验和所提供服务的 ...

  9. 重新整理 .net core 实践篇—————日志系统之服务与日志之间[十六]

    前言 前文介绍了一些基本思路,那么这里介绍一下,服务如何与配置文件配合. 正文 服务: public interface ISelfService { void ShowLog(); } public ...

  10. spring整合mybatis,ioc容器及声明式事务配置

    步骤: 1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息 jdbc.properties:jdbc.user=root jdbc.password=123456 jdbc. ...