I wanna to output a barcode image stream. My action code as follow:
public function barcodeAction()
{
$barcode = $this->getRequest()->getQuery('barcode','JOKE1234');
$barcodeOptions = ['text' => $barcode,'barHeight' => 70];
$rendererOptions = [];
return Barcode::render(
'code39',
'image',
$barcodeOptions,
$rendererOptions
);
}
The barcode picture show well, it seems work good, I did not use a code reader to test.
How to resolve this?
Try to call the URL for the barcode in the browser and you will see that an image cannot be the return type of a controller action.
It’s still a 500 error.
Btw. do you use the controller plugin AcceptableViewModelSelector
?
No, there is no special acceptableViewModelSelector setted in my code.
You must create a correct response, the barcode renderer returns an image in your case which can not be used a return type of a controller action.
Some pseudo code:
public function barcodeAction()
{
$image = Barcode::render(…);
$response = $this->getResponse();
$response->setContent($image);
$response
->getHeaders()
->addHeaderLine('Content-Transfer-Encoding', 'binary')
->addHeaderLine('Content-Type', 'image/png')
->addHeaderLine('Content-Length', mb_strlen($image));
return $response;
}
Thanks for your reply!
Now my action is
public function barcodeAction()
{
$barcode = $this->getRequest()->getQuery('barcode','JOKE1234');
$barcodeOptions = ['text' => $barcode,'barHeight' => 70];
$rendererOptions = [];
$image = Barcode::render(
'code39',
'image',
$barcodeOptions,
$rendererOptions
);
$response = $this->getResponse();
$response->setContent($image);
$response
->getHeaders()
->addHeaderLine('Content-Transfer-Encoding', 'binary')
->addHeaderLine('Content-Type', 'image/png')
->addHeaderLine('Content-Length', mb_substr($image));
return $response;
}
Please check your server logs, the error must be listed there.
jobsfan
9
yesterday, I mistaken the mb_strlen to mb_substr, however in this case, the barcode picture shows great.
Just now, I corrected it from mb_substr to mb_strlen, then nothing shows!!! no picture at all.
I checked over the /var/log/nginx/error.xxx.log, nothing related found!!!
You can check if the alternative works: strlen
PHP basics: error reporting! 
jobsfan
11
not work when I change mb_strlen to strlen
I am working on the php error log, I am not familar with linux system, I need time
jobsfan
12
I run the project locallly with php -S 0.0.0.0:8080 -t public
the 500 server error here
[Thu Sep 2 11:49:33 2021] 127.0.0.1:58986 [500]: GET /application/outcome/barcode?barcode=2021090214
no further detail infomartion can refer.