Projet

Général

Profil

Paste
Télécharger (12,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / captcha / image_captcha / image_captcha.user.inc @ a84ddea4

1
<?php
2

    
3
/**
4
 * @file
5
 * Functions for the generation of the CAPTCHA image.
6
 *
7
 * Loosely Based on MyCaptcha by Heine Deelstra
8
 * (http://heine.familiedeelstra.com/mycaptcha-download)
9
 */
10

    
11
/**
12
 * Menu callback function that generates the CAPTCHA image.
13
 */
14
function image_captcha_image() {
15
  // If output buffering is on: discard current content and disable further buffering
16
  if (ob_get_level()) {
17
    ob_end_clean();
18
  }
19

    
20
  if (!isset($_GET['sid'])) {
21
    exit();
22
  }
23
  $captcha_sid = $_GET['sid'];
24

    
25
  // Get solution (the code to show).
26
  $code = db_query("SELECT solution FROM {captcha_sessions} WHERE csid = :csid",
27
    array(':csid' => $captcha_sid)
28
  )->fetchField();
29

    
30
  // Only generate captcha if code exists in the session.
31
  if ($code !== FALSE) {
32
    // generate the image
33
    $image = @_image_captcha_generate_image($code);
34
    // check of generation was successful
35
    if (!$image) {
36
      watchdog('CAPTCHA', 'Generation of image CAPTCHA failed. Check your image CAPTCHA configuration and especially the used font.', array(), WATCHDOG_ERROR);
37
      exit();
38
    }
39
    // Send the image resource as an image file to the client.
40
    $file_format = variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG);
41
    if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_JPG) {
42
      drupal_add_http_header('Content-Type', 'image/jpeg');
43
      imagejpeg($image);
44
    }
45
    else {
46
      drupal_add_http_header('Content-Type', 'image/png');
47
      imagepng($image);
48
    }
49
    // Clean up the image resource.
50
    imagedestroy($image);
51
  }
52
  exit();
53
}
54

    
55

    
56
/**
57
 * Small helper function for parsing a hexadecimal color to a RGB tuple.
58
 */
59
function _image_captcha_hex_to_rgb($hex) {
60
  // handle #RGB format
61
  if (strlen($hex) == 4) {
62
    $hex = $hex[1] . $hex[1] . $hex[2] . $hex[2] . $hex[3] . $hex[3];
63
  }
64
  $c = hexdec($hex);
65
  $rgb = array();
66
  for ($i = 16; $i >= 0; $i -= 8) {
67
    $rgb[] = ($c >> $i) & 0xFF;
68
  }
69
  return $rgb;
70
}
71

    
72

    
73
/**
74
 * Base function for generating a image CAPTCHA.
75
 */
76
function _image_captcha_generate_image($code) {
77
  // Get font.
78
  $fonts = _image_captcha_get_enabled_fonts();
79

    
80
  // get other settings
81
  $font_size = (int) variable_get('image_captcha_font_size', 30);
82
  list($width, $height) = _image_captcha_image_size($code);
83

    
84
  // create image resource
85
  $image = imagecreatetruecolor($width, $height);
86
  if (!$image) {
87
    return FALSE;
88
  }
89

    
90
  // Get the background color and paint the background.
91
  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
92
  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
93
  // Set transparency if needed.
94
  $file_format = variable_get('image_captcha_file_format', IMAGE_CAPTCHA_FILE_FORMAT_JPG);
95
  if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
96
    imagecolortransparent($image, $background_color);
97
  }
98
  imagefilledrectangle($image, 0, 0, $width, $height, $background_color);
99

    
100
  // Do we need to draw in RTL mode?
101
  global $language;
102
  $rtl = $language->direction && ((bool) variable_get('image_captcha_rtl_support', 0));
103

    
104
  // draw text
105
  $result = _image_captcha_image_generator_print_string($image, $width, $height, $fonts, $font_size, $code, $rtl);
106
  if (!$result) {
107
    return FALSE;
108
  }
109

    
110
  // add noise
111
  $noise_colors = array();
112
  for ($i = 0; $i < 20; $i++) {
113
    $noise_colors[] = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
114
  }
115
  // Add additional noise.
116
  if (variable_get('image_captcha_dot_noise', 0)) {
117
    _image_captcha_image_generator_add_dots($image, $width, $height, $noise_colors);
118
  }
119
  if (variable_get('image_captcha_line_noise', 0)) {
120
    _image_captcha_image_generator_add_lines($image, $width, $height, $noise_colors);
121
  }
122

    
123
  // Distort the image.
124
  $distortion_amplitude = .25 * $font_size * variable_get('image_captcha_distortion_amplitude', 0) / 10.0;
