public function Textimage::buildImage in Textimage 8.4
Same name and namespace in other branches
- 8.3 src/Textimage.php \Drupal\textimage\Textimage::buildImage()
Build the image via core ImageStyle::createDerivative() method.
Return value
$this
Overrides TextimageInterface::buildImage
File
- src/
Textimage.php, line 648
Class
- Textimage
- Provides a Textimage.
Namespace
Drupal\textimageCode
public function buildImage() {
// Do not proceed if not processed.
if (!$this->processed) {
throw new TextimageException("Attempted to build Textimage before processing data");
}
// Do not re-build.
if ($this->built) {
return $this;
}
// Check file store and return if hit.
if ($this->caching && is_file($this
->getUri())) {
$this->logger
->debug('Stored Textimage, @uri', [
'@uri' => $this
->getUri(),
]);
return $this;
}
// If no source image specified, we are processing a pure Textimage
// request. In that case we create a new 1x1 image to ensure we start
// with a clean background.
$source = isset($this->sourceImageFile) ? $this->sourceImageFile
->getFileUri() : NULL;
$image = $this->imageFactory
->get($source);
if (!$source) {
$image
->createNew(1, 1, $this->extension, $this->gifTransparentColor);
}
// Reset state.
$this->factory
->setState();
$this->factory
->setState('building_module', 'textimage');
// Try a lock to the file generation process. If cannot get the lock,
// return success if the file exists already. Otherwise return failure.
$lock_name = 'textimage_process:' . Crypt::hashBase64($this
->getUri());
if (!($lock_acquired = $this->lock
->acquire($lock_name))) {
return file_exists($this
->getUri()) ? TRUE : FALSE;
}
// Inject processed text in the image_effects_text_overlay effects data,
// and build a runtime-only style.
$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);
// Manage change of file extension if needed.
if ($this->sourceImageFile) {
$runtime_extension = pathinfo($this->sourceImageFile
->getFileUri(), PATHINFO_EXTENSION);
}
else {
$runtime_extension = $this->extension;
}
$runtime_extension = $runtime_style
->getDerivativeExtension($runtime_extension);
if ($runtime_extension != $this->extension) {
// Find the max weight from effects.
$max_weight = NULL;
foreach ($runtime_style
->getEffects()
->getConfiguration() as $effect_configuration) {
if (!$max_weight || $effect_configuration['weight'] > $max_weight) {
$max_weight = $effect_configuration['weight'];
}
}
// Add an image_convert effect as last effect.
$convert = [
'id' => 'image_convert',
'weight' => ++$max_weight,
'data' => [
'extension' => $this->extension,
],
];
$runtime_style
->addImageEffect($convert);
}
// Generate the image.
if (!($this->processed = $this
->createDerivativeFromImage($runtime_style, $image, $this
->getUri()))) {
if (isset($this->style)) {
throw new TextimageException("Textimage failed to build an image for image style '{$this->style->id()}'");
}
else {
throw new TextimageException("Textimage failed to build an image");
}
}
$this->logger
->debug('Built Textimage, @uri', [
'@uri' => $this
->getUri(),
]);
// Release lock.
if (!empty($lock_acquired)) {
$this->lock
->release($lock_name);
}
// Reset state.
$this->factory
->setState();
$this->built = TRUE;
return $this;
}