php


How to use Endroid\QrCode on Yii2?


I have the following code, the library was installed through composer.
Model:
public function insertQR()
{
$this->qrCode = new QrCode();
$this->qrCode
->setText($this->qr_text)
->setSize(300)
->setPadding(10)
->setErrorCorrection('high')
->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
->setLabel('Scan the code')
->setLabelFontSize(16)
->setImageType(QrCode::IMAGE_TYPE_PNG)
;
}// end function
View:
<?php
// now we can directly output the qrcode
header('Content-Type: '.$model->qrCode->getContentType());
$model->qrCode->render();
?>
However at the moment of rendering the view I got the following raw png instead of the png:
�PNG IHDR#[V�J�!PLTE���������???���___���>�-� pHYs���+�IDATx����n�F�a�� ....
What could be wrong? Thanks.
Set the headers like this:
// At the top.
use yii\web\Response;
// In your function.
$response = Yii::$app->getResponse();
$response->headers->set('Content-Type', $model->qrCode->getContentType());
$response->format = Response::FORMAT_RAW;
$model->qrCode->render();
Yii2 requires the format of the response to be set to raw when returns an image response.

Related Links

Why I can't return a proper json structure?
MySQL getting data that can contain multiple sub data
Spam prevention from Humans/Bots
I want to save file on server directory without prompt screen
How can I add specific css id to parent menu with php or javascript
Injecting a lot of dependencies in my controller
How to prevent php of loading a not requested page
Paypal ipn script not called when refunding
time conversion to float
Getting specific CSV columns or nth values from a PHP array
PHP $_GET from index.php?page= [duplicate]
What is the MySQL datatype SET equivalent in Laravel Schema?
How to assign a number to a variable in PHP
mysql RDBMS, applying foreign key constraints in many tables
Getting relational data into arrays
Retrieving the actual displayed link value from MediaWiki API

Categories

HOME
spring-data
powerbi
yaml
schema.org
barcode-scanner
activex
nsview
odata
swift2
sentry
position
onvif
jpa-criteria
fireloop
watch-os-3
documentum
spring-jpa
cuba-platform
distributed-computing
global-variables
pywinauto
google-tasks-api
pinterest
user-defined-types
exec-maven-plugin
clr
adminlte
amazon-mws
gitkraken
workday
bosun
mime-types
ini
vuforia
prompt
guice
location-services
infopath2010
publishing
quick-nimble
wampsharp
epplus
xvfb
gitolite
react-intl
ipp-protocol
karabiner
renderman
clrs
dojo-build
node.js-connect
fdt
gravatar
ioctl
wepay
grinder
gdl
json-spirit
outlook.com
sqlbase
replicaset
swagger-maven-plugin
google-gdk
angular-amd
ampersand
operands
adhoc-polymorphism
igor
xhprof
android-2.3-gingerbread
stripe.net
cloo
autonumber
hints
pacman
unicoins
semicolon
audiotoolbox
memory-pool
sigar
botnet
ognl
chunking
ninject-interception
eventaggregator
bucket
jquery-effects
chronic

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App