You are here

function CollageFormatter::collageformatter_image_file_check in Collage Formatter 8

Checks for/creates the original image reference file.

1 call to CollageFormatter::collageformatter_image_file_check()
CollageFormatter::collageformatter_render_box in src/Plugin/Field/FieldFormatter/CollageFormatter.php
Recursive function to render the box.

File

src/Plugin/Field/FieldFormatter/CollageFormatter.php, line 999
Contains \Drupal\collageformatter\src\Plugin\Field\FieldFormatter\CollageFormatter.

Class

CollageFormatter
Plugin implementation of the 'collageformatter' formatter.

Namespace

Drupal\collageformatter\Plugin\Field\FieldFormatter

Code

function collageformatter_image_file_check($box, $settings) {
  $image_width = round($box['total_width'] - 2 * $settings['border_size'] - $settings['gap_size'] - 0.5);
  $image_height = round($box['total_height'] - 2 * $settings['border_size'] - $settings['gap_size'] - 0.5);
  $filesystem = \Drupal::service('file_system');
  $filename = $image_width . 'x' . $image_height . '_' . $settings['advanced']['original_image_reference'] . '_' . $filesystem
    ->basename($box['uri']);
  $directory = $filesystem
    ->dirname(file_build_uri('collageformatter/' . file_uri_target($box['uri'])));
  $image_uri = $directory . '/' . $filename;
  if (!file_exists($image_uri)) {
    if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      if ($settings['advanced']['original_image_reference'] == 'symlink') {
        if (!symlink($filesystem
          ->realpath($box['uri']), $filesystem
          ->realpath($image_uri))) {
          \Drupal::logger('collageformatter')
            ->notice('Failed to symlink file @source to @destination.', [
            '@source' => $box['uri'],
            '@destination' => $image_uri,
          ]);
        }
      }
      elseif ($settings['advanced']['original_image_reference'] == 'copy') {
        if (!file_unmanaged_copy($box['uri'], $image_uri, FILE_EXISTS_REPLACE)) {
          \Drupal::logger('collageformatter')
            ->notice('Failed to copy file from @source to @destination.', [
            '@source' => $box['uri'],
            '@destination' => $image_uri,
          ]);
        }
      }
      elseif ($settings['advanced']['original_image_reference'] == 'fake') {
        $image = \Drupal::service('image.factory')
          ->get($box['uri']);
        $image
          ->apply('image_scale_effect', [
          'width' => 1,
          'height' => 1,
        ]);
        $image
          ->save($image_uri);
      }
    }
  }
  return $image_uri;
}