Last active
March 1, 2016 21:32
-
-
Save divinity76/bc3573696f2ac8c09442 to your computer and use it in GitHub Desktop.
This file contains 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 characters
<?php | |
require_once('hhb_.inc.php'); | |
hhb_init(); | |
function cleanthinking() | |
{ | |
array_map('unlink', glob("THINKING/*testTHINK*")); | |
} | |
function cleanextracted() | |
{ | |
array_map('unlink', glob("extracted/*")); | |
} | |
cleanthinking(); | |
cleanextracted(); | |
$img1 = '3/1.gif'; | |
$img2 = '3/2.gif'; | |
$img1r = imagecreatefromgif($img1); | |
$img1size = getimagesize($img1); | |
$img1sizex = $img1size[0]; | |
$img1sizey = $img1size[1]; | |
$img2r = imagecreatefromgif($img2); | |
$img2size = getimagesize($img2); | |
$img2sizex = $img2size[0]; | |
$img2sizey = $img2size[1]; | |
//hhb_var_dump(findXnotwhite($img2r,$img2sizex,$img2sizey,0,0)); | |
//hhb_var_dump(findXwhiterow($img2r,$img2sizex,$img2sizey,15,21)); | |
//hhb_var_dump(extractImages($img2)); | |
//echo ('answer is probably:'.crack($img1,$img2)); | |
for($i=1;$i<10;++$i){ | |
cleanextracted(); | |
echo "cracking ".$i.".."; | |
echo "answer: ".crack($i.'/1.gif',$i.'/2.gif').PHP_EOL; | |
} | |
function compareImages($img1,$img2){ | |
assert(is_string($img1)); | |
assert(is_string($img2)); | |
$image1 = new imagick($img1); | |
$image2 = new imagick($img2); | |
unset($img1,$img2); | |
$result = $image1->compareImages($image2,imagick::METRIC_MEANSQUAREERROR); | |
return $result[1]; | |
} | |
function compareImages2($img1,$img2){ | |
assert(is_string($img1)); | |
assert(is_string($img2)); | |
require_once('phpdupeimage.php'); | |
$pdi=new phpDupeImage(); | |
} | |
function loadimage($file){ | |
if(pathinfo($file,PATHINFO_EXTENSION )==='png'){ | |
return imagecreatefrompng($file); | |
}elseif(pathinfo($file,PATHINFO_EXTENSION )==='gif'){ | |
return imagecreatefromgif($file); | |
} else { | |
throw new Exception('unknown file type: '.$file); | |
} | |
} | |
function difference($file1, $file2) { | |
// Sets the width and height of the thumbnail sized image we use for deep comparison. | |
$small_size = 1; | |
// Load in both images and resize them to 16x16 pixels | |
assert(is_string($file1)); | |
assert(is_string($file1)); | |
assert(file_exists($file1)); | |
assert(file_exists($file2)); | |
$image1_src = loadimage($file1); | |
$image2_src = loadimage($file2); | |
list($image1_width, $image1_height) = getimagesize($file1); | |
list($image2_width, $image2_height) = getimagesize($file2); | |
$image1_small = imagecreatetruecolor($small_size, $small_size); | |
$image2_small = imagecreatetruecolor($small_size, $small_size); | |
imagecopyresampled($image1_small, $image1_src, 0, 0, 0, 0, | |
$small_size, $small_size, $image1_width, $image1_height); | |
imagecopyresampled($image2_small, $image2_src, 0, 0, 0, 0, | |
$small_size, $small_size, $image2_width, $image2_height); | |
// Compare the pixels of each image and figure out the colour difference between them | |
$difference=0; | |
for ($x = 0; $x < $small_size; ++$x) { | |
for ($y = 0; $y < $small_size; ++$y) { | |
$image1_color = imagecolorsforindex($image1_small, | |
imagecolorat($image1_small, $x, $y)); | |
$image2_color = imagecolorsforindex($image2_small, | |
imagecolorat($image2_small, $x, $y)); | |
$difference += abs($image1_color['red'] - $image2_color['red']) + | |
abs($image1_color['green'] - $image2_color['green']) + | |
abs($image1_color['blue'] - $image2_color['blue']); | |
} | |
} | |
$difference = $difference / 256; | |
return $difference; | |
} | |
function extractImages($img,$extrastr='') | |
{ | |
assert(is_string($img)); | |
$imgr = imagecreatefromgif($img); | |
$size = getimagesize($img); | |
$imgsizex = $size[0]; | |
$imgsizey = $size[1]; | |
$extracts = array(); | |
$extractsr= array(); | |
$x = 0; | |
$extractscounter=0; | |
while ($x < $imgsizex) { | |
$newx = findXnotwhite($imgr, $imgsizex, $imgsizey, $x, 0); | |
if ($newx === false) { | |
//hhb_var_dump("reached the end! no more NONwhite pixels!"); | |
break; | |
} | |
$x=$newx['x']; | |
$tmpx=findXwhiterow($imgr,$imgsizex,$imgsizey,$x,0); | |
if($tmpx===false){ | |
hhb_var_dump("reached the end! no more whiterows!"); | |
break; | |
} | |
$tmpx=$tmpx['x']; | |
$diff=abs($x-$tmpx); | |
//hhb_var_dump($diff);die(); | |
$tmpimgr=imagecreate($diff,$imgsizey); | |
//hhb_var_dump($y,$tmpy);die(); | |
$shittylib=array(); | |
$shittylib['thinking']=imagecolorallocate($tmpimgr,153,0,255); | |
assert($shittylib['thinking']!==false); | |
assert($shittylib['thinking']!==NULL); | |
$x0=0; | |
$y0=0; | |
//hhb_var_dump($x,$tmpx);die(); | |
assert($x<$tmpx); | |
$pixels_copied=0; | |
$xstart=$x; | |
for($xi=$x;$xi<$tmpx;++$xi){// | |
++$x0; | |
$y0=0; | |
for($yi=0;$yi<$imgsizey;++$yi){ | |
++$y0; | |
$newpixel=imagecolorat($imgr,$xi,$yi); | |
$newpixelcolors=imagecolorsforindex($imgr,$newpixel); | |
if(array_key_exists('red'.$newpixelcolors['red'].'green'.$newpixelcolors['green'].'blue'.$newpixelcolors['blue'],$shittylib)){ | |
//nothing... | |
} else{ | |
$shittylib['red'.$newpixelcolors['red'].'green'.$newpixelcolors['green'].'blue'.$newpixelcolors['blue']]=imagecolorallocate($tmpimgr, | |
$newpixelcolors['red'],$newpixelcolors['green'],$newpixelcolors['blue']); | |
assert(false!=$shittylib['red'.$newpixelcolors['red'].'green'.$newpixelcolors['green'].'blue'.$newpixelcolors['blue']]); | |
} | |
$tmpcol=$shittylib['red'.$newpixelcolors['red'].'green'.$newpixelcolors['green'].'blue'.$newpixelcolors['blue']]; | |
//$tmpcol=imagecolorallocate($tmpimgr,$newpixelcolors['red'], | |
// $newpixelcolors['green'],$newpixelcolors['blue']); | |
assert(false!==$tmpcol); | |
assert(NULL!==$tmpcol); | |
if($tmpcol==0){ | |
die("REALLYWTF");; | |
} | |
//hhb_var_dump("copy pixel! ",$tmpcol); | |
$foo=imagesetpixel($tmpimgr,($x0-1),($y0-1),$tmpcol); | |
//$foo=imagesetpixel($tmpimgr,$x0,$y0,$shittylib['thinking']); | |
assert(false!==$foo); | |
++$pixels_copied; | |
} | |
} | |
++$extractscounter; | |
$newfilepath='extracted/extract'.$extractscounter.'_'.$extrastr.'_xstart'.$x.'_xend'.$tmpx.'.png'; | |
imagepng($tmpimgr,$newfilepath,3); | |
foreach($shittylib as $key=>$val){ | |
imagecolordeallocate($tmpimgr,$val);//do i still need to do this when im destroying the image? | |
} | |
imagedestroy($tmpimgr); | |
$extracts[]=$newfilepath; | |
//hhb_var_dump($shittylib);die(); | |
//hhb_var_dump($pixels_copied,'supposed to be:',$diff*$imgsizey);die(); | |
$x=$tmpx; | |
} | |
//hhb_var_dump($x,$imgsizex);die(); | |
//assert($x===$imgsizex); | |
//hhb_var_dump('reached the end! $x === $imgsizex'); | |
imagedestroy($imgr); | |
return $extracts; | |
} | |
function findXnotwhite($imgr, $xsize, $ysize, $startx = 0, $starty = 0) | |
{ | |
//hhb_var_dump($imgr,$xsize,$ysize,$startx,$starty);die(); | |
assert(is_resource($imgr)); | |
$thinki = 0; | |
$starty = 0; //ignored.. | |
for ($xi = $startx; $xi < $xsize; ++$xi) { | |
for ($yi = $starty; $yi < $ysize; ++$yi) { | |
$in = imagecolorat($imgr, $xi, $yi); | |
$colors = imagecolorsforindex($imgr, $in); | |
if ((255 === $colors['red'] && 255 === $colors['green'] && 255 === $colors['blue'])) { | |
//echo "white!"; | |
//imagesetpixel($imgr,$xi,$yi,123); | |
//++$thinki; | |
//imagepng($imgr,'THINKING/testTHINK'.$thinki.'.png',3); | |
continue; | |
} | |
//echo "NOT WHITE!"; | |
return array( | |
'x' => $xi, | |
'y' => $yi, | |
'colors' => $colors | |
); | |
imagesetpixel($imgr, $xi, $yi, 123); | |
} | |
} | |
//imagepng($imgr,'test.png',3); | |
return false; //no more non-white pixels. reached the end... | |
} | |
function findXwhiterow($imgr, $xsize, $ysize, $startx = 0, $starty = 0) | |
{ | |
//hhb_var_dump($imgr,$xsize,$ysize,$startx,$starty);die(); | |
assert(is_resource($imgr)); | |
$thinki = 0; | |
$starty = 0; //ignored... | |
$foundnonwhite = false; | |
for ($xi = $startx; $xi < $xsize; ++$xi) { | |
$foundnonwhite = false; | |
for ($yi = $starty; $yi < $ysize; ++$yi) { | |
$in = imagecolorat($imgr, $xi, $yi); | |
$colors = imagecolorsforindex($imgr, $in); | |
//imagesetpixel($imgr,$xi,$yi,123); | |
//++$thinki; | |
//imagepng($imgr,'THINKING/testTHINK'.$thinki.'.png',3); | |
if ((255 === $colors['red'] && 255 === $colors['green'] && 255 === $colors['blue'])) { | |
//echo "white!"; | |
//$foundnonwhite=false; | |
continue; | |
} | |
//echo "NOT WHITE!"; | |
$foundnonwhite = true; | |
break; | |
} | |
if ($foundnonwhite === false) { | |
return array( | |
'x' => $xi, | |
'y' => $yi, | |
'colors' => $colors | |
); | |
} | |
} | |
return false; | |
//imagepng($imgr,'test.png',3); | |
} | |
function crack($img1, $img2) | |
{ | |
assert(is_string($img1)); | |
assert(is_string($img2)); | |
$img1r = imagecreatefromgif($img1); | |
$img2r = imagecreatefromgif($img2); | |
assert(is_resource($img1r)); | |
assert(is_resource($img2r)); | |
imagedestroy($img1r); | |
imagedestroy($img2r); | |
$foo=extractImages($img1,'optimizedANSWER'); | |
assert(count($foo)===1); | |
$img1=$foo[0]; | |
unset($foo); | |
$best=""; | |
$bestf=(float)(PHP_INT_MAX);//is not really PHP_FLOAT_MAX but close enough for this code.. | |
$extractedImages=extractImages($img2); | |
$i=0; | |
foreach($extractedImages as $extractedImage){ | |
++$i; | |
$diff=difference($extractedImage,$img1); | |
$tmpf=$diff; | |
//var_dump($tmpf); | |
if($tmpf<$bestf){ | |
$bestf=$tmpf; | |
$best=array($extractedImage,$i); | |
} | |
} | |
return 'image '.$best[1].': '.$best[0]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment