2016-12-06 113 views
0

我想將上傳的圖片與數據庫中的所有圖片進行比較,如果匹配,則應顯示匹配圖片的詳細信息。使用PHP比較兩張圖片

我試過這個,但不明白,任何人都可以幫助不同的?

<?php 
class compareImages 
{ 
    private function mimeType($i) 
    { 
     /*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/ 
     $mime = getimagesize($i); 
     $return = array($mime[0],$mime[1]); 

     switch ($mime['mime']) 
     { 
      case 'image/jpeg': 
       $return[] = 'jpg'; 
       return $return; 
      case 'image/png': 
       $return[] = 'png'; 
       return $return; 
      default: 
       return false; 
     } 
    } 

    private function createImage($i) 
    { 
     /*retuns image resource or false if its not jpg or png*/ 
     $mime = $this->mimeType($i); 

     if($mime[2] == 'jpg') 
     { 
      return imagecreatefromjpeg ($i); 
     } 
     else if ($mime[2] == 'png') 
     { 
      return imagecreatefrompng ($i); 
     } 
     else 
     { 
      return false; 
     } 
    } 

    private function resizeImage($i,$source) 
    { 
     /*resizes the image to a 8x8 squere and returns as image resource*/ 
     $mime = $this->mimeType($source); 

     $t = imagecreatetruecolor(8, 8); 

     $source = $this->createImage($source); 

     imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]); 

     return $t; 
    } 

    private function colorMeanValue($i) 
    { 
     /*returns the mean value of the colors and the list of all pixel's colors*/ 
     $colorList = array(); 
     $colorSum = 0; 
     for($a = 0;$a<8;$a++) 
     { 

      for($b = 0;$b<8;$b++) 
      { 

       $rgb = imagecolorat($i, $a, $b); 
       $colorList[] = $rgb & 0xFF; 
       $colorSum += $rgb & 0xFF; 

      } 

     } 

     return array($colorSum/64,$colorList); 
    } 

    private function bits($colorMean) 
    { 
     /*returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1*/ 
     $bits = array(); 

     foreach($colorMean[1] as $color){$bits[]= ($color>=$colorMean[0])?1:0;} 

     return $bits; 

    } 

    public function compare($a,$b) 
    { 
     /*main function. returns the hammering distance of two images' bit value*/ 
     $i1 = $this->createImage($a); 
     $i2 = $this->createImage($b); 

     if(!$i1 || !$i2){return false;} 

     $i1 = $this->resizeImage($i1,$a); 
     $i2 = $this->resizeImage($i2,$b); 

     imagefilter($i1, IMG_FILTER_GRAYSCALE); 
     imagefilter($i2, IMG_FILTER_GRAYSCALE); 

     $colorMean1 = $this->colorMeanValue($i1); 
     $colorMean2 = $this->colorMeanValue($i2); 

     $bits1 = $this->bits($colorMean1); 
     $bits2 = $this->bits($colorMean2); 

     $hammeringDistance = 0; 

     for($a = 0;$a<64;$a++) 
     { 

      if($bits1[$a] != $bits2[$a]) 
      { 
       $hammeringDistance++; 
      } 

     } 

     return $hammeringDistance; 
    } 
} 
?> 
+1

快捷的方式來比較,檢查兩個文件是相同的,可以使用文件的md5散列並對它們進行比較。不過,我不確定這是否是最好的方法。 – 2016-12-06 11:13:05

+0

您可以使用php ** ImageMagick **(圖像處理)庫並使用它的** Imagick :: compareImages **函數來執行此操作。鏈接:http://sg2.php.net/manual/en/imagick.compareimages.php –

+0

http://stackoverflow.com/a/13758760/4248328然後比較 –

回答

0

其實你可以使用MD5在以前的評論中的文件作爲提的,也可以將圖像轉換成字符串的base64和的base64字符串之間進行比較。 base64string將返回相同的模式,如果你有兩個相同的圖像

好運

0

試試這個:也許有幫助

<?php 
$sourceImagepath = encodeImage("folder/source.png"); 
$uploadedImagepath = encodeImage("temp/uploded.png"); 

if(areEqual($sourceImagepath , $uploadedImagepath)){ 
    echo "Image is already Exist"; 
    return false; 
} 
echo "Image Upload"; 


function encodeImage($ImagePath){ 
    $ext = pathinfo($ImagePath, PATHINFO_EXTENSION); 
    $Imgcontent = file_get_contents($ImagePath); 
    $base64 = 'data:image/' . $ext . ';base64,' . base64_encode($Imgcontent); 
    return $base64; 
} 

function areEqual($sourceImage, $uploadedImage){  
    if (strcmp($sourceImage, $uploadedImage) !== 0) { 
      return false; 
    } 
    return true; 
} 
?> 

OR

<?php 

    function areEqual($firstPath, $secondPath, $chunkSize = 500){ 

     // First check if file are not the same size as the fastest method 
     if(filesize($firstPath) !== filesize($secondPath)){ 
      return false; 
     } 

     // Compare the first ${chunkSize} bytes 
     // This is fast and binary files will most likely be different 
     $fp1 = fopen($firstPath, 'r'); 
     $fp2 = fopen($secondPath, 'r'); 
     $chunksAreEqual = fread($fp1, $chunkSize) == fread($fp2, $chunkSize); 
     fclose($fp1); 
     fclose($fp2); 

     if(!$chunksAreEqual){ 
      return false; 
     } 

     // Compare hashes 
     // SHA1 calculates a bit faster than MD5 
     $firstChecksum = sha1_file($firstPath); 
     $secondChecksum = sha1_file($secondPath); 
     if($firstChecksum != $secondChecksum){ 
      return false; 
     } 

     return true; 
    } 
    ?> 
+0

感謝它的工作,但抱歉延伸懷疑....如果我想接受類似的圖像也像,相同的對象(在圖像中)與不同的顏色,旋轉,壓縮。 –

+0

如果它的工作比請標記答案並且贊成它,讓其他開發人員可以輕鬆找到解決方案。 –