You are here

protected function Textimage::buildImage in Textimage 7.3

Build the image via core image_style_create_derivative() function.

Return value

self this object

2 calls to Textimage::buildImage()
Textimage::load in classes/Textimage.inc
Load Textimage metadata from store.
Textimage::process in classes/Textimage.inc
Process the Textimage, with the required raw text.

File

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

Class

Textimage
@file Textimage - Textimage class.

Code

protected function buildImage() {

  // Track the image generation time.
  timer_start('Textimage::process');

  // Get URI of the to-be image file.
  if (!$this->uri) {
    $this
      ->buildUri();
  }

  // Inject processed text in the textimage_text effects data.
  $effects = $this->effects;
  $processed_text = $this->text;
  foreach ($effects as &$e_data) {
    if ($e_data['name'] == 'textimage_text') {
      $e_data['data']['text_string'] = array_shift($processed_text);
    }
  }

  // If no source image specified, we are processing a pure Textimage
  // request. In that case we need to use a source dummy 1x1 image stored
  // in textimage/misc/images, and prepend an additional
  // 'textimage_background' effect to ensure we start with a clean
  // background.
  $source = isset($this->sourceImageFile) ? $this->sourceImageFile->uri : NULL;
  if (!$source) {
    $source = drupal_get_path('module', 'textimage') . '/misc/images/base.' . $this->extension;
    $cleanup_effect_id = max(array_keys($effects)) + 1;
    $cleanup_effect = array(
      $cleanup_effect_id => image_effect_definition_load('textimage_background'),
    );
    $cleanup_effect[$cleanup_effect_id]['data'] = array(
      'background_image' => array(
        'mode' => '',
      ),
    );
    $effects = $cleanup_effect + $effects;
  }

  // Build a runtime-only style.
  $runtime_style = TextimageStyles::buildFromEffectsOutline($effects);

  // Reset state.
  TextimageImager::setState();
  TextimageImager::setState('building_module', 'textimage');

  // Try a lock to the file generation process. If cannot get the lock,
  // return success if the file exists already. Otherwise return failure.
  $lock_name = 'textimage_process:' . $this->uri;
  if (!($lock_acquired = lock_acquire($lock_name))) {
    return file_exists($this->uri) ? TRUE : FALSE;
  }

  // Generate the image.
  if (!($this->processed = image_style_create_derivative($runtime_style, $source, $this->uri))) {
    if (isset($this->style)) {
      _textimage_diag(t("Textimage failed to build an image for image style '@style'.", array(
        '@style' => $this->style['name'],
      )), WATCHDOG_ERROR, NULL, $this->userMessages);
    }
    else {
      _textimage_diag(t("Textimage failed to build an image."), WATCHDOG_ERROR, NULL, $this->userMessages);
    }
  }

  // Release lock.
  if (!empty($lock_acquired)) {
    lock_release($lock_name);
  }

  // Reset state.
  TextimageImager::setState();

  // Track the image generation time.
  $timer = timer_stop('Textimage::process');

  // Saves db imagestore data.
  if ($this->processed && $this->caching) {
    $this
      ->setCached();
    $this->timer = $timer['time'];
    $this
      ->putInStore();
  }
}