You are here

function textimage_image in Textimage 5

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

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.

This function takes a dynamic number of arguments.

Parameters

$preset_name: The name of the preset to be used in this text image

...: An unlimited number of additional text parameters to be used as the display text for text images displayed on top of one another. Only used if the current preset has the its Background Image option set to the result of another preset. Text is used in reverse order. So the last directory will be the first chained preset used.

$text: The text to be displayed in this preset with the output format appended as the file extension. For example, 'sample.png' will output a PNG with the text 'sample'. 'sample.jpg' will output the same image but in JPG format.

1 string reference to 'textimage_image'
textimage_menu in ./textimage.module
Implementation of hook_menu().

File

./textimage.module, line 634

Code

function textimage_image() {
  $args = func_get_args();
  $preset_name = array_shift($args);
  $filename = array_pop($args);
  $additional_text = $args;

  // Determine our output format
  preg_match('/\\.([a-z]+)$/i', $filename, $matches);
  $format = $matches[1];
  if ($format == 'jpg') {
    $format = 'jpeg';
  }

  // Determine the text to display
  $text = preg_replace('/\\.([a-z]+)$/i', '', $filename);

  // Integrety check
  $output_function = 'image' . $format;
  if (!function_exists($output_function)) {
    $message = t('Unable to generate Text Image %text because the file extension is unsupported on this system. Files must have a .png, .jpg, or .gif extension.', array(
      '%text' => $filename,
    ));
    watchdog('textimage', $message, WATCHDOG_ERROR);
    return $message;
  }

  // Generate the image resource
  $preset = _textimage_preset_load_by_name($preset_name);

  // Check that it exists
  if (!$preset) {
    $message = t('Unable to generate Text Image %text because the preset %preset is not defined.', array(
      '%text' => $filename,
      '%preset' => $preset_name,
    ));
    watchdog('textimage', $message, WATCHDOG_ERROR);
    return $message;
  }
  $img = textimage_image_from_preset($preset['pid'], $text, $additional_text);

  // Display the image to be browser
  header('Content-type: image/' . $format);
  $output_function($img);

  // Save the result so we don't have to recreate
  textimage_directory_check(str_replace('/' . $filename, '', $_GET['q']));
  $output_function($img, $_GET['q']);
  imagedestroy($img);
  exit;
}