You are here

function image_captcha_font_preview in CAPTCHA 7

Same name and namespace in other branches
  1. 8 image_captcha/image_captcha.admin.inc \image_captcha_font_preview()
  2. 6.2 image_captcha/image_captcha.admin.inc \image_captcha_font_preview()

Menu handler for font preview request.

1 string reference to 'image_captcha_font_preview'
image_captcha_menu in image_captcha/image_captcha.module
Implements hook_menu().

File

image_captcha/image_captcha.admin.inc, line 381
Functions for administration/settings interface.

Code

function image_captcha_font_preview($font_token) {

  // Get the font from the given font token.
  if ($font_token == 'BUILTIN') {
    $font = 'BUILTIN';
  }
  else {

    // Get the mapping of font tokens to font file objects.
    $fonts = variable_get('image_captcha_fonts_preview_map_cache', array());
    if (!isset($fonts[$font_token])) {
      echo 'bad token';
      exit;
    }

    // Get the font path.
    $font = $fonts[$font_token]->uri;

    // Some sanity checks if the given font is valid.
    if (!is_file($font) || !is_readable($font)) {
      echo 'bad font';
      exit;
    }
  }

  // Settings of the font preview.
  $width = 120;
  $text = 'AaBbCc123';
  $font_size = 14;
  $height = 2 * $font_size;

  // Allocate image resource.
  $image = imagecreatetruecolor($width, $height);
  if (!$image) {
    exit;
  }

  // White background and black foreground.
  $background_color = imagecolorallocate($image, 255, 255, 255);
  $color = imagecolorallocate($image, 0, 0, 0);
  imagefilledrectangle($image, 0, 0, $width, $height, $background_color);

  // Draw preview text.
  if ($font == 'BUILTIN') {
    imagestring($image, 5, 1, 0.5 * $height - 10, $text, $color);
  }
  else {
    imagettftext($image, $font_size, 0, 1, 1.5 * $font_size, $color, realpath($font), $text);
  }

  // Set content type.
  drupal_add_http_header('Content-Type', 'image/png');

  // Dump image data to client.
  imagepng($image);

  // Release image memory.
  imagedestroy($image);

  // Close connection.
  exit;
}