You are here

function textimage_image_from_preset in Textimage 5.2

Same name and namespace in other branches
  1. 5 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 Textimage preset and generates the GD image resource.

Parameters

$pid: The id of the preset to be used in this textimage

$text: The text to be displayed in this preset

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

1 call to textimage_image_from_preset()
textimage_build_image in ./textimage.module

File

./textimage.module, line 332

Code

function textimage_image_from_preset($preset, $text, $additional_text = array()) {
  foreach ($preset['settings'] as $key => $settings) {
    if (is_array($settings)) {
      foreach ($settings as $name => $value) {
        ${$key . '_' . $name} = $value;
      }
    }
  }
  $text_fixed_width = isset($text_fixed_width) ? $text_fixed_width : 0;

  // Set font path
  $font_file = variable_get('textimage_fonts_path', drupal_get_path('module', 'textimage') . '/fonts') . '/' . $font_file;

  // Convert text case
  switch ($text_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 = preg_replace('/\\s(\\w+)\\b/e', "drupal_ucfirst('\$1')", $text);
      break;
  }

  // Generate the textimage
  $img = textimage_text_to_image($text, $font_size, $font_file, $font_color, $text_angle, $text_maximum_width, $text_fixed_width, $text_align);

  // Add a border
  if ($text_stroke['width'] && $text_stroke['color']) {
    $img = textimage_image_add_stroke($img, $text_stroke['width'], $text_stroke['color'], $font_color['opacity']);
  }

  // Add margin
  if ($text_margin['top'] || $text_margin['right'] || $text_margin['bottom'] || $text_margin['left']) {
    $img = textimage_image_add_margin($img, $text_margin);
  }

  // Place result on top of another preset's result
  if (is_numeric($background_image) && ($background_image = _textimage_preset_load($background_image))) {
    $next_preset = $background_image;
    $next_text = array_pop($additional_text);
    if (empty($next_text)) {
      $next_text = $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, $background_xoffset, $background_yoffset, 0, 0, $text_width, $text_height);
    imagesavealpha($background_resource, TRUE);
    $img = $background_resource;
  }
  elseif (is_file($background_image)) {
    $info = image_get_info($background_image);
    $background_resource = image_toolkit_invoke('open', array(
      $background_image,
      $info['extension'],
    ));
    $text_width = imagesx($img);
    $text_height = imagesy($img);
    imagealphablending($background_resource, TRUE);
    imagecopy($background_resource, $img, $background_xoffset, $background_yoffset, 0, 0, $text_width, $text_height);
    imagesavealpha($background_resource, TRUE);
    $img = $background_resource;
  }
  else {
    $background_resource = imagecreatetruecolor(imagesx($img), imagesy($img));
    $alpha = 0;
    if (!$background_color) {
      $alpha = 127;
      $background_color = '#FFFFFF';
    }
    list($r, $g, $b) = _textimage_hex2rgb($background_color);
    $back = imagecolorallocatealpha($background_resource, $r, $g, $b, $alpha);
    imagefill($background_resource, 0, 0, $back);
    imagealphablending($background_resource, TRUE);
    imagecopy($background_resource, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
    imagesavealpha($background_resource, TRUE);
    $img = $background_resource;
  }
  return $img;
}