forked from 4ducky/4ducky
mirror
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
// ini_set('memory_limit', '256M');
|
||||
|
||||
class imagediff
|
||||
{
|
||||
private $image1;
|
||||
private $image2;
|
||||
|
||||
function __construct($img1, $img2)
|
||||
{
|
||||
$this->image1['path'] = realpath($img1);
|
||||
$this->image2['path'] = realpath($img2);
|
||||
if($this->image1['path'] === false || $this->image2['path'] === false)
|
||||
{
|
||||
throw new Exception('Image "'.htmlspecialchars( $this->image1 ? $img2 : $img1 ).'" not found!');
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->image1['type'] = $this->imagetyte($this->image1['path']);
|
||||
$this->image2['type'] = $this->imagetyte($this->image2['path']);
|
||||
}
|
||||
}
|
||||
|
||||
private function imagetyte($imgname)
|
||||
{
|
||||
$file_info = pathinfo($imgname);
|
||||
if(!empty ($file_info['extension']))
|
||||
{
|
||||
$filetype = strtolower($file_info['extension']);
|
||||
$filetype = $filetype == 'jpg' ? 'jpeg' : $filetype;
|
||||
$func = 'imagecreatefrom' . $filetype;
|
||||
if(function_exists($func))
|
||||
{
|
||||
return $filetype;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('File type "'.htmlspecialchars( $filetype ).'" not supported!');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('File type not supported!');
|
||||
}
|
||||
}
|
||||
|
||||
private function imagehex($image)
|
||||
{
|
||||
$size = getimagesize($image['path']);
|
||||
|
||||
|
||||
|
||||
list( $srcWidth, $srcHeight, $type ) = getimagesize( $image['path'] );
|
||||
if( $type == IMAGETYPE_JPEG ) {
|
||||
$extension = 'jpeg';
|
||||
} else if( $type == IMAGETYPE_GIF ) {
|
||||
$extension = 'gif';
|
||||
} else if( $type == IMAGETYPE_PNG ) {
|
||||
$extension = 'png';
|
||||
}
|
||||
|
||||
$func = 'imagecreatefrom'.$extension;
|
||||
$imageres = $func($image['path']);
|
||||
$zone = imagecreate(20, 20);
|
||||
imagecopyresized($zone, $imageres, 0, 0, 0, 0, 20, 20, $size[0], $size[1]);
|
||||
$colormap = array();
|
||||
$average = 0;
|
||||
$result = array();
|
||||
for($x=0; $x<20; $x++)
|
||||
{
|
||||
for($y=0; $y<20; $y++)
|
||||
{
|
||||
$color = imagecolorat($zone, $x, $y);
|
||||
|
||||
$color = imagecolorsforindex($zone, $color);
|
||||
|
||||
$colormap[$x][$y]= 0.212671 * $color['red'] + 0.715160 * $color['green'] + 0.072169 * $color['blue'];
|
||||
$average += $colormap[$x][$y];
|
||||
}
|
||||
}
|
||||
$average /= 400;
|
||||
for($x=0; $x<20; $x++)
|
||||
{
|
||||
for($y=0; $y<20; $y++)
|
||||
{
|
||||
$result[]=($x<10?$x:chr($x+97)) . ($y<10?$y:chr($y+97)) . round(2*$colormap[$x][$y]/$average);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function diff()
|
||||
{
|
||||
$hex1 = $this->imagehex($this->image1) ;
|
||||
$hex2 = $this->imagehex($this->image2);
|
||||
$result=(count($hex1) + count($hex2)) - count(array_diff($hex2,$hex1))-400;
|
||||
return $result / ( ( count($hex1) + count($hex2) ) / 2 );
|
||||
}
|
||||
}
|
||||
|
||||
//$diff = new imagediff('lol1.png', 'lol.png');
|
||||
//print ($diff->diff() * 100 ).'%';
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user