public function Textimage::process in Textimage 8.3
Same name and namespace in other branches
- 8.4 src/Textimage.php \Drupal\textimage\Textimage::process()
Process the Textimage, with the required raw text.
Parameters
array|string $text: An array of text strings, or a single string, with tokens not resolved.
Return value
$this
Overrides TextimageInterface::process
File
- src/
Textimage.php, line 481
Class
- Textimage
- Provides a Textimage.
Namespace
Drupal\textimageCode
public function process($text) {
// Do not re-process.
if ($this->processed) {
throw new TextimageException("Attempted to re-process an already processed Textimage");
}
// Effects must be loaded.
if (empty($this->effects)) {
$this->logger
->error('Textimage had no image effects to process.');
return $this;
}
// Collect bubbleable metadata.
if ($this->style) {
$this->bubbleableMetadata = $this->bubbleableMetadata
->addCacheableDependency($this->style);
}
if ($this->sourceImageFile) {
$this->bubbleableMetadata = $this->bubbleableMetadata
->addCacheableDependency($this->sourceImageFile);
}
// Normalise $text to an array.
if (!$text) {
$text = [];
}
if (!is_array($text)) {
$text = [
$text,
];
}
// Find the default text from effects.
$default_text = [];
foreach ($this->effects as $uuid => $effect_configuration) {
if ($effect_configuration['id'] == 'image_effects_text_overlay') {
$uuid = isset($effect_configuration['uuid']) ? $effect_configuration['uuid'] : $uuid;
$default_text[$uuid] = $effect_configuration['data']['text_string'];
}
}
// Process text to resolve tokens and required case conversions.
$processed_text = [];
$this->tokenData['file'] = isset($this->tokenData['file']) ? $this->tokenData['file'] : $this->sourceImageFile;
foreach ($default_text as $uuid => $default_text_item) {
$text_item = array_shift($text);
$effect_instance = $this->imageEffectManager
->createInstance($this->effects[$uuid]['id']);
$effect_instance
->setConfiguration($this->effects[$uuid]);
if ($text_item) {
// Replace any tokens in text with run-time values.
$text_item = $text_item == '[textimage:default]' ? $default_text_item : $text_item;
$processed_text[$uuid] = $this->factory
->processTextString($text_item, NULL, $this->tokenData, $this->bubbleableMetadata);
}
else {
$processed_text[$uuid] = $this->factory
->processTextString($default_text_item, NULL, $this->tokenData, $this->bubbleableMetadata);
}
// Let text be altered by the effect's alter hook.
$processed_text[$uuid] = $effect_instance
->getAlteredText($processed_text[$uuid]);
}
$this->text = $processed_text;
// Set the output image file extension, and find derivative dimensions.
$runtime_effects = $this->effects;
foreach ($this->text as $uuid => $text_item) {
$runtime_effects[$uuid]['data']['text_string'] = $text_item;
}
$runtime_style = $this
->buildStyleFromEffects($runtime_effects);
if ($this->sourceImageFile) {
if ($this->width && $this->height) {
$dimensions = [
'width' => $this->width,
'height' => $this->height,
];
}
else {
// @todo (core) we need to take dimensions via image system as they are
// not available from the file entity, see #1448124.
$source_image = $this->imageFactory
->get($this->sourceImageFile
->getFileUri());
$dimensions = [
'width' => $source_image
->getWidth(),
'height' => $source_image
->getHeight(),
];
}
$uri = $this->sourceImageFile
->getFileUri();
}
else {
$dimensions = [
'width' => 1,
'height' => 1,
];
$uri = NULL;
}
$runtime_style
->transformDimensions($dimensions, $uri);
$this
->set('width', $dimensions['width']);
$this
->set('height', $dimensions['height']);
// Resolve image file extension.
if (!$this->extension) {
if ($this->sourceImageFile) {
$extension = pathinfo($this->sourceImageFile
->getFileUri(), PATHINFO_EXTENSION);
}
else {
$extension = $this->configFactory
->get('textimage.settings')
->get('default_extension');
}
$this
->setTargetExtension($runtime_style
->getDerivativeExtension($extension));
}
// Data for this textimage.
$this->imageData = [
'text' => $this->text,
'extension' => $this->extension,
'sourceImageFileId' => $this->sourceImageFile ? $this->sourceImageFile
->id() : NULL,
'sourceImageFileUri' => $this->sourceImageFile ? $this->sourceImageFile
->getFileUri() : NULL,
'gifTransparentColor' => $this->gifTransparentColor,
];
// Remove text from effects outline, as actual runtime text goes
// separately to the hash.
foreach ($this->effects as $uuid => &$effect_configuration) {
if ($effect_configuration['id'] == 'image_effects_text_overlay') {
unset($effect_configuration['data']['text_string']);
}
}
// Get SHA256 hash, being the Textimage id, for cache checking.
$hash_input = [
'effects_outline' => $this->effects,
'image_data' => $this->imageData,
];
$this->id = hash('sha256', serialize($hash_input));
// Check cache and return if hit.
if ($this->caching && ($cached_data = $this
->getCachedData())) {
$this
->set('uri', $cached_data['uri']);
$this->processed = TRUE;
$this->logger
->debug('Cached Textimage, @uri', [
'@uri' => $this
->getUri(),
]);
if (is_file($this
->getUri())) {
$this->built = TRUE;
}
return $this;
}
else {
// Not found, build the image.
// Get URI of the to-be image file.
if (!$this->uri) {
$this
->buildUri();
}
$this->processed = TRUE;
if ($this->caching) {
$this
->setCached();
}
}
return $this;
}