Sponsorenverwaltung - Team StarCraft e.V.
 All Data Structures Files Functions Variables
captcha.php
Go to the documentation of this file.
1 <?php
2  /**
3  * @file captcha.php
4  *
5  * @brief Creates a captcha image.
6  *
7  * @details
8  * Creates a captcha image. The color of the text displayed in the captcha
9  * can be set with the GET-variable color= followed by a 6 characters long
10  * hexadeximal description of the desired color.
11  *
12  * This file is based on captcha.php from UserCake (Version 2.0.2)
13  * with modifications by Daniel Seichter
14  *
15  * This file has no dependencies.
16  *
17  * @copyright 2013, Team StarCraft e.V.
18  * @version 1.0.0
19  * @author Usercake (http://usercake.com)
20  * @author Daniel Seichter
21  * @date 02.07.2013
22  */
23 
24 
25  /**
26  * Converts a hex color string to an array with decimal RGB values
27  * @param mixed $hex hex color string
28  * @return array array containing the RGB values
29  * @author Daniel Seichter
30  */
31  function hex2rgb($hex) {
32  // replace # with ''
33  $hex = str_replace("#", "", $hex);
34  // convert the three hex to decimal values
35  $r = hexdec(substr($hex,0,2));
36  $g = hexdec(substr($hex,2,2));
37  $b = hexdec(substr($hex,4,2));
38  return array($r, $g, $b);
39  }
40 
41  // start session to bin captcha code
42  session_start();
43 
44  // set some header information
45  header("Content-Type: image/png");
46 
47  // configuration
48  //! holds default font size
49  $fontSize = 12;
50 
51  //! holds default font name
52  $fontName = 'font/Verdana_Italic.ttf';
53 
54  //! holds default font color
55  $fontColor = array(255, 255, 255);
56 
57  //! holds default height
58  $height = 35;
59 
60  //! holds default width
61  $width = 75;
62 
63 
64 
65  /// @cond MAINPART
66  // generate secure code
67  $md5_hash = md5(rand(0,99999));
68  $security_code = substr($md5_hash, 25, 6);
69  $enc = md5($security_code);
70 
71  // bind to current session
72  $_SESSION['captcha'] = $enc;
73 
74  // draw secure code with transparancy
75  $image = imageCreateTrueColor($width, $height);
76  $black = imageColorAllocate($image, 0, 0, 0);
77 
78  // get user specified color
79  if (!empty($_GET['color']) && ((strlen($_GET['color']) == 6) || (strlen($_GET['color']) == 7))) {
80  $rgb = hex2rgb($_GET['color']);
81  $fontColor = imageColorAllocate($image, $rgb[0], $rgb[1], $rgb[2]);
82  }
83  else {
84  $fontColor = imageColorAllocate($image, $fontColor[0], $fontColor[1], $fontColor[2]);
85  }
86  imageFill($image, 0, 0, $black);
87  imageColorTransparent($image, $black);
88  imagettftext($image, $fontSize, 10, 9, 28, $fontColor, $fontName, $security_code);
89 
90  // image output and free image object
91  ImagePng($image);
92  ImageDestroy($image);
93 
94  /// @endcond
95 ?>