You are here

function textimage_build_image in Textimage 6.2

Same name and namespace in other branches
  1. 5.2 textimage.module \textimage_build_image()
  2. 7.2 textimage.module \textimage_build_image()
2 calls to textimage_build_image()
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.
theme_textimage_image in ./textimage.module
Theme function for displaying textimages

File

./textimage.module, line 289

Code

function textimage_build_image($method, $preset, $text, $additional_text = array(), $format = 'png') {

  // Integrety check
  $output_function = 'image' . $format;
  if (!function_exists($output_function)) {
    $message = t('Unable to generate Textimage as the file extension is unsupported on this system. Files must have a .png, .jpg, or .gif extension.');
    watchdog('textimage', $message, array(), WATCHDOG_ERROR);
    drupal_set_message($message, 'error');
    return FALSE;
  }

  // Load preset
  if (!is_array($preset) && !($preset = _textimage_preset_load($preset))) {
    $message = t('Unable to generate Textimage as the preset %preset is not defined.', array(
      '%preset' => $preset,
    ));
    watchdog('textimage', $message, array(), WATCHDOG_ERROR);
    drupal_set_message($message, 'error');
    return FALSE;
  }
  $result = db_query("SELECT file FROM {textimage_image} WHERE pid = %d AND data = '%s'", $preset['pid'], serialize(array(
    'format' => $format,
    'text' => $text,
    'additional_text' => $additional_text,
  )));
  $result = db_fetch_object($result);
  if (!$result && (user_access('create textimages') || $method == 'theme') || $preset['pid'] == 0) {

    // Generate the image resource
    $img = textimage_image_from_preset($preset, $text, $additional_text);
    $filename = time() . rand(1000, 9999) . '.' . $format;
    $directory = file_directory_path() . '/textimage/' . $preset['pid'];

    // Save the result so we don't have to recreate
    textimage_directory_check($directory);
    $output_function($img, $directory . '/' . $filename);
    imagedestroy($img);
    if ($preset['pid'] != 0) {
      db_query("INSERT INTO {textimage_image} (pid, file, data) VALUES ('%s', '%s', '%s')", $preset['pid'], $directory . '/' . $filename, serialize(array(
        'format' => $format,
        'text' => $text,
        'additional_text' => $additional_text,
      )));
    }
    return $directory . '/' . $filename;
  }
  elseif (!$result) {
    drupal_access_denied();
    return FALSE;
  }
  else {
    return $result->file;
  }
}