You are here

function textimage_preset_formatter in Textimage 7.2

Default theme callback for all preset.

1 call to textimage_preset_formatter()
textimage_deliver in ./textimage.module
Todo.
1 string reference to 'textimage_preset_formatter'
textimage_theme in ./textimage.module
Implements hook_theme().

File

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

Code

function textimage_preset_formatter($variables) {
  $method = isset($variables['method']) ? $variables['method'] : 'theme';
  $image = (array) $variables['image'];
  if (!empty($image)) {
    $path = variable_get('file_default_scheme', 'public') . '://textimage/' . $variables['pid'] . '/' . $image['filename'];
    if (is_file(drupal_realpath($path))) {
      return $path;
    }
    else {

      // Image was flushed, recreate the textimage from its data.
      $result = db_query('SELECT pid, data FROM {textimage_image} WHERE file LIKE :file', array(
        ':file' => $path,
      ))
        ->fetchObject();
      if (!empty($result)) {
        $format = str_replace('image/', '', $image['filemime']);
        $uri = preg_replace('/(.' . $format . '$)/', '', $image['uri']);
        $data = unserialize($result->data);
        return textimage_build_image($method, $variables['preset'], $data['text'], $data['additional_text'], $format, $uri);
      }
      else {

        // Try to replace the cache of previously
        // assigned preset for this image.
        $result = db_query('SELECT pid, data, file FROM {textimage_image} WHERE file LIKE :file', array(
          ':file' => '%/' . $image['filename'],
        ))
          ->fetchObject();
        if (!empty($result)) {
          $format = str_replace('image/', '', $image['filemime']);
          $uri = preg_replace('/(.' . $format . '$)/', '', $image['uri']);
          file_unmanaged_delete($result->file);
          db_delete('textimage_image')
            ->condition('pid', $result->pid)
            ->condition('file', $result->file)
            ->condition('data', $result->data)
            ->execute();
          $data = unserialize($result->data);
          return textimage_build_image($method, $variables['preset'], $data['text'], $data['additional_text'], $format, $uri);
        }
        elseif (!empty($variables['text'])) {
          $format = str_replace('image/', '', $image['filemime']);
          $uri = preg_replace('/(.' . $format . '$)/', '', $image['uri']);

          // Try to create new textimage from defined 'text'
          return textimage_build_image($method, $variables['preset'], $variables['text'], !empty($variables['additional_text']) ? $variables['additional_text'] : array(), $format, $uri);
        }
      }
    }
  }
  $message = t('Cached Textimage image does not exist or has been flushed.');
  watchdog('textimage', $message, array(), WATCHDOG_ERROR);
  return '';
}