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);