PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/wigit/imagebuilder.php

https://bitbucket.org/gopchu/gopchu.org
PHP | 161 lines | 85 code | 22 blank | 54 comment | 10 complexity | 966376755e1845ff5cb7fb6d9300ed96 MD5 | raw file
  1<?php
  2/*
  3============================
  4QuickCaptcha 1.0 - A bot-thwarting text-in-image web tool.
  5Copyright (c) 2006 Web 1 Marketing, Inc.
  6
  7This program is free software; you can redistribute it and/or
  8modify it under the terms of the GNU General Public License
  9as published by the Free Software Foundation; either version 2
 10of the License, or (at your option) any later version.
 11
 12This program is distributed in the hope that it will be useful,
 13but WITHOUT ANY WARRANTY; without even the implied warranty of
 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15GNU General Public License for more details.
 16============================
 17See settings.php for common settings. You shouldn't need to change
 18anything in this file.
 19============================
 20*/
 21// This string contains allowable characters for the image.
 22// To reduce confusion, zero and the letter 'o' have been removed,
 23// and QuickCaptcha is NOT case-sensitive.
 24$acceptedChars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
 25
 26// Number of characters in image.
 27$stringlength = 5;
 28
 29// A value between 0 and 100 describing how much color overlap
 30// there is between text and other objects.  Lower is more
 31// secure against bots, but also harder to read.
 32$contrast = 60;
 33
 34// Various obfuscation techniques.
 35$num_polygons = 3; // Number of triangles to draw.  0 = none
 36$num_ellipses = 6;  // Number of ellipses to draw.  0 = none
 37$num_lines = 0;  // Number of lines to draw.  0 = none
 38$num_dots = 0;  // Number of dots to draw.  0 = none
 39
 40$min_thickness = 2;  // Minimum thickness in pixels of lines
 41$max_thickness = 8;  // Maximum thickness in pixles of lines
 42$min_radius = 5;  // Minimum radius in pixels of ellipses
 43$max_radius = 15;  // Maximum radius in pixels of ellipses
 44
 45// How opaque should the obscuring objects be. 0 is opaque, 127
 46// is transparent.
 47$object_alpha = 75;
 48
 49// Keep #'s reasonable.
 50$min_thickness = max(1,$min_thickness);
 51$max_thickness = min(20,$max_thickness);
 52// Make radii into height/width
 53$min_radius *= 2;
 54$max_radius *= 2;
 55// Renormalize contrast
 56$contrast = 255 * ($contrast / 100.0);
 57$o_contrast = 1.3 * $contrast;
 58
 59$width = 15 * imagefontwidth (5);
 60$height = 2.5 * imagefontheight (5);
 61$image = imagecreatetruecolor ($width, $height);
 62imagealphablending($image, true);
 63$black = imagecolorallocatealpha($image,0,0,0,0);
 64
 65// Build the  validation string
 66$max = strlen($acceptedChars)-1;
 67$password = NULL;
 68for($i=0; $i < $stringlength; $i++) {
 69	$cnum[$i] = $acceptedChars{mt_rand(0, $max)};
 70	$password .= $cnum[$i];
 71}
 72
 73// Add string to image
 74$rotated = imagecreatetruecolor (70, 70);
 75$x = 0;
 76for ($i = 0; $i < $stringlength; $i++) {
 77	$buffer = imagecreatetruecolor (20, 20);
 78	$buffer2 = imagecreatetruecolor (40, 40);
 79
 80	// Get a random color
 81	$red = mt_rand(0,255);
 82	$green = mt_rand(0,255);
 83	$blue = 255 - sqrt($red * $red + $green * $green);
 84	$color = imagecolorallocate ($buffer, $red, $green, $blue);
 85
 86	// Create character
 87	imagestring($buffer, 5, 0, 0, $cnum[$i], $color);
 88
 89	// Resize character
 90	imagecopyresized ($buffer2, $buffer, 0, 0, 0, 0, 25 + mt_rand(0,12), 25 + mt_rand(0,12), 20, 20);
 91/*
 92	// Rotate characters a little
 93	$rotated = imagerotate($buffer2, mt_rand(-25, 25),imagecolorallocatealpha($buffer2,0,0,0,0));
 94	imagecolortransparent ($rotated, imagecolorallocatealpha($rotated,0,0,0,0));
 95
 96	// Move characters around a little
 97	$y = mt_rand(1, 3);
 98	$x += mt_rand(2, 6);
 99	imagecopymerge ($image, $rotated, $x, $y, 0, 0, 40, 40, 100);
100	$x += 22;
101*/
102	imagecolortransparent ($buffer2, imagecolorallocatealpha($buffer2,0,0,0,0));
103
104	// Move characters around a little
105	$y = mt_rand(1, 3);
106	$x += mt_rand(2, 6);
107	imagecopymerge ($image, $buffer2, $x, $y, 0, 0, 40, 40, 100);
108	$x += 22;
109
110	imagedestroy ($buffer);
111	imagedestroy ($buffer2);
112}
113
114// Draw polygons
115if ($num_polygons > 0) for ($i = 0; $i < $num_polygons; $i++) {
116	$vertices = array (
117		mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25),
118		mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25),
119		mt_rand(-0.25*$width,$width*1.25),mt_rand(-0.25*$width,$width*1.25)
120	);
121	$color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
122	imagefilledpolygon($image, $vertices, 3, $color);
123}
124
125// Draw random circles
126if ($num_ellipses > 0) for ($i = 0; $i < $num_ellipses; $i++) {
127	$x1 = mt_rand(0,$width);
128	$y1 = mt_rand(0,$height);
129	$color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
130//	$color = imagecolorallocate($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast));
131	imagefilledellipse($image, $x1, $y1, mt_rand($min_radius,$max_radius), mt_rand($min_radius,$max_radius), $color);
132}
133
134// Draw random lines
135if ($num_lines > 0) for ($i = 0; $i < $num_lines; $i++) {
136	$x1 = mt_rand(-$width*0.25,$width*1.25);
137	$y1 = mt_rand(-$height*0.25,$height*1.25);
138	$x2 = mt_rand(-$width*0.25,$width*1.25);
139	$y2 = mt_rand(-$height*0.25,$height*1.25);
140	$color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), $object_alpha);
141	imagesetthickness ($image, mt_rand($min_thickness,$max_thickness));
142	imageline($image, $x1, $y1, $x2, $y2 , $color);
143}
144
145// Draw random dots
146if ($num_dots > 0) for ($i = 0; $i < $num_dots; $i++) {
147	$x1 = mt_rand(0,$width);
148	$y1 = mt_rand(0,$height);
149	$color = imagecolorallocatealpha ($image, mt_rand(0,$o_contrast), mt_rand(0,$o_contrast), mt_rand(0,$o_contrast),$object_alpha);
150	imagesetpixel($image, $x1, $y1, $color);
151}
152
153session_start();
154$_SESSION['capstring'] = $password;
155
156header('Content-type: image/png');
157imagepng($image);
158imagedestroy($image);
159
160?>
161