Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
endroid_qr_code
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
endroid_qr_code
Merge requests
!5
Issue
#3238336
: Cache generated QR codes
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3238336
: Cache generated QR codes
issue/endroid_qr_code-3238336:3238336-cache-generated-qr
into
3.0.x
Overview
0
Commits
1
Pipelines
0
Changes
1
Open
Eric Sembrat
requested to merge
issue/endroid_qr_code-3238336:3238336-cache-generated-qr
into
3.0.x
3 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Compare
3.0.x
3.0.x (HEAD)
and
latest version
latest version
8e8da70c
1 commit,
3 years ago
1 file
+
96
−
26
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/Response/QRImageResponse.php
+
96
−
26
Options
@@ -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