You are here

protected function TextOverlayImageEffect::buildPreviewRender in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::buildPreviewRender()
  2. 8.2 src/Plugin/ImageEffect/TextOverlayImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\TextOverlayImageEffect::buildPreviewRender()

Builds a render array with the Text Overlay preview.

Requires the Textimage module to be installed.

Parameters

array $data: An array with the plugin configuration to be used for rendering the preview.

Return value

array A simple array with two elements, indicating:

  • success of the preview build.
  • a render array of the preview, or markup describing the failure if the build was unsuccessful.
2 calls to TextOverlayImageEffect::buildPreviewRender()
TextOverlayImageEffect::buildConfigurationForm in src/Plugin/ImageEffect/TextOverlayImageEffect.php
Form constructor.
TextOverlayImageEffect::processAjaxPreview in src/Plugin/ImageEffect/TextOverlayImageEffect.php
Preview Ajax callback.

File

src/Plugin/ImageEffect/TextOverlayImageEffect.php, line 1093

Class

TextOverlayImageEffect
Overlays text on the image, defining text font, size and positioning.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

protected function buildPreviewRender(array $data) {

  // If no font file specified, nothing to preview.
  if (empty($data['font']['uri'])) {
    return [
      FALSE,
      [
        '#markup' => $this
          ->t("No font specified. Select a font and click on 'Refresh preview'."),
      ],
    ];
  }

  // Need the textimage.factory service to produce the preview image.
  $textimage_factory = $this
    ->getTextimageFactory();
  if (!$textimage_factory) {
    return [
      FALSE,
      [
        '#markup' => $this
          ->t("The Textimage module is not installed. It is not possible to provide the text overlay preview image."),
      ],
    ];
  }
  $data['layout']['x_pos'] = 'center';
  $data['layout']['y_pos'] = 'center';
  $data['layout']['x_offset'] = 0;
  $data['layout']['y_offset'] = 0;
  $data['layout']['overflow_action'] = 'extend';
  $data['layout']['extended_color'] = NULL;
  $data['debug_visuals'] = isset($data['preview_bar']['debug_visuals']) ? $data['preview_bar']['debug_visuals'] : FALSE;
  try {
    $textimage = $textimage_factory
      ->get()
      ->setEffects([
      [
        'id' => 'image_effects_text_overlay',
        'data' => $data,
      ],
    ])
      ->setTemporary(TRUE)
      ->process([
      $data['text_string'],
    ])
      ->buildImage();
    $render = [
      '#theme' => 'textimage_formatter',
      '#uri' => $textimage
        ->getUri(),
      '#width' => $textimage
        ->getWidth(),
      '#height' => $textimage
        ->getHeight(),
      '#title' => $this
        ->t('Text overlay preview'),
      '#alt' => $this
        ->t('Text overlay preview.'),
    ];
    $textimage
      ->getBubbleableMetadata()
      ->applyTo($render);
    return [
      TRUE,
      $render,
    ];
  } catch (\Exception $e) {
    return [
      FALSE,
      [
        '#markup' => $this
          ->t("Could not build a preview of the text overlay."),
      ],
    ];
  }
}