You are here

function textimage_image_from_preset in Textimage 5

Same name and namespace in other branches
  1. 5.2 textimage.module \textimage_image_from_preset()
  2. 6.2 textimage.module \textimage_image_from_preset()
  3. 7.2 textimage.module \textimage_image_from_preset()

Loads the Text Image preset and generates the GD image resource.

Parameters

$preset_id: The id of the preset to be used in this text image

$text: The text to be displayed in this preset

$additional_text: An array of text to be used in subsequent text images. Only used if this preset uses the result of another preset as its background image.

1 call to textimage_image_from_preset()
textimage_image in ./textimage.module
Menu Callback function converts the current textimage path into an image. On first request, the image is returned to the browser from Drupal and then saved to the disk. On subsequent requests (with Clean URLs enabled), the cached image is loaded directly.

File

./textimage.module, line 693

Code

function textimage_image_from_preset($preset_id, $text, $additional_text) {
  $preset = _textimage_preset_load($preset_id);
  $text = str_replace('_', ' ', $text);
  $font = variable_get('textimage_fonts_path', '') . '/' . $preset['settings']['text']['font'];

  // Fill in default preset settings.
  $defaults = array(
    'text' => array(
      'size' => '',
      'angle' => 0,
      'color' => '',
      'case' => '',
      'stroke_width' => '',
      'stroke_color' => '#000000',
      'maximum_width' => 0,
      'margin_top' => 0,
      'margin_right' => 0,
      'margin_bottom' => 0,
      'margin_left' => 0,
      'fixed_width' => 0,
      'align' => ALIGN_LEFT,
    ),
    'background' => array(
      'back_color' => '',
      'back_image' => '',
      'back_xoffset' => 0,
      'back_yoffset' => 0,
    ),
  );
  foreach ($defaults as $set => $values) {
    $settings = $preset['settings'][$set];
    foreach ($values as $key => $value) {

      // Strip prefixes like 'back_' for array lookups.
      $array_key = preg_replace('/^back_/', '', $key);

      // Assign to variable and fill in default.
      ${$key} = $settings[$array_key] ? $settings[$array_key] : $value;
    }
  }

  // Convert text case
  switch ($case) {
    case 'upper':
      $text = drupal_strtoupper($text);
      break;
    case 'lower':
      $text = drupal_strtolower($text);
      break;
    case 'ucfirst':
      $text = drupal_ucfirst($text);
      break;
    case 'ucwords':
      $text = drupal_strtolower($text);
      $words = explode(' ', $text);
      foreach ($words as $key => $word) {
        $words[$key] = drupal_ucfirst($word);
      }
      $text = implode(' ', $words);
      break;
  }
  if (function_exists('drupal_' . $case)) {
    $text = drupal_strtoupper($text);
  }

  // Generate the text image
  $img = textimage_text_to_image($text, $size, $font, $color, $back_color, $angle, $maximum_width, $fixed_width, $align);

  // Add margin
  if ($margin_top || $margin_right || $margin_bottom || $margin_left) {
    $img = textimage_image_add_margin($img, $margin_top, $margin_right, $margin_bottom, $margin_left, $back_color);
  }

  // Add a border
  if ($stroke_width && $stroke_color) {
    $img = textimage_image_add_stroke($img, $stroke_width, $stroke_color);
  }

  // Place result on top of another preset's result
  if (is_numeric($back_image) && _textimage_preset_load($back_image)) {
    $next_preset = $back_image;
    $next_text = array_pop($additional_text);
    $background_resource = textimage_image_from_preset($next_preset, $next_text, $additional_text);
    $text_width = imagesx($img);
    $text_height = imagesy($img);
    imagealphablending($background_resource, true);
    imagecopy($background_resource, $img, $back_xoffset, $back_yoffset, 0, 0, $text_width, $text_height);
    $img = $background_resource;
  }
  elseif (is_file($back_image)) {
    $info = image_get_info($back_image);
    $background_resource = image_gd_open($back_image, $info['extension']);
    $text_width = imagesx($img);
    $text_height = imagesy($img);
    imagecopy($background_resource, $img, $back_xoffset, $back_yoffset, 0, 0, $text_width, $text_height);
    $img = $background_resource;
  }
  return $img;
}