125
  if ($distortion_amplitude > 1) {
126
    // distortion parameters
127
    $wavelength_xr = (2+3*lcg_value())*$font_size;
128
    $wavelength_yr = (2+3*lcg_value())*$font_size;
129
    $freq_xr = 2 * 3.141592 / $wavelength_xr;
130
    $freq_yr = 2 * 3.141592 / $wavelength_yr;
131
    $wavelength_xt = (2+3*lcg_value())*$font_size;
132
    $wavelength_yt = (2+3*lcg_value())*$font_size;
133
    $freq_xt = 2 * 3.141592 / $wavelength_xt;
134
    $freq_yt = 2 * 3.141592 / $wavelength_yt;
135

    
136
    $distorted_image = imagecreatetruecolor($width, $height);
137
    if ($file_format == IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG) {
138
      imagecolortransparent($distorted_image, $background_color);
139
    }
140
    if (!$distorted_image) {
141
      return FALSE;
142
    }
143

    
144
    if (variable_get('image_captcha_bilinear_interpolation', FALSE)) {
145
      // distortion with bilinear interpolation
146
      for ($x = 0; $x < $width; $x++) {
147
        for ($y = 0; $y < $height; $y++) {
148
          // get distorted sample point in source image
149
          $r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
150
          $theta = $x * $freq_xt + $y * $freq_yt;
151
          $sx = $x + $r * cos($theta);
152
          $sy = $y + $r * sin($theta);
153
          $sxf = (int)floor($sx);
154
          $syf = (int)floor($sy);
155
          if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
156
            $color = $background_color;
157
          }
158
          else {
159
            // bilinear interpolation: sample at four corners
160
            $color_00 = imagecolorat($image, $sxf  , $syf  );
161
            $color_00_r = ($color_00 >> 16) & 0xFF;
162
            $color_00_g = ($color_00 >> 8) & 0xFF;
163
            $color_00_b = $color_00 & 0xFF;
164
            $color_10 = imagecolorat($image, $sxf+1, $syf  );
165
            $color_10_r = ($color_10 >> 16) & 0xFF;
166
            $color_10_g = ($color_10 >> 8) & 0xFF;
167
            $color_10_b = $color_10 & 0xFF;
168
            $color_01 = imagecolorat($image, $sxf  , $syf+1);
169
            $color_01_r = ($color_01 >> 16) & 0xFF;
170
            $color_01_g = ($color_01 >> 8) & 0xFF;
171
            $color_01_b = $color_01 & 0xFF;
172
            $color_11 = imagecolorat($image, $sxf+1, $syf+1);
173
            $color_11_r = ($color_11 >> 16) & 0xFF;
174
            $color_11_g = ($color_11 >> 8) & 0xFF;
175
            $color_11_b = $color_11 & 0xFF;
176
            // interpolation factors
177
            $u  = $sx - $sxf;
178
            $v  = $sy - $syf;
179
            // interpolate
180
            $r = (int)((1-$v)*((1-$u)*$color_00_r + $u*$color_10_r) + $v*((1-$u)*$color_01_r + $u*$color_11_r));
181
            $g = (int)((1-$v)*((1-$u)*$color_00_g + $u*$color_10_g) + $v*((1-$u)*$color_01_g + $u*$color_11_g));
182
            $b = (int)((1-$v)*((1-$u)*$color_00_b + $u*$color_10_b) + $v*((1-$u)*$color_01_b + $u*$color_11_b));
183
            // build color
184
            $color = ($r<<16) + ($g<<8) + $b;
185
          }
186
          imagesetpixel($distorted_image, $x, $y, $color);
187
        }
188
      }
189
    }
190
    else {
191
      // distortion with nearest neighbor interpolation
192
      for ($x = 0; $x < $width; $x++) {
193
        for ($y = 0; $y < $height; $y++) {
194
          // get distorted sample point in source image
195
          $r = $distortion_amplitude * sin($x * $freq_xr + $y * $freq_yr);
196
          $theta = $x * $freq_xt + $y * $freq_yt;
197
          $sx = $x + $r * cos($theta);
198
          $sy = $y + $r * sin($theta);
199
          $sxf = (int)floor($sx);
200
          $syf = (int)floor($sy);
201
          if ($sxf < 0 || $syf < 0 || $sxf >= $width - 1 || $syf >= $height - 1) {
202
            $color = $background_color;
203
          }
204
          else {
205
            $color = imagecolorat($image, $sxf, $syf);
206
          }
207
          imagesetpixel($distorted_image, $x, $y, $color);
208
        }
209
      }
210
    }
211
    // release undistorted image
212
    imagedestroy($image);
213
    // return distorted image
214
    return $distorted_image;
215
  }
216
  else {
217
    return $image;
218
  }
219
}
220

    
221
function _image_captcha_image_generator_add_lines(&$image, $width, $height, $colors) {
222
  $line_quantity = $width * $height/200.0 * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
223
  for ($i = 0; $i <  $line_quantity; $i++) {
224
    imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
225
  }
226
}
227

    
228
function _image_captcha_image_generator_add_dots(&$image, $width, $height, $colors) {
229
  $noise_quantity = $width * $height * ((int) variable_get('image_captcha_noise_level', 5)) / 10.0;
230
  for ($i = 0; $i < $noise_quantity; $i++ ) {
231
    imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $colors[array_rand($colors)]);
232
  }
233
}
234

    
235
/**
236
 * Helper function for drawing text on the image.
237
 */
238
function _image_captcha_image_generator_print_string(&$image, $width, $height, $fonts, $font_size, $text, $rtl=FALSE) {
239
  // get characters
240
  $characters = _image_captcha_utf8_split($text);
241
  $character_quantity = count($characters);
242

    
243
  // get colors
244
  $background_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_background_color', '#ffffff'));
245
  $foreground_rgb = _image_captcha_hex_to_rgb(variable_get('image_captcha_foreground_color', '#000000'));
246
  $background_color = imagecolorallocate($image, $background_rgb[0], $background_rgb[1], $background_rgb[2]);
247
  $foreground_color = imagecolorallocate($image, $foreground_rgb[0], $foreground_rgb[1], $foreground_rgb[2]);
248
  // precalculate the value ranges for color randomness
249
  $foreground_randomness = (int)(variable_get('image_captcha_foreground_color_randomness', 100));
250
  if ($foreground_randomness) {
251
    $foreground_color_range = array();
252
    for ($i=0; $i<3; $i++) {
253
      $foreground_color_range[$i] = array(max(0, $foreground_rgb[$i] - $foreground_randomness), min(255, $foreground_rgb[$i] + $foreground_randomness));
254
    }
255
  }
256

    
257
  // set default text color
258
  $color = $foreground_color;
259

    
260
  // the image is seperated in different character cages, one for each character
261
  // each character will be somewhere inside that cage
262
  $ccage_width = $width / $character_quantity;
263
  $ccage_height = $height;
264

    
265
  foreach ($characters as $c => $character) {
266
    // initial position of character: in the center of its cage
267
    $center_x = ($c + 0.5) * $ccage_width;
268
    if ($rtl) {
269
      $center_x = $width - $center_x;
270
    }
271
    $center_y = 0.5 * $height;
272

    
273
    // Pick a random font from the list.
274
    $font = $fonts[array_rand($fonts)];
275

    
276
    // Get character dimensions for TrueType fonts.
277
    if ($font != 'BUILTIN') {
278
      $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
279
      // In very rare cases with some versions of the GD library, the x-value
280
      // of the left side of the bounding box as returned by the first call of
281
      // imagettfbbox is corrupt (value -2147483648 = 0x80000000).
282
      // The weird thing is that calling the function a second time
283
      // can be used as workaround.
284
      // This issue is discussed at http://drupal.org/node/349218.
285
      if ($bbox[2] < 0) {
286
        $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
287
      }
288
    }
289
    else {
290
      $character_width = imagefontwidth(5);
291
      $character_height = imagefontheight(5);
292
      $bbox = array(0, $character_height, $character_width, $character_height, $character_width, 0, 0, 0);
293
    }
294

    
295
    // Random (but small) rotation of the character.
296
    // TODO: add a setting for this?
297
    $angle = mt_rand(-10, 10);
298

    
299
    // Determine print position: at what coordinate should the character be
300
    // printed so that the bounding box would be nicely centered in the cage?
301
    $bb_center_x = .5 * ($bbox[0] + $bbox[2]);
302
    $bb_center_y = .5 * ($bbox[1] + $bbox[7]);
303
    $angle_cos = cos($angle*3.1415/180);
304
    $angle_sin = sin($angle*3.1415/180);
305
    $pos_x = $center_x - ($angle_cos * $bb_center_x + $angle_sin * $bb_center_y);
306
    $pos_y = $center_y - (-$angle_sin * $bb_center_x + $angle_cos * $bb_center_y);
307

    
308
    // Calculate available room to jitter: how much can the character be moved
309
    // so that it stays inside its cage?
310
    $bb_width = $bbox[2] - $bbox[0];
311
    $bb_height = $bbox[1] - $bbox[7];
312
    $dev_x = .5 * max(0, $ccage_width - abs($angle_cos) * $bb_width - abs($angle_sin) * $bb_height);
313
    $dev_y = .5 * max(0, $ccage_height - abs($angle_cos) * $bb_height - abs($angle_sin) * $bb_width);
314

    
315
    // add jitter to position
316
    $pos_x = $pos_x + mt_rand(-$dev_x, $dev_x);
317
    $pos_y = $pos_y + mt_rand(-$dev_y, $dev_y);
318

    
319
    // calculate text color in case of randomness
320
    if ($foreground_randomness) {
321
      $color = imagecolorallocate($image,
322
        mt_rand($foreground_color_range[0][0], $foreground_color_range[0][1]),
323
        mt_rand($foreground_color_range[1][0], $foreground_color_range[1][1]),
324
        mt_rand($foreground_color_range[2][0], $foreground_color_range[2][1])
325
      );
326
    }
327

    
328
    // draw character
329
    if ($font == 'BUILTIN') {
330
      imagestring($image, 5, $pos_x, $pos_y, $character, $color);
331
    }
332
    else {
333
      imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, drupal_realpath($font), $character);
334
    }
335

    
336
    // For debugging purposes: draw character bounding box (only valid when rotation is disabled).
337
    // imagerectangle($image, $pos_x + $bbox[0], $pos_y + $bbox[1], $pos_x + $bbox[2], $pos_y + $bbox[7], $color);
338

    
339
  }
340

    
341
  // return a sign of success
342
  return TRUE;
343
}