You are here

function _image_captcha_settings_form_font_section in CAPTCHA 7

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

Form elements for the font specific setting.

This is refactored to a separate function to avoid poluting the general form function image_captcha_settings_form with some specific logic.

Return value

array the font settings specific form elements.

1 call to _image_captcha_settings_form_font_section()
image_captcha_settings_form in image_captcha/image_captcha.admin.inc
Configuration form for image_captcha.

File

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

Code

function _image_captcha_settings_form_font_section() {

  // Put it all in a fieldset.
  $form = array(
    '#type' => 'fieldset',
    '#title' => t('Font settings'),
  );

  // First check if there is TrueType support.
  $setup_status = _image_captcha_check_setup(FALSE);
  if ($setup_status & IMAGE_CAPTCHA_ERROR_NO_TTF_SUPPORT) {

    // Show a warning that there is no TrueType support.
    $form['no_ttf_support'] = array(
      '#type' => 'item',
      '#title' => t('No TrueType support'),
      '#markup' => t('The Image CAPTCHA module can not use TrueType fonts because your PHP setup does not support it. You can only use a PHP built-in bitmap font of fixed size.'),
    );
  }
  else {

    // Build a list of  all available fonts.
    $available_fonts = array();

    // List of folders to search through for TrueType fonts.
    $fonts = _image_captcha_get_available_fonts_from_directories();

    // Cache the list of previewable fonts. All the previews are done
    // in separate requests, and we don't want to rescan the filesystem
    // every time, so we cache the result.
    variable_set('image_captcha_fonts_preview_map_cache', $fonts);

    // Put these fonts with preview image in the list.
    foreach ($fonts as $token => $font) {
      $img_src = check_url(url('admin/config/people/captcha/image_captcha/font_preview/' . $token));
      $title = t('Font preview of @font (@file)', array(
        '@font' => $font->name,
        '@file' => $font->uri,
      ));
      $available_fonts[$font->uri] = '<img src="' . $img_src . '" alt="' . $title . '" title="' . $title . '" />';
    }

    // Append the PHP built-in font at the end.
    $img_src = check_url(url('admin/config/people/captcha/image_captcha/font_preview/BUILTIN'));
    $title = t('Preview of built-in font');
    $available_fonts['BUILTIN'] = t('PHP built-in font: !font_preview', array(
      '!font_preview' => '<img src="' . $img_src . '" alt="' . $title . '" title="' . $title . '" />',
    ));
    $default_fonts = _image_captcha_get_enabled_fonts();
    $form['image_captcha_fonts'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Fonts'),
      '#default_value' => $default_fonts,
      '#description' => t('Select the fonts to use for the text in the image CAPTCHA. Apart from the provided defaults, you can also use your own TrueType fonts (filename extension .ttf) by putting them in %fonts_library_general or %fonts_library_specific.', array(
        '%fonts_library_general' => 'sites/all/libraries/fonts',
        '%fonts_library_specific' => conf_path() . '/libraries/fonts',
      )),
      '#options' => $available_fonts,
      '#attributes' => array(
        'class' => array(
          'image_captcha_admin_fonts_selection',
        ),
      ),
      '#process' => array(
        'form_process_checkboxes',
      ),
    );

    // Font size.
    $form['image_captcha_font_size'] = array(
      '#type' => 'select',
      '#title' => t('Font size'),
      '#options' => array(
        9 => '9 pt - ' . t('tiny'),
        12 => '12 pt - ' . t('small'),
        18 => '18 pt',
        24 => '24 pt - ' . t('normal'),
        30 => '30 pt',
        36 => '36 pt - ' . t('large'),
        48 => '48 pt',
        64 => '64 pt - ' . t('extra large'),
      ),
      '#default_value' => (int) variable_get('image_captcha_font_size', 30),
      '#description' => t('The font size influences the size of the image. Note that larger values make the image generation more CPU intensive.'),
    );
  }

  // Character spacing (available for both the TrueType fonts and the builtin font.
  $form['image_captcha_font_settings']['image_captcha_character_spacing'] = array(
    '#type' => 'select',
    '#title' => t('Character spacing'),
    '#description' => t('Define the average spacing between characters. Note that larger values make the image generation more CPU intensive.'),
    '#default_value' => variable_get('image_captcha_character_spacing', '1.2'),
    '#options' => array(
      '0.75' => t('tight'),
      '1' => t('normal'),
      '1.2' => t('wide'),
      '1.5' => t('extra wide'),
    ),
  );
  return $form;
}