You are here

function textimage_build_image in Textimage 7.2

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

Todo.

3 calls to textimage_build_image()
textimage_image in ./textimage.module
Menu Callback function.
textimage_preset_formatter in ./textimage.module
Default theme callback for all preset.
theme_textimage_image in ./textimage.module
Theme function for displaying textimages.

File

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

Code

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

  // 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;
  }
  if (is_numeric($preset)) {
    $preset = _textimage_preset_load($preset);
  }
  elseif (!is_array($preset)) {

    // This will use value from static variable
    // and will possibly avoid database query.
    $presets = textimage_get_presets();
    $preset = (array) $presets[$preset];
  }

  // Load preset.
  if (!is_array($preset) && !$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;
  }
  $directory = variable_get('file_default_scheme', 'public') . '://textimage/' . $preset['pid'] . '/';
  if (empty($file_uri)) {
    $result = db_query("SELECT file FROM {textimage_image} WHERE pid = :pid AND data = :data", array(
      ':pid' => $preset['pid'],
      ':data' => serialize(array(
        'format' => $format,
        'text' => $text,
        'additional_text' => $additional_text,
      )),
    ))
      ->fetchObject();
  }
  else {
    $filename = explode('/', $file_uri);
    $filename = $filename[count($filename) - 1];
    $file_path = "{$directory}{$filename}.{$format}";
    $result = (object) array(
      'file' => $file_path,
    );
  }

  // CHECK IF FILE EXIST.
  $file_missing = FALSE;
  if (empty($result) || !empty($result) && !is_file(drupal_realpath($result->file))) {
    $file_missing = TRUE;
  }
  if (!$result && (user_access('create textimages') || $method == 'theme') || $preset['pid'] == 0 || $file_missing) {

    // Generate the image resource.
    $img = textimage_image_from_preset($preset, $text, $additional_text);
    if (empty($file_uri)) {
      $filename = check_plain(str_replace('/', '', $text));
      if (empty($filename)) {
        $filename = REQUEST_TIME . rand(1000, 9999) . '.' . $format;
      }

      // Save the result so we don't have to recreate.
      $file_path = $directory . $filename;
      $file_path = textimage_save_image($img, $file_path, $format);
    }
    else {
      $file_uri = textimage_save_image($img, $file_uri, $format);
      $path = file_stream_wrapper_uri_normalize($directory);
      file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
      if ("{$path}/{$filename}.{$format}" != $file_uri) {
        file_unmanaged_copy($file_uri, $file_path, FILE_EXISTS_REPLACE);
      }
    }
    if ($file_missing && $preset['pid'] !== 0) {
      try {
        db_insert('textimage_image')
          ->fields(array(
          'pid' => $preset['pid'],
          'file' => $file_path,
          'data' => serialize(array(
            'format' => $format,
            'text' => $text,
            'additional_text' => $additional_text,
          )),
        ))
          ->execute();
      } catch (Exception $error) {
        db_update('textimage_image')
          ->fields(array(
          'data' => serialize(array(
            'format' => $format,
            'text' => $text,
            'additional_text' => $additional_text,
          )),
        ))
          ->condition('pid', $preset['pid'])
          ->condition('file', $file_path)
          ->execute();
      }
    }
    return $file_path;
  }
  elseif (!$result) {
    drupal_access_denied();
    return FALSE;
  }
  else {
    return $result->file;
  }
}