php识别相似图片简易版

简介: php识别相似图片简易版

这个识别图片的原理是分析像素点,计算平均颜色,大于平均颜色则为1,小于则为0,然后进行比对

精确度很低,只能匹配形状和比例一样的图片

class img
{
//比较图片相似度
    public function cpimg($img1, $img2, $rate = '2')
    {
        $data1 = $this->dataimg($img1);
        $data2 = $this->dataimg($img2);
        $than=$this->thanimg($data1,$data2);
        $rate=$than/(64*$rate*$rate);
        return $rate;
    }
//计算图片数据
    public function dataimg($image,$if_url=1,$rate = '2')
    {
        if($if_url) {
            $image = $this->creatimg($image);
        }
        $result = $this->imgdeflate($image);
        return $result;
    }
    /**
     * 打开一张图片
     */
    public function creatimg($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 = '2')
    {
        $width = imagesx($image);
        $height = imagesy($image);
        $n_w = 8 * $rate;//新图片宽度
        $n_h = 8 * $rate;//新图片高度
        $new = imagecreatetruecolor($n_w, $n_h);//新建一张设定真彩色宽高的图
//取出一个png图形
//copy部分图像并调整
        imagecopyresized($new, $image, 0, 0, 0, 0, $n_w, $n_h, $width, $height);
//图像输出新图片、另存为
        imagefilter($new, IMG_FILTER_GRAYSCALE);//将图片转为64级灰度
//获取每个像素的灰度值
        $total = 0;
        $array = array();
        for ($y = 0; $y < $n_h; $y++) {
            for ($x = 0; $x < $n_w; $x++) {
                $gray = (imagecolorat($new, $x, $y) >> 8) & 0xFF;
                $array[$y] = array();
                $array[$y][$x] = $gray;
                $total += $gray;
                //echo $total.'<br>';
            }
        }//平均值计算
        //echo $total.'<br>';
        $average = intval($total / (64 * $rate * $rate));
        //echo $average."<br>";
        $total = 0;
        $result = '';
        $array = array();
        for ($y = 0; $y < $n_h; $y++) {
            for ($x = 0; $x < $n_w; $x++) {
                $gra = (imagecolorat($new, $x, $y) >> 8) & 0xFF;
                $array[$y][$x] = $gra;
                if ($gra >= $average) {
                    $result .= '1';
                } else {
                    $result .= '0';
                }
            }
        }
        return $result;
    }
//进行编辑距离数值比较
    public function thanimg($data1, $data2)
    {
        $dist = 0;
        $len1 = strlen($data1);
        $len2 = strlen($data2);
        if ($len1 == 0) {
            return $len2;
        }
        if ($len2 == 0) {
            return $len1;
        }
        for ($i = 0; $i <= $len1; $i++) {
            $matrix[$i][0] = 0;
        }
        for ($j = 0; $j <= $len2; $j++) {
            $matrix[0][$j] = 0;
        }
        for ($i = 1; $i <= $len1; $i++) {
            $ch1 = $data1[$i - 1];
            for ($j = 1; $j <= $len2; $j++) {
                $ch2 = $data2[$j - 1];
                $temp = $ch1 == $ch2 ? 0 : 1;
                $arr = array(
                    $matrix[$i - 1][$j] + 1,
                    $matrix[$i][$j - 1] + 1,
                    $matrix[$i - 1][$j - 1] + $temp
                );
                $matrix[$i][$j] = min($arr);
                $val=$matrix[$i][$j];
            }
        }
        return $val;
    }
    /*
     *
     *
     * 汉明距离
     */
    public function hamimg($data1, $data2){
        $len1 = strlen($data1);
        $len2 = strlen($data2);
        if($len1 != $len2)
        {
            return false;
        }
        $dist = 0;
        for($i = 0; $i < $len1; $i++)
        {
            if($data1[$i] != $data2[$i])
            {
                $dist++;
            }
        }
        return $dist;
    }
}
目录
相关文章
|
4月前
|
PHP
使用PHP实现随机调用图片
使用PHP实现随机调用图片
150 0
使用PHP实现随机调用图片
|
5月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
48 0
|
7月前
|
PHP
【PHP】读取本地文件夹中所有图片并显示
PHP图片收集系统收集作业后,为了方便老师在线查阅作业,特意写了个读取图片然后显示出来的php 比较粗糙,可以再多美化美化
65 0
|
13天前
|
PHP 数据库
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务-2
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务
|
13天前
|
存储 PHP Apache
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务-1
DIY私人图床:使用CFimagehost源码自建无需数据库支持的PHP图片托管服务
|
18天前
|
数据采集 机器学习/深度学习 存储
图片大搜罗:PHP下载器带你畅游Twitter图像海洋
构建Twitter图像下载器,使用PHP模拟请求抓取图像,通过代理IP规避限制。示例代码展示如何设置代理、用户代理和Cookie,解析HTML提取图像链接并下载。结合机器学习与元数据分析,可洞察用户行为和社会趋势。代理服务器信息及Twitter URL需自行替换。
图片大搜罗:PHP下载器带你畅游Twitter图像海洋
|
2月前
|
存储 PHP Apache
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
|
10月前
|
PHP
PHP实现自制随机图片API- 调用文件夹和引用网络图片
PHP实现随机图片API- 调用文件夹和引用网络图片
113 0
|
7月前
|
小程序 PHP
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)