public function TextimageDownloadController::urlDeliver in Textimage 8.3
Same name and namespace in other branches
- 8.4 src/Controller/TextimageDownloadController.php \Drupal\textimage\Controller\TextimageDownloadController::urlDeliver()
Deliver directly a Textimage from the URL request.
After generating an image, transfer it to the requesting agent.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request object. The 'text' query parameter coming from the URL contains the text elements to be used to deliver the Textimage.
\Drupal\image\ImageStyleInterface $image_style: The image style to deliver.
Return value
\Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\Response The transferred file as response or some error response.
Throws
\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown when Textimage URL generation is not enabled.
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the image style is missing.
File
- src/
Controller/ TextimageDownloadController.php, line 118
Class
- TextimageDownloadController
- Defines a controller to serve image styles.
Namespace
Drupal\textimage\ControllerCode
public function urlDeliver(Request $request, ImageStyleInterface $image_style) {
// Check if the URL generation is enabled.
if (!$this->configFactory
->get('textimage.settings')
->get('url_generation.enabled')) {
throw new AccessDeniedHttpException('Textimage URL generation is not enabled on this site');
}
// Check if the style exists, is relevant, and set to 'public' scheme in
// TPS.
if (!$this->textimageFactory
->isTextimage($image_style)) {
$this->logger
->error("URL generation - The image style '%style_name' is not relevant for Textimage.", [
'%style_name' => $image_style
->getName(),
]);
throw new NotFoundHttpException("The image style requested is not relevant for Textimage");
}
if ($image_style
->getThirdPartySetting('textimage', 'uri_scheme', $this->configFactory
->get('system.file')
->get('default_scheme')) !== 'public') {
$this->logger
->error("URL generation - The image style '%style_name' is not set to produce image files for the 'public' file scheme -> disabled.", [
'%style_name' => $image_style
->getName(),
]);
throw new AccessDeniedHttpException("The image style requested is not set to produce image files for the 'public' file scheme");
}
// {Text_0}[sep]{Text_1}[sep]...[sep]{Text_n} to the $text array.
$text_string = $request->query
->get('text');
$text = explode($this->configFactory
->get('textimage.settings')
->get('url_generation.text_separator'), $text_string);
// Manage the [extension].
$last_text = array_pop($text);
$extension = pathinfo($last_text, PATHINFO_EXTENSION);
if ($extension) {
$text[] = str_replace('.' . $extension, '', pathinfo($last_text, PATHINFO_BASENAME));
}
else {
$this->logger
->error("URL generation - No file extension specified.");
throw new NotFoundHttpException('No file extension specified');
}
// Get the Textimage URI.
$file_uri = 'public://textimage/' . $image_style
->id() . '/' . $text_string;
try {
$image_uri = $this->textimageFactory
->get()
->setStyle($image_style)
->setTargetUri($file_uri)
->process($text)
->buildImage()
->getUri();
return $this
->returnBinary($request, $image_uri);
} catch (TextimageException $e) {
$this->logger
->error("URL generation - Failed to build an image at '%file_uri'.", [
'%file_uri' => $file_uri,
]);
throw new NotFoundHttpException('Image not found');
}
}