You are here

function textimage_background_effect_form in Textimage 7.3

Settings for 'textimage_background' image effect.

Implements hook_form().

Parameters

array $data: array of settings for this action

Return value

array a form definition

1 string reference to 'textimage_background_effect_form'
textimage_image_effect_info in ./textimage.module
Implements hook_image_effect_info().

File

effects/textimage_background.inc, line 52
Implementation of the 'textimage_background' image effect.

Code

function textimage_background_effect_form($data) {

  // Merge input data with effect defaults.
  $data = drupal_array_merge_deep(_textimage_background_effect_defaults(), $data);
  $form = array();

  // Background image mode.
  $form['background_image'] = array(
    '#name' => 'background_image',
    '#type' => 'fieldset',
    '#title' => t('Background image'),
    '#description' => t('Select a background image to be used.'),
  );

  // Background image mode - options.
  $image_options = array(
    '' => t('No background image.'),
    'passthrough' => t('Inherit image from previous effect.'),
    'select' => t('Select image.'),
  );
  $form['background_image']['mode'] = array(
    '#type' => 'radios',
    '#options' => $image_options,
    '#default_value' => $data['background_image']['mode'],
  );

  // Background image mode - image selection.
  if (_textimage_get_variable('backgrounds_handling_module') == 'media') {

    // Media module available - use media form element.
    if (!isset($data['background_image']['fid'])) {
      $data['background_image']['fid'] = 0;
    }
    $scheme_options = array();
    foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $stream_wrapper) {
      $scheme_options[$scheme] = $scheme;
    }
    $form['background_image']['fid'] = array(
      '#title' => NULL,
      '#type' => 'media',
      '#media_options' => array(
        'global' => array(
          'types' => array(
            'image',
          ),
          'file_extensions' => 'png gif jpg jpeg',
          'schemes' => $scheme_options,
        ),
      ),
      '#description' => NULL,
      '#default_value' => array(
        'fid' => $data['background_image']['fid'],
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="data[background_image][mode]"]' => array(
            'value' => 'select',
          ),
        ),
      ),
    );
  }
  else {

    // Default - use dropdown with list of images.
    $image_files = drupal_map_assoc(_textimage_background_image_list(_textimage_get_variable('backgrounds_path')));
    if (empty($image_files)) {
      _textimage_diag(t('No background images available. Make sure at least one image is available in the directory specified in the <a href="@url">configuration page</a>.', array(
        '@url' => url('admin/config/media/textimage'),
      )), WATCHDOG_WARNING);
    }
    $form['background_image']['uri'] = array(
      '#type' => 'select',
      '#options' => $image_files,
      '#default_value' => $data['background_image']['uri'] ? pathinfo($data['background_image']['uri'], PATHINFO_BASENAME) : '',
      '#states' => array(
        'visible' => array(
          ':input[name="data[background_image][mode]"]' => array(
            'value' => 'select',
          ),
        ),
      ),
    );
  }

  // Background color.
  $form['background'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => 'Background color',
    '#description' => t('Select the color you wish to use for the background of the image. This color will be placed around the image or fill the background if no image is selected.'),
  );
  $form['background']['color'] = array(
    '#type' => 'textimage_color',
    '#title' => t('Color'),
    '#allow_transparent' => TRUE,
    '#allow_opacity' => TRUE,
    '#default_value' => $data['background']['color'],
  );
  $form['background']['repeat'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use color for subsequent Textimage effects'),
    '#description' => t('If checked, this color will be used as a filler in case Textimage effects applied later need to extend the size of the image.'),
    '#default_value' => $data['background']['repeat'],
  );

  // Background image exact dimensions.
  $form['exact'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => 'Exact size',
    '#description' => t('Set the background image to an exact size, either width or heigth. If only one of width or heigth is set, the other dimension will be automatically calculated based on resize/scale/crop options.'),
  );
  if (!$data['exact']['width'] && !$data['exact']['height']) {
    $form['exact']['#collapsed'] = TRUE;
  }
  $form['exact']['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#default_value' => $data['exact']['width'],
    '#description' => t('Enter a value in pixels.'),
    '#size' => 5,
    '#field_suffix' => 'px',
  );
  $form['exact']['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#default_value' => $data['exact']['height'],
    '#description' => t('Enter a value in pixels.'),
    '#size' => 5,
    '#field_suffix' => 'px',
  );
  $form['exact']['position'] = array(
    '#type' => 'radios',
    '#title' => t('Position'),
    '#options' => array(
      'left-top' => t('Top') . ' ' . t('Left'),
      'center-top' => t('Top') . ' ' . t('Center'),
      'right-top' => t('Top') . ' ' . t('Right'),
      'left-center' => t('Center') . ' ' . t('Left'),
      'center-center' => t('Center'),
      'right-center' => t('Center') . ' ' . t('Right'),
      'left-bottom' => t('Bottom') . ' ' . t('Left'),
      'center-bottom' => t('Bottom') . ' ' . t('Center'),
      'right-bottom' => t('Bottom') . ' ' . t('Right'),
    ),
    '#theme' => 'image_anchor',
    '#default_value' => implode('-', array(
      $data['exact']['xpos'],
      $data['exact']['ypos'],
    )),
    '#description' => t('Position of the image on the resulting canvas, if the background image selected is smaller than the canvas.'),
  );
  $form['exact']['dimensions'] = array(
    '#type' => 'radios',
    '#title' => t('Image overflow'),
    '#default_value' => $data['exact']['dimensions'],
    '#options' => array(
      'scale' => t('Scale image to fit.'),
      'crop' => t('Crop image.'),
      'resize' => t('Resize image to fit - allowing possible distortion.'),
    ),
    '#description' => t('Action to take when the background image selected is bigger size than the canvas.'),
  );
  $form['exact']['crop'] = array(
    '#type' => 'radios',
    '#title' => t('Crop anchor'),
    '#options' => array(
      'left-top' => t('Top') . ' ' . t('Left'),
      'center-top' => t('Top') . ' ' . t('Center'),
      'right-top' => t('Top') . ' ' . t('Right'),
      'left-center' => t('Center') . ' ' . t('Left'),
      'center-center' => t('Center'),
      'right-center' => t('Center') . ' ' . t('Right'),
      'left-bottom' => t('Bottom') . ' ' . t('Left'),
      'center-bottom' => t('Bottom') . ' ' . t('Center'),
      'right-bottom' => t('Bottom') . ' ' . t('Right'),
    ),
    '#theme' => 'image_anchor',
    '#default_value' => $data['exact']['crop'],
    '#description' => t('Select which part of the selected background image should be cropped.'),
    '#states' => array(
      'visible' => array(
        ':input[name="data[exact][dimensions]"]' => array(
          'value' => 'crop',
        ),
      ),
    ),
  );

  // Background image relative dimensions.
  $form['relative'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Relative size'),
    '#description' => t('Set the background image to a relative size, based on the original image dimensions. Use to add simple borders or expand by a fixed amount. Negative values will crop the image.'),
    'topdiff' => array(
      '#type' => 'textfield',
      '#title' => t('Top'),
      '#default_value' => $data['relative']['topdiff'],
      '#size' => 6,
      '#description' => t('Enter a value in pixels.'),
      '#field_suffix' => 'px',
    ),
    'rightdiff' => array(
      '#type' => 'textfield',
      '#title' => t('Right'),
      '#default_value' => $data['relative']['rightdiff'],
      '#size' => 6,
      '#description' => t('Enter a value in pixels.'),
      '#field_suffix' => 'px',
    ),
    'bottomdiff' => array(
      '#type' => 'textfield',
      '#title' => t('Bottom'),
      '#default_value' => $data['relative']['bottomdiff'],
      '#size' => 6,
      '#description' => t('Enter a value in pixels.'),
      '#field_suffix' => 'px',
    ),
    'leftdiff' => array(
      '#type' => 'textfield',
      '#title' => t('Left'),
      '#default_value' => $data['relative']['leftdiff'],
      '#size' => 6,
      '#description' => t('Enter a value in pixels.'),
      '#field_suffix' => 'px',
    ),
  );
  if (!$data['relative']['leftdiff'] && !$data['relative']['rightdiff'] && !$data['relative']['topdiff'] && !$data['relative']['bottomdiff']) {
    $form['relative']['#collapsed'] = TRUE;
  }
  $form['#element_validate'][] = 'textimage_background_effect_form_validate';
  return $form;
}