You are here

protected function MediaThumbnailSVG::createThumbnailGD in Media Thumbnails SVG 8

1 call to MediaThumbnailSVG::createThumbnailGD()
MediaThumbnailSVG::createThumbnail in src/Plugin/MediaThumbnail/MediaThumbnailSVG.php
Creates a managed thumbnail file using the passed source file uri.

File

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

Class

MediaThumbnailSVG
Media thumbnail plugin for svg documents.

Namespace

Drupal\media_thumbnails_svg\Plugin\MediaThumbnail

Code

protected function createThumbnailGD($path) {
  $image = SVG::fromFile($path);
  if (!$image) {
    $this->logger
      ->warning($this
      ->t('Media entity source file (svg) not found.'));
    return NULL;
  }

  // Create a raster image using the target width, keeping the aspect ratio.
  $width = $image
    ->getDocument()
    ->getWidth() ?: $image
    ->getDocument()
    ->getViewBox()[2];
  $height = $image
    ->getDocument()
    ->getHeight() ?: $image
    ->getDocument()
    ->getViewBox()[3];
  $ratio = $width && $height ? $height / $width : 1;
  $height = (int) ($this->width * $ratio);
  $raster_image = $image
    ->toRasterImage($this->width, $height, $this->bg_color);

  // Create a new image thumbnail blob.
  ob_start();
  if (!imagepng($raster_image, NULL, 9)) {
    $this->logger
      ->warning($this
      ->t('Could not create png from svg using GD.'));
    ob_end_clean();
    return NULL;
  }
  return ob_get_clean();
}