You are here

public function MediaThumbnailSVG::createThumbnail in Media Thumbnails SVG 8

Creates a managed thumbnail file using the passed source file uri.

Overrides MediaThumbnailInterface::createThumbnail

File

src/Plugin/MediaThumbnail/MediaThumbnailSVG.php, line 32

Class

MediaThumbnailSVG
Media thumbnail plugin for svg documents.

Namespace

Drupal\media_thumbnails_svg\Plugin\MediaThumbnail

Code

public function createThumbnail($sourceUri) {

  // Not all rasterizers support stream wrappers, use absolute paths.
  $path = $this->fileSystem
    ->realpath($sourceUri);

  // Get width and background color, if any.
  $this->bg_color = $this->configuration['bgcolor_active'] ? $this->configuration['bgcolor_value'] : 'transparent';
  $this->width = $this->configuration['width'] ?? 500;

  // Create a thumbnail image blob using the best rasterizer available.
  switch (TRUE) {
    case strpos(shell_exec('gm'), 'GraphicsMagick') !== FALSE:
      $image = $this
        ->createThumbnailGM($path);
      break;
    case strpos(shell_exec('convert'), 'ImageMagick') !== FALSE:
      $image = $this
        ->createThumbnailIM($path);
      break;
    default:
      $image = $this
        ->createThumbnailGD($path);
  }

  // Return a new managed file object using the generated thumbnail.
  return $image ? file_save_data($image, $sourceUri . '.png', FileSystemInterface::EXISTS_REPLACE) : NULL;
}