You are here

class MediaThumbnailSVG in Media Thumbnails SVG 8

Media thumbnail plugin for svg documents.

Plugin annotation


@MediaThumbnail(
  id = "media_thumbnail_svg",
  label = @Translation("Media Thumbnail SVG"),
  mime = {
    "image/svg",
    "image/svg+xml",
  }
)

Hierarchy

Expanded class hierarchy of MediaThumbnailSVG

File

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

Namespace

Drupal\media_thumbnails_svg\Plugin\MediaThumbnail
View source
class MediaThumbnailSVG extends MediaThumbnailBase {
  protected $width;
  protected $bg_color;

  /**
   * Creates a managed thumbnail file using the passed source file uri.
   *
   * {@inheritdoc}
   */
  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;
  }
  protected function createThumbnailGM($path) {
    $source = escapeshellarg($path);
    $target = $source . '.png';
    shell_exec("gm convert -background '{$this->bg_color}' -size {$this->width} -quality 100 -strip {$source} {$target}");
    $data = file_get_contents($path . '.png');
    if (!$data) {
      $this->logger
        ->warning($this
        ->t('Could not create png from svg using GM.'));
    }
    return $data;
  }
  protected function createThumbnailIM($path) {
    $source = escapeshellarg($path);
    $target = $source . '.png';
    shell_exec("convert -background '{$this->bg_color}' -density {$this->width} -thumbnail {$this->width} -quality 100 -strip {$source} {$target}");
    $data = file_get_contents($path . '.png');
    if (!$data) {
      $this->logger
        ->warning($this
        ->t('Could not create png from svg using IM.'));
    }
    return $data;
  }
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaThumbnailBase::$config protected property The config factory.
MediaThumbnailBase::$fileSystem protected property The file system interface.
MediaThumbnailBase::$logger protected property The logger interface.
MediaThumbnailBase::create public static function Injects some default services. Overrides ContainerFactoryPluginInterface::create
MediaThumbnailBase::__construct public function Constructs a new Media Thumbnail class. Overrides PluginBase::__construct
MediaThumbnailSVG::$bg_color protected property
MediaThumbnailSVG::$width protected property
MediaThumbnailSVG::createThumbnail public function Creates a managed thumbnail file using the passed source file uri. Overrides MediaThumbnailInterface::createThumbnail
MediaThumbnailSVG::createThumbnailGD protected function
MediaThumbnailSVG::createThumbnailGM protected function
MediaThumbnailSVG::createThumbnailIM protected function
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.