更新了1.0.1版本
更新内容如下:
*增调了调用函数部分代码
*调整了对比部分代码
*调整了上传图片部分代码
写了一个PHP界面,用上传的图片与数据库中图片做对比以显示图片中的信息,但是完全混乱中
PHP上传图片的代码↓
<!DOCTYPE html>
<html>
<head>
<title>上传识图</title>
<link rel="stylesheet" href="shitu.css">
<meta charset="UTF-8">
</head>
<style>
*{
padding: 0;
margin: 0 atuo;
}
.top{
height: 300px;
width: 1000px;
display: flex;
justify-content: space-between;
}
.left{
text-align: center;
width: 400px;
height: 300px;
border: 1px dashed #cccccc;
background: #efefef;
}
.right{
width: 590px;
height: 300px;
border: 1px dashed #cccccc;
background: white;
}
#imgDiv{
width: 100vw;
height: 100vh;
}
#imgDiv img{
width: 10vw;
height: 10vh;
}
</style>
<body>
<div class="top">
<div class="left">
<form method="get" enctype="multipart/form-data">
<label for="inputFile" class="button">
<div style="line-height: 40px; margin: auto; width: 200px;height: 40px;color: #FFFFFF;background: #00B7EF;border: none;border-radius: 3px;">点击选择文件</div>
</label>
<input type="file" id="inputFile" style="display: none; " multiple="multiple"></form>
</div>
<div class="right"></div>
</div>
<div style="width: 998px;height: 45px; background: #FFFFFF;border: 1px solid #CCCCCC;line-height: 45px;">
<span id="wenjian">选中0张文件 共0KB</span>
<button style="border:none;background:#00B7EF ;color: white;width: 100px;height: 35px;" onclick="read()">开始对比</button>
<input type="file" id="inputFile" style="display: none; " multiple="multiple">
<button style="border:none;background:#00B7EF ;color: white;width: 100px;height: 35px;" onclick="sub()">开始上传</button>
</div>
<div id="imgDiv"></div>
<script>
let imgarr = []
var file = document.getElementById("inputFile");
let imgDiv = document.getElementById("imgDiv");
let wenjian = document.getElementById("wenjian");
let size1
file.addEventListener("change", function(event){
imgarr = [...file.files]
let arr = imgarr.map(v => {
console.log(v.size,"size")
return v.size
})
arr = arr.reduce((total, num) => {
return total + num;
})
wenjian.innerText = `选中${imgarr.length}张文件 共${arr}KB`
})
function sub() {
for (var i = 0; i < file.files.length; i++) {
let reader = new FileReader();
var file1 = file.files[i];
reader.readAsDataURL(file1);
reader.onload = function(result) {
imgDiv.innerHTML += '<img src="' + reader.result + '" alt=""/>'
// console.log(reader.result)
}
}
console.log(file.files)
}
function read() {
for (var i = 0; i < file.files.length; i++) {
let reader = new FileReader();
var aa=document. getElementById( reader.result ).value;
window. location.href="compare.php?data="+aa;
var file1 = file.files[i];
reader.readAsDataURL(file1);
reader.onload = function(result) {
//reader对象的result属性存储流读取的数据
imgDiv.innerHTML += '<img src="' + reader.result + '" alt="" />'
console.log(reader.result) // 打印出转换后的base64
}
}
}
</script>
</body>
</html>
进行图片对比的代码↓
<?php
<?php
/* 均值哈希
* 1.将图片缩小为8*8的尺寸
* 2.将小图片变为灰度图像
* 3.计算每个像素的灰度平均值
* 4.与平均值进行比较,大于等于为 1,小于为 0,得到指纹
* 5.将两个图片的指纹依次进行比较,相同 count++ count越大,相似度越高
* */
class img_compare{
// 比较相似度 实现步骤5
public function compare($img1,$img2){
static $self;
if(!$self) $self = new static;
$hash1 = $self->gethash($img1);
$hash2 = $self->gethash($img2);
if(strlen($hash1) !== strlen($hash2)) return false;
$count = 0;
$len = strlen($hash1);
for($i = 0; $i < $len; $i++){
if($hash1[$i] == $hash2[$i]){
$count++;
}
}
//return $count <= 10 ? true : false; 直接返回是否相似
return $count; // 相当于返回相似度
}
// 将图片文件返回为图像标识符 代表图片
public function getimg($url){
$name = pathinfo($url,PATHINFO_EXTENSION); // 获得图片文件的后缀名
$img = call_user_func('imagecreatefrom'. ( $name == 'jpg' ? 'jpeg' : $name ) , $url); // 由文件创建图片
return $img;
}
// 获得图片指纹
public function gethash($url){
$array = array();
$total = 0;
$new_img = imagecreatetruecolor(8,8); // 建立一个 8*8 的黑色图像
list($ex_w,$ex_h) = getimagesize($url); // 将获得的图像长宽赋值给 $ex_w $ex_h
$ex_img = $this->getimg($url); // 获得图片
imagecopyresampled($new_img, $ex_img,0,0,0,0,8,8,$ex_w,$ex_h); // 实现步骤 1 2
imagedestroy($ex_img); // 销毁原图
for($i=0;$i<8;$i++){
for($j=0;$j<8;$j++){
$gray = (imagecolorat($new_img, $j, $i) >> 8) & 0xFF; // 获得每个位置像素的索引值 0xFF 1111 1111
$array[$i][$j] = $gray; // 记录每个点的像素值
$total += $gray; // 计算总和
}
}
imagedestroy($new_img);
$average = intval($total / (8 * 8 * 2)); // 平均值 实现步骤3
$hash = '';
for($i=0;$i<8;$i++){
for($j=0;$j<8;$j++){
$hash .= ($array[$i][$j] >= $average) ? '1' : '0'; // 实现步骤4
}
}
return$hash;
}
}
新增调用代码↓
<?php
$conn = mysqli_connect("localhost", "root", "123456") or die("数据库链接错误");
$db_selected=mysqli_select_db($conn,"SafeAskM");//管他呢 先链接数据库
for($i=1,$i<=16,i++)
{
$pct = "SELECT picture FROM ask_cm";
$result=mysqli_query($conn,$pct);
$pce = mysqli_fetch_row($result);//这些目的应该是查询到数据库中所有图片
$reader = $_GET['data'];//这是上传图片的参数名
require_once('photocompare.php');//调用对比函数
$img = new img_compare();
$count = $img->compare($pce,$reader);
echo $count;
}
?>
//不是一个人写的 互相看不懂对方的代码 不知道咋改
上传图片界面
想要成为正常的能够对比图片内容并将对比结果输出的代码
应该问题很多(目移)
能指导至成功运行的采纳+打赏
这一段代码要取消注释啊,同时compare函数的两个参数换成:1.你数据库中获取到的图片,2.上传的图片。
/*$img = new img_compare();
$count = $img->compare('images/arrow.png','images/abc.jpg');
echo $count; */
这个需要把完整的项目代码提供出来,然后本地调试才可以。你这个项目最终要实现什么效果 ?
你这贴的不全,帮不上忙