You are here

function textimage_image_from_preset in Textimage 7.2

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

Loads the Textimage preset and generates the GD image resource.

Parameters

array $preset: The preset to be used in this textimage

string $text: The text to be displayed in this preset

array $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.

2 calls to textimage_image_from_preset()
textimage_build_image in ./textimage.module
Todo.
textimage_build_preview in ./textimage.module
Todo.

File

./textimage.module, line 464
Provides text to image manipulations.

Code

function textimage_image_from_preset($preset, $text, $additional_text = array()) {
  module_load_include('inc', 'textimage', 'textimage.utils');
  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_callback('/(\\w+)/', '_textimage_ucwords', $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)) {

    // Place result on background image if available.
    $info = image_get_info($background_image);
    $imagecreate = 'imagecreatefrom' . $info['extension'];
    if (function_exists($imagecreate)) {
      $background_resource = $imagecreate($background_image);
      $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;
}