Skip to content
Snippets Groups Projects

Issue #3238336: Cache generated QR codes

1 file
+ 96
26
Compare changes
  • Side-by-side
  • Inline
@@ -4,9 +4,13 @@ namespace Drupal\endroid_qr_code\Response;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;
use Drupal\file\Entity\File;
use Endroid\QrCodeBundle\Response\QrCodeResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\image\Entity\ImageStyle;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\File\FileSystemInterface;
/**
* Response which is returned as the QR code.
@@ -91,33 +95,99 @@ class QRImageResponse extends Response {
* String to be converted to Qr Code.
*/
private function generateQrCode(string $string = '') {
$qrCode = new QrCode($string);
$qrCode->setLogoWidth($this->logoWidth);
$qrCode->setSize($this->logoSize);
$qrCode->setMargin($this->logoMargin);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
$qrCode->setForegroundColor([
'r' => 0,
'g' => 0,
'b' => 0,
'a' => 0,
]);
$qrCode->setBackgroundColor([
'r' => 255,
'g' => 255,
'b' => 255,
'a' => 0,
]);
$qrCode->setRoundBlockSize(TRUE);
$qrCode->setValidateResult(FALSE);
$response = new QrCodeResponse($qrCode);
if ($response->isOk()) {
$im = imagecreatefromstring($response->getContent());
ob_start();
imagejpeg($im);
imagedestroy($im);
// if we somehow queried this without a string, return null.
if(empty($string)){
return null;
}
// Theory craft if the file has already been generated
$uri_tentative = $this->getQrFilename($this->cleanQrFilename($string));
// Check if file exists.
if (file_exists($uri_tentative)) {
$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri_tentative]);
foreach($files as $file){
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager')->getViaUri($file->getFileUri());
ob_start();
$img = imagecreatefromjpeg($stream_wrapper_manager->realpath());
imagejpeg($img);
imagedestroy($img);
}
}
else {
$qrCode = new QrCode($string);
$qrCode->setSize($this->logoSize);
$qrCode->setMargin($this->logoMargin);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
$qrCode->setForegroundColor([
'r' => 0,
'g' => 0,
'b' => 0,
'a' => 0,
]);
$qrCode->setBackgroundColor([
'r' => 255,
'g' => 255,
'b' => 255,
'a' => 0,
]);
$qrCode->setRoundBlockSize(TRUE);
$qrCode->setValidateResult(FALSE);
$response = new QrCodeResponse($qrCode);
if ($response->isOk()) {
$this->storeQrCode($response->getContent(),$string);
$im = imagecreatefromstring($response->getContent());
ob_start();
imagejpeg($im);
imagedestroy($im);
}
}
}
/**
* Undocumented function
*
* @param string $data_string
* @return void
*/
public function storeQrFilename(string $data_string_transliterated){
# Now, writing to correct file.
$scheme = \Drupal::config('system.file')->get('default_scheme');
// Forma
$file_location = \Drupal::service('file_system')->createFilename($data_string_transliterated . '.jpg', $scheme . '://');
return $file_location;
}
public function cleanQrFilename(string $data_string){
# Preparing string
$data_string_transliterated = \Drupal::transliteration()->transliterate($data_string, LanguageInterface::LANGCODE_DEFAULT, '_');
$data_string_transliterated = mb_strtolower($data_string_transliterated);
$data_string_transliterated = preg_replace('@[^a-z0-9_.]+@', '_', $data_string_transliterated);
return $data_string_transliterated;
}
public function getQrFilename(string $data_string_transliterated){
$scheme = \Drupal::config('system.file')->get('default_scheme');
$file_location = \Drupal::service('file_system')->getDestinationFilename($scheme . "://" . $data_string_transliterated . '.jpg', FileSystemInterface::EXISTS_REPLACE);
return $file_location;
}
/**
* Undocumented function
*
* @param string $data_qr
* @param string $data_string
* @return void
*/
public function storeQrCode(string $data_qr, string $data_string){
$file_location = $this->getQrFilename($this->cleanQrFilename($data_string));
# Create file
$image = imagecreatefromstring($data_qr);
imagejpeg($image, $file_location);
$file_data = file_get_contents($file_location);
$file_file = file_save_data($file_data, $file_location, FileSystemInterface::EXISTS_REPLACE);
}
}
Loading