You are here

public function PreviewController::preview in Retina Images 8

Deliver a retina version of the preview image.

Parameters

\Symfony\Component\HttpFoundation\Request $request:

\Drupal\image\ImageStyleInterface $image_style:

1 string reference to 'PreviewController::preview'
retina_images.routing.yml in ./retina_images.routing.yml
retina_images.routing.yml

File

src/Controller/PreviewController.php, line 78
Controller for alternate preview image.

Class

PreviewController
Class PreviewController @package Drupal\retina_images\Controller

Namespace

Drupal\retina_images\Controller

Code

public function preview(Request $request, ImageStyleInterface $image_style) {
  $original_path = $this->config
    ->get('preview_image');

  // Set up derivative file information.
  $preview_file = $image_style
    ->buildUri($original_path);

  // Create derivative if necessary.
  if (!file_exists($preview_file)) {
    $image_style
      ->createDerivative($original_path, $preview_file);
  }
  $preview_image = $this->imageFactory
    ->get($preview_file);
  $path_info = pathinfo($preview_file);
  $filename = $path_info['basename'];
  $variables['derivative'] = [
    'url' => file_url_transform_relative(file_create_url($preview_file)),
    'width' => $preview_image
      ->getWidth(),
    'height' => $preview_image
      ->getHeight(),
  ];
  $image_style
    ->transformDimensions($variables['derivative'], $preview_file);
  $image = [
    '#theme' => 'image',
    '#uri' => $variables['derivative']['url'] . '?cache_bypass=' . REQUEST_TIME,
    '#attributes' => [
      'width' => $variables['derivative']['width'],
      'height' => $variables['derivative']['height'],
    ],
  ];
  $image_tag = drupal_render($image);
  $output = <<<EOT
<html>
  <head>
  <title>{<span class="php-variable">$filename</span>} {<span class="php-variable">$variables</span>[<span class="php-string">'derivative'</span>][<span class="php-string">'width'</span>]}&times;{<span class="php-variable">$variables</span>[<span class="php-string">'derivative'</span>][<span class="php-string">'height'</span>]} pixels</title>
  </head>
  <body>
    {<span class="php-variable">$image_tag</span>}
  </body>
</html>

EOT;
  return Response::create($output);
}