Created
December 31, 2020 06:07
Revisions
-
muthu32 created this gist
Dec 31, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,107 @@ <?php /** * Image Comparing Function (C)2011 Robert Lerner, All Rights Reserved * $image1 STRING/RESOURCE Filepath and name to PNG or passed image resource handle * $image2 STRING/RESOURCE Filepath and name to PNG or passed image resource handle * $RTolerance INTEGER (0-/+255) Red Integer Color Deviation before channel flag thrown * $GTolerance INTEGER (0-/+255) Green Integer Color Deviation before channel flag thrown * $BTolerance INTEGER (0-/+255) Blue Integer Color Deviation before channel flag thrown * $WarningTolerance INTEGER (0-100) Percentage of channel differences before warning returned * $ErrorTolerance INTEGER (0-100) Percentage of channel difference before error returned */ function imageCompare($image1, $image2, $RTolerance=0, $GTolerance=0, $BTolerance=0, $WarningTolerance=1, $ErrorTolerance=5) { if (is_resource($image1)) $im = $image1; else if (!$im = imagecreatefrompng($image1)) trigger_error("Image 1 could not be opened",E_USER_ERROR); if (is_resource($image2)) $im2 = $image2; else if (!$im2 = imagecreatefrompng($image2)) trigger_error("Image 2 could not be opened",E_USER_ERROR); $OutOfSpec = 0; if (imagesx($im)!=imagesx($im2)) die("Width does not match."); if (imagesy($im)!=imagesy($im2)) die("Height does not match."); //By columns for ($width=0;$width<=imagesx($im)-1;$width++) { for ($height=0;$height<=imagesy($im)-1;$height++) { $rgb = imagecolorat($im, $width, $height); $r1 = ($rgb >> 16) & 0xFF; $g1 = ($rgb >> 8) & 0xFF; $b1 = $rgb & 0xFF; $rgb = imagecolorat($im2, $width, $height); $r2 = ($rgb >> 16) & 0xFF; $g2 = ($rgb >> 8) & 0xFF; $b2 = $rgb & 0xFF; if (!($r1>=$r2-$RTolerance && $r1<=$r2+$RTolerance)) $OutOfSpec++; if (!($g1>=$g2-$GTolerance && $g1<=$g2+$GTolerance)) $OutOfSpec++; if (!($b1>=$b2-$BTolerance && $b1<=$b2+$BTolerance)) $OutOfSpec++; } } $TotalPixelsWithColors = (imagesx($im)*imagesy($im))*3; $RET['PixelsByColors'] = $TotalPixelsWithColors; $RET['PixelsOutOfSpec'] = $OutOfSpec; if ($OutOfSpec!=0 && $TotalPixelsWithColors!=0) { $PercentOut = ($OutOfSpec/$TotalPixelsWithColors)*100; $RET['PercentDifference']=$PercentOut; if ($PercentOut>=$WarningTolerance) //difference triggers WARNINGTOLERANCE% $RET['WarningLevel']=TRUE; if ($PercentOut>=$ErrorTolerance) //difference triggers ERRORTOLERANCE% $RET['ErrorLevel']=TRUE; } RETURN $RET; } Use: $diff = imageCompare("c:/building.png","c:/building2.png",5,5,5,1,5); echo "<pre>"; print_r($diff); Using these images: Building 1 Building 2 Returns: Array ( [PixelsByColors] => 19608 [PixelsOutOfSpec] => 224 [PercentDifference] => 1.1423908608731 [WarningLevel] => 1 ) Interpretation of Results: PixelsByColors = Pixel Count * 3 (for R, G, and B) PixelsOutOfSpec = (If a pixel varies outside of xTolerance, for each red, green, and blue. Where x = R/G/B) If any channel exceeds the threshhold, this number is incremented. PercentDifference = PixelsOutOfSpec/PixelsByColors*100 WarningLevel = TRUE (or 1) if level exceeds prescribed level ErrorLevel = TRUE (or 1) if level exceeds prescribed level