php搜索图片
‘壹’ php有没有简单的方法识别两张图片是否相似/相同
理论上肯定是可以的。扮举不过这应该是一个很高深的东西。所以,我只能给你我很粗浅的方法:
一:写个函数,这个函数的主要功能是找出一张图片上的主要颜色(代码在最后面)
二:把一张图片有GD库分成9份(3*3)或更多薯备份,然后用第一个函数来取得每一份小图的主要颜色。
三:比对这几份小图的颜色,看哪一份的颜色是你需要的。
如果你原数缺毁意,你也可以切很多份,比如10000份(100*100),然后计算这10000个颜色的中你需要的颜色的分布,找出你需要的区域。
$i=("image.jpg");
for($x=0;$x for($y=0;$y $rgb=($i,$x,$y); $r=($rgb<<16)&0xFF; $g=($rgb<<&0xFF; $b=$rgb&0xFF; $rTotal=$r; $gTotal=$g; $bTotal=$b; $total; } } $rAverage=round($rTotal/$total); $gAverage=round($gTotal/$total); $bAverage=round($bTotal/$total); ‘贰’ php从数据库中调用图片
一:可以所图片转码成代码存入数据库里,需要显示的地方取出来显示,好像有一个软件可以把图片转换成代码的,你可以找一下(不推荐使用此方法) ‘叁’ 用PHP获取链接及图片路径的方法 ‘肆’ php 正则查找 图片的src 并替换相应的src
/TestDemo/Tpl/default/Public/Files/2010_09_04/1283591048.jpg转 ‘伍’ php中如何调用数据库中的图片并且显示到页面 php是采用二进制形式存储图片及读取显示的,首先通过代码创建数据表,然后上传图片服务器再通过浏览器显示,具体编程代码举例: 1、首先需要创建数据表,具体代码如下图所示。 ‘陆’ php怎么实现根据图片搜索图片功能
php爱好者 们很对php开发的追求是永不止步的,今天偶然想起来了 发下 ‘柒’ 请问php有办法识别一张图片色块分布吗比如找出最适合添加文字的地方
理论上肯定是可以的。不过这应该是一个很高深的东西。所以,我只能给你我很粗浅的方法:
二:可以把要显示的图片上传到一个专门的文件夹里面
数据库时只存此图片的名称和位置
在网页需要显示的地方插入一个图片,图片的地址从数据库里取得,可以很方便的显示变换图像
<?php
$str="Thisisatest.Thisisatest.Thisisa<ahref=http://link1.com><imgsrc=http://img1.jpg/></a>test.Thisisatest.Thisisatest.
".
"Thisisatest.Thisisatest.<ahref=http://link2.com><imgsrc=http://img2.jpg/></a>Thisisatest.Thisisatest.Thisisatest.
".
"<ahref=http://link3.com><imgsrc=http://img3.jpg/></a>";
$regex='/<as+href=(.*)s*><imgs+src=(.*)s*/></a>/';
$output=array();
if(preg_match_all($regex,$str,$matches)!==false){
if(isset($matches[1])&&isset($matches[2])){
$links=$matches[1];
$imgs=$matches[2];
foreach($linksas$key=>$link){
$img=isset($imgs[$key])?$imgs[$key]:'';
$output[]="<ahref="{$link}"><imgsrc="{$img}"/></a>";
}
}
}
var_mp($output);
/TestDemo/Tpl/default/File/Uploads/2010_9_6/1283591048.jpg
根据你题目里面举例的这种对比的需求,转换条件认为是
“/Public/Files/全日期/数字.jpg”形式的图片转换成
“/File/Uploads/紧缩日期/数字.jpg”形式的图片。如果实际你的需求有所不同,可以根据这个略微对正则做修改。代码替换部分如下:
$str1 = "<p><img height=\"32\" alt=\"\" src=\"/TestDemo/Tpl/default/Public/Files/2010_09_04/1283591048.jpg\" width=\"32\" border=\"0\" />来留言看看这的风景好吗 <img alt=\"\" src=\"/TestDemo/Public/kindeditor/plugins/emoticons/13.gif\" border=\"0\" /></p><img height=\"32\" alt=\"\" src=\"/TestDemo/Tpl/default/Public/Files/2010_12_01/88668866.jpg\" width=\"32\" border=\"0\" />";
//这里多举一个图片例子试试
$str2 = preg_replace("/(src=\"[^\"]*?)\/Public\/Files(\/\d{4}_)[0]?(\d{1,2}_)[0]?(\d{1,2}\/\d+[.](?:jpg|gif|bmp)\")/","$1/File/Uploads$2$3$4$5",$str1);
?>
//规则有不同的话你可以这里对比修改下正则参数。good luck~
//----------------------------------------
既然需求少了那么多,就可以
$str2 = preg_replace("/(src=\"[^\"]*?)\/Public\/Files(\/\d{4}_\d{1,2}_\d{1,2}\/\d+[.](?:jpg|gif|bmp)\")/","$1/File/Uploads$2",$str1);
行了,如果对图片的格式限制不严格(严格点的就需要在尾巴那里罗列出来),可以再简化成
$str2 = preg_replace("/(src=\"[^\"]*?)\/Public\/Files(\/\d{4}_\d{1,2}_\d{1,2}\/\d+[.][a-zA-Z]+\")/","$1/File/Uploads$2",$str1);

php怎么实现相似图片的搜索呢?
其中的原理来解释下
1、缩小尺寸。将图片缩小到8×8的尺寸,总共64个像素。这一步的作用是去除图片的细节,只保留结构、明暗等基本信息,摒弃不同尺寸、比例带来的图片差异。
2、简化色彩。将缩小后的图片,转为64级灰度。也就是说,所有像素点总共只有64种颜色。
3、计算平均值。计算所有64个像素的灰度平均值。
4、比较像素的灰度。将每个像素的灰度,与平均值进行比较。大于或等于平均值,记为1;小于平均值,记为0。
5、计算哈希值。将上一步的比较结果,组合在一起,就构成了一个64位的整数,这就是这张图片的指纹。组合的次序并不重要,只要保证所有图片都采用同样次序就行了。得到指纹以后,就可以对比不同的图片,看看64位中有多少位是不一样的。
使用代码
hash($f);
}
return $isString ? $result[0] : $result;
}
public function checkIsSimilarImg($imgHash, $otherImgHash){
if (file_exists($imgHash) && file_exists($otherImgHash)){
$imgHash = $this->run($imgHash);
$otherImgHash = $this->run($otherImgHash);
}
if (strlen($imgHash) !== strlen($otherImgHash)) return false;
$count = 0;
$len = strlen($imgHash);
for($i=0;$i<$len;$i++){
if ($imgHash{$i} !== $otherImgHash{$i}){
$count++;
}
}
return $count <= (5 * $rate * $rate) ? true : false;
}
public function hash($file){
if (!file_exists($file)){
return false;
}
$height = 8 * $this->rate;
$width = 8 * $this->rate;
$img = imagecreatetruecolor($width, $height);
list($w, $h) = getimagesize($file);
$source = $this->createImg($file);
imageresampled($img, $source, 0, 0, 0, 0, $width, $height, $w, $h);
$value = $this->getHashValue($img);
imagedestroy($img);
return $value;
}
public function getHashValue($img){
$width = imagesx($img);
$height = imagesy($img);
$total = 0;
$array = array();
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
$gray = ( imagecolorat($img, $x, $y) >> 8 ) & 0xFF;
if (!is_array($array[$y])){
$array[$y] = array();
}
$array[$y][$x] = $gray;
$total += $gray;
}
}
$average = intval($total / (64 * $this->rate * $this->rate));
$result = '';
for ($y=0;$y<$height;$y++){
for ($x=0;$x<$width;$x++){
if ($array[$y][$x] >= $average){
$result .= '1';
}else{
$result .= '0';
}
}
}
return $result;
}
public function createImg($file){
$ext = $this->getFileExt($file);
if ($ext === 'jpeg') $ext = 'jpg';
$img = null;
switch ($ext){
case 'png' : $img = imagecreatefrompng($file);break;
case 'jpg' : $img = imagecreatefromjpeg($file);break;
case 'gif' : $img = imagecreatefromgif($file);
}
return $img;
}
public function getFileExt($file){
$infos = explode('.', $file);
$ext = strtolower($infos[count($infos) - 1]);
return $ext;
}
}
调用方式如下:
require_once "Imghash.class.php";
$instance = ImgHash::getInstance();
$result = $instance->checkIsSimilarImg('chenyin/IMG_3214.png', 'chenyin/IMG_3212.JPG');
如果$result值为true, 则表明2个图片相似,否则不相似。
一:写个函数,这个函数的主要功能是找出一张图片上的主要颜色(代码在最后面)
二:把一张图片有GD库分成9份(3*3)或更多份,然后用第一个函数来取得每一份小图的主要颜色。
三:比对这几份小图的颜色,看哪一份的颜色是你需要的。
如果你原意,你也可以切很多份,比如10000份(100*100),然后计算这10000个颜色的中你需要的颜色的分布,找出你需要的区域。
$i = imagecreatefromjpeg("image.jpg");
for ($x=0;$x<imagesx($i);$x++) {
for ($y=0;$y<imagesy($i);$y++) {
$rgb = imagecolorat($i,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> & 0xFF;
$b = $rgb & 0xFF;
$rTotal += $r;
$gTotal += $g;
$bTotal += $b;
$total++;
}
}
$rAverage = round($rTotal/$total);
$gAverage = round($gTotal/$total);
$bAverage = round($bTotal/$total);
