You are here

public function Textimage::process in Textimage 7.3

Process the Textimage, with the required raw text.

Parameters

array $text: An array of text strings, with tokens not resolved.

Return value

self this object

File

classes/Textimage.inc, line 412
Textimage - Textimage class.

Class

Textimage
@file Textimage - Textimage class.

Code

public function process($text) {

  // Do not re-process.
  if ($this->processed) {
    return $this;
  }

  // Effects must be loaded.
  if (empty($this->effects)) {
    _textimage_diag(t("Textimage had no image effects to process."), WATCHDOG_ERROR, NULL, $this->userMessages);
    return $this;
  }

  // Normalise $text to an array.
  if (!$text) {
    $text = array();
  }
  if (!is_array($text)) {
    $text = array(
      $text,
    );
  }

  // Build an array with default text from effects.
  $default_text = array();
  foreach ($this->effects as &$effect) {
    if ($effect['name'] == 'textimage_text') {
      $default_text[] = $effect['data']['text_string'];
    }
  }

  // Process text to resolve tokens and required case conversions.
  $processed_text = array();
  foreach ($this->effects as $e_data) {
    if ($e_data['name'] == 'textimage_text') {
      $text_item = array_shift($text);
      $default_text_item = array_shift($default_text);
      if ($text_item) {

        // Replace any tokens in text with run-time values.
        $text_item = $text_item == '[textimage:default]' ? $default_text_item : $text_item;
        $processed_text[] = TextimageImager::processTextString($text_item, $e_data['data']['text']['case_format'], $this->node, $this->sourceImageFile);
      }
      elseif ($default_text_item) {
        $processed_text[] = TextimageImager::processTextString($default_text_item, $e_data['data']['text']['case_format'], $this->node, $this->sourceImageFile);
      }
      else {
        $processed_text[] = t('* Missing text *');
      }
    }
  }
  $this->text = $processed_text;

  // Remove default text from effects outline, as actual runtime text goes
  // separately to the hash.
  foreach ($this->effects as &$effect) {
    if ($effect['name'] == 'textimage_text') {
      unset($effect['data']['text_string']);
    }
  }

  // Data for this textimage. Use a dummy filename at this stage,
  // purely to resolve the mime type.
  $this->imageData = array(
    'text' => $this->text,
    'filemime' => file_get_mimetype('dummy.' . $this->extension),
    'extension' => $this->extension,
    'source' => $this->sourceImageFile ? $this->sourceImageFile->uri : NULL,
  );
  if ($this->forceHashedFilename) {
    $this->imageData['forceHashedFilename'] = TRUE;
  }

  // Get md5 hash, being the Textimage id, for cache checking.
  $hash_input = array(
    'effects_outline' => $this->effects,
    'image_data' => $this->imageData,
  );
  $this->id = md5(serialize($hash_input));

  // Check cache and/or store and return if db and file hit.
  if ($this->caching && $this
    ->getCached()) {
    $this->processed = TRUE;
    return $this;
  }

  // Build the image.
  $this
    ->buildImage();
  return $this;
}