Skip to content

Instantly share code, notes, and snippets.

@gray
Last active August 29, 2015 13:55
Show Gist options
  • Save gray/8787057 to your computer and use it in GitHub Desktop.
Save gray/8787057 to your computer and use it in GitHub Desktop.
Obscure text by converting to a captcha-like image
<?php
$text = isset($_GET['t']) ? $_GET['t'] : '0.0000005';
$width = 140;
$height = 32;
$img = imagecreate(140, 32);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$text_color = imagecolorallocate(
$img, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)
);
$size = mt_rand(19, 20);
$angle = mt_rand(-2, 2);
$x = 2;
$y = $height - 9;
$font = './fonts/Lato-Reg.ttf';
imagettftext($img, $size, $angle, $x, $y, $text_color, $font, $text);
// The image on the external site allocates an extra color for anti-aliasing.
// Manually allocate it here to make the palette size match.
$unknown_color = imagecolorallocate(
$img, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)
);
# Create 5 random lines.
for ($i = 1; $i <= 5; $i++) {
$color = imagecolorallocate(
$img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)
);
$x1 = mt_rand(0, $width); $y1 = mt_rand(0, $height);
$x2 = mt_rand(0, $width); $y2 = mt_rand(0, $height);
imagesetthickness($img, 2);
imageline($img, $x1, $y1, $x2, $y2, $color);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment