[PHP] reCaptcha Image Merging

    • Release

      [PHP] reCaptcha Image Merging

      Mit ein bisschen Raffinesse lassen sich die reCaptcha Bilder in eigene Bilder hineinkopieren - ohne den weißen Hintergrund.

      Benötigt wird ein Webserver mit PHP und aktiver GD Erweiterung (Ist zu 99% immer aktiviert)

      so könnte es zb aussehen:

      Folgenden Code kopieren, die 2 Funktionen ggf in andere Dateien auslagern, Keys eintragen und Rest ebenfalls nach Wunsch anpassen

      PHP-Quellcode

      1. <?php
      2. ///////////////////////////////////////
      3. // additional recaptcha functions
      4. function recaptcha_load_challange($publicKey)
      5. {
      6. $recaptchaStateJS = @file_get_contents("http://www.google.com/recaptcha/api/challenge?k=$publicKey");
      7. if (!$recaptchaStateJS) die("recaptcha_load_challange: cannot load file");
      8. if (!preg_match("/challenge\s*?\:\s*?\'([^\']+)\'/", $recaptchaStateJS, $recaptchaChallange)) die("recaptcha_load_challange: cannot find field");
      9. return $recaptchaChallange[1];
      10. }
      11. function recaptcha_merge_images($challange, $targetImage, $targetStartX, $targetStartY)
      12. {
      13. if (!is_string($challange)) die("recaptcha_merge_images: challange is no string");
      14. if (!is_resource($targetImage)) die("recaptcha_merge_images: targetImage is no resource");
      15. if (!is_numeric($targetStartX)) die("recaptcha_merge_images: targetStartX is no number");
      16. if (!is_numeric($targetStartY)) die("recaptcha_merge_images: targetStartY is no number");
      17. $targetStartX = (int)round($targetStartX);
      18. $targetStartY = (int)round($targetStartY);
      19. $recaptchaImage = @file_get_contents("http://www.google.com/recaptcha/api/image?c=$challange");
      20. if (!$recaptchaImage) die("recaptcha_merge_images: cannot load image");
      21. $recaptchaImage = imagecreatefromstring($recaptchaImage);
      22. if (!$recaptchaImage) die("recaptcha_merge_images: cannot read image");
      23. $recaptchaWidth = imagesx($recaptchaImage);
      24. $recaptchaHeight = imagesy($recaptchaImage);
      25. $targetWidth = imagesx($targetImage);
      26. $targetHeight = imagesy($targetImage);
      27. $targetWidth = ($recaptchaWidth+$targetStartX >= $targetWidth) ? $targetWidth-$targetStartX : $recaptchaWidth;
      28. $targetHeight = ($recaptchaHeight+$targetStartY >= $targetHeight) ? $targetHeight-$targetStartY : $recaptchaHeight;
      29. $recaptchaStartX = ($targetStartX < 0) ? $targetStartX*-1 : 0;
      30. $recaptchaStartY = ($targetStartY < 0) ? $targetStartY*-1 : 0;
      31. if ($targetWidth <= 0 || $targetHeight <= 0) die("err 5");
      32. function rgba2int($r, $g, $b, $a=0)
      33. {
      34. return ($a << 24) + ($r << 16) + ($g << 8) + $b;
      35. }
      36. function int2rgba($int)
      37. {
      38. return array(
      39. 3 => ($int >> 24) & 0xFF,
      40. 0 => ($int >> 16) & 0xFF,
      41. 1 => ($int >> 8) & 0xFF,
      42. 2 => $int & 0xFF
      43. );
      44. $a = ($int >> 24) & 0xFF;
      45. $r = ($int >> 16) & 0xFF;
      46. $g = ($int >> 8) & 0xFF;
      47. $b = $int & 0xFF;
      48. return array($r, $g, $b, $a);
      49. }
      50. function distance($r, $g, $b)
      51. {
      52. return sqrt($r*$r + $g*$g + $b*$b);
      53. }
      54. $maxDistance = 441.6729559300637098;
      55. for ($x=$recaptchaStartX; $x<$targetWidth; $x++)
      56. for ($y=$recaptchaStartY; $y<$targetHeight; $y++)
      57. {
      58. $targetColor = int2rgba(imagecolorat($targetImage, $x+$targetStartX, $y+$targetStartY));
      59. $recaptchaColor = int2rgba(imagecolorat($recaptchaImage, $x, $y));
      60. $percent = 1 / $maxDistance * distance($recaptchaColor[0], $recaptchaColor[1], $recaptchaColor[2]);
      61. imagesetpixel(
      62. $targetImage,
      63. $x+$targetStartX,
      64. $y+$targetStartY,
      65. rgba2int(
      66. round(min(255, $recaptchaColor[0]*(1-$percent) + $targetColor[0]*$percent)),
      67. round(min(255, $recaptchaColor[1]*(1-$percent) + $targetColor[1]*$percent)),
      68. round(min(255, $recaptchaColor[2]*(1-$percent) + $targetColor[2]*$percent))
      69. )
      70. );
      71. }
      72. imagedestroy($recaptchaImage);
      73. return $targetImage;
      74. }
      75. ///////////////////////////////////////
      76. $recaptchaPublic = "oOoOo0o0ooOoooO0OoOOoOO0OO0o0oO0O0Ooo";
      77. $recaptchaPrivate = "li1II1II1IlIII1iIililli1i11ilIi1";
      78. if (isset($_GET["go"]))
      79. switch($_GET["go"])
      80. {
      81. case "image":
      82. if (!isset($_GET["challange"])) die("...");
      83. $image = recaptcha_merge_images(
      84. $_GET["challange"],
      85. imagecreatefrompng("preview.png"),
      86. 50, // start x
      87. 50 // start y
      88. );
      89. // keep alpha
      90. imagealphablending($image, true);
      91. imagesavealpha($image, true);
      92. // output
      93. header('Content-Type: image/png');
      94. imagepng($image);
      95. imagedestroy($image);
      96. // and exit
      97. exit();
      98. break;
      99. case "verify":
      100. if (!isset($_POST["challange"])) die("...");
      101. if (!isset($_POST["answer"])) die("...");
      102. require_once('recaptchalib.php');
      103. $resp = recaptcha_check_answer(
      104. $recaptchaPrivate,
      105. $_SERVER["REMOTE_ADDR"],
      106. $_POST["challange"],
      107. $_POST["answer"]
      108. );
      109. if (!$resp->is_valid) {
      110. die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
      111. "(reCAPTCHA said: " . $resp->error . ")");
      112. } else {
      113. echo "answer was correct";
      114. // Your code here to handle a successful verification
      115. }
      116. exit();
      117. break;
      118. }
      119. $challange = recaptcha_load_challange($recaptchaPublic);
      120. ?>
      121. <html>
      122. <body>
      123. <form method="post" action="?go=verify">
      124. <img src="?go=image&challange=<?php echo $challange; ?>">
      125. <input type="hidden" name="challange" value="<?php echo $challange; ?>" />
      126. <input type="text" name="answer" />
      127. <input type="submit" />
      128. </form>
      129. </body>
      130. </html>


      Weiß nich unter welchen Bedingungen ich dies veröffentlichen darf, daher gibs das ma als Example-SourceCode und nich in meiner WebLib =)