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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\media_thumbnails\Plugin\MediaThumbnailBase implements ContainerFactoryPluginInterface, MediaThumbnailInterface uses StringTranslationTrait
- class \Drupal\media_thumbnails_svg\Plugin\MediaThumbnail\MediaThumbnailSVG
- class \Drupal\media_thumbnails\Plugin\MediaThumbnailBase implements ContainerFactoryPluginInterface, MediaThumbnailInterface uses StringTranslationTrait
Expanded class hierarchy of MediaThumbnailSVG
File
- src/
Plugin/ MediaThumbnail/ MediaThumbnailSVG.php, line 21
Namespace
Drupal\media_thumbnails_svg\Plugin\MediaThumbnailView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaThumbnailBase:: |
protected | property | The config factory. | |
MediaThumbnailBase:: |
protected | property | The file system interface. | |
MediaThumbnailBase:: |
protected | property | The logger interface. | |
MediaThumbnailBase:: |
public static | function |
Injects some default services. Overrides ContainerFactoryPluginInterface:: |
|
MediaThumbnailBase:: |
public | function |
Constructs a new Media Thumbnail class. Overrides PluginBase:: |
|
MediaThumbnailSVG:: |
protected | property | ||
MediaThumbnailSVG:: |
protected | property | ||
MediaThumbnailSVG:: |
public | function |
Creates a managed thumbnail file using the passed source file uri. Overrides MediaThumbnailInterface:: |
|
MediaThumbnailSVG:: |
protected | function | ||
MediaThumbnailSVG:: |
protected | function | ||
MediaThumbnailSVG:: |
protected | function | ||
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |