In previous article we already discuss about the qr code generater in laravel. Here in this article we learn how to download the simplesoftwareio simple-qrcode in jpg file.
The QR-Code package gives us the code for downloading the qr-code in png, svg and eps but not in jpg or transparent background, So here we have added the code for download the qr-code in jpg easily.
How to Generate Simple QR-Code in Laravel
For generating qr code in laravel you need to check here : Generate simple-qrcode in Laravel, In this tutorial we explained step by step how to generate or create QR code in laravel application.
For downloading the qr code first we need to add code in blade something like below.
<li>
<form class="form-horizontal" action="{{ route('qrcode.download',['type' => 'jpg'])}}" method="post">
@csrf
<input type='hidden' value="jpg" name="qr_type" />
<input type='hidden' value="{{ 'jpg' }}" name="section" />
<button type="submit" class="align-middle btn btn-outline-primary btn-sm ml-1">
<i class="fas fa-fw fa-download"></i>
JPG
</button>
</form>
</li>
Next Update code in controller for saving qr code in jpg file
public function downloadQRCode(Request $request, $id)
{
$imageName = 'qr-code';
$headers = array('Content-Type' => ['png','svg','eps']);
$type = $request->qr_type == 'jpg' || $request->qr_type == 'transparent' ? 'png' : $request->qr_type;
$image = \QrCode::format($type)
->size(200)->errorCorrection('H')
->generate('codingdriver');
\Storage::disk('public')->put($imageName, $image);
if ($request->qr_type == 'jpg') {
$type = 'jpg';
$image = imagecreatefrompng('storage/'.$imageName);
imagejpeg($image, 'storage/'.$imageName, 100);
imagedestroy($image);
}
return response()->download('storage/'.$imageName, $imageName.'.'.$type, $headers)->deleteFileAfterSend();
}
Download QR Code in Transparent Background of PNG or SVG
If you want to download the QR code in with transparent background such as png or svg then you need to follow the below example:
Add in Blade file code
Add the following code in your blade file:
<form class="form-horizontal" action="{{ route('qrcode.download', [ 'type' => 'png' ])}}" method="post">
@csrf
<input type='hidden' value="transparent" name="qr_type" />
<button type="submit" class="align-middle btn btn-outline-primary btn-sm">
<i class="fas fa-fw fa-download"></i>
PNG
</button>
</form>
Add Code in Controller
Next, you need to update the code in your controller file for download the qr code;
public function downloadQRCode(Request $request, $type)
{
$imageName = 'qr-code';
$headers = array('Content-Type' => ['png','svg','eps']);
$type = $request->qr_type == 'jpg' || $request->qr_type == 'transparent' ? 'png' : $request->qr_type;
$image = \QrCode::format($type)
->size(200)->errorCorrection('H')
->generate('codingdriver');
\Storage::disk('public')->put($imageName, $image);
if ($request->qr_type == 'transparent') {
$this->transparentQRCode($imageName);
}
return response()->download('storage/'.$imageName, $imageName.'.'.$type, $headers)->deleteFileAfterSend();
}
public function transparentQRCode($imageName)
{
$imagick = new Imagick('storage/'.$imageName);
$backgroundColor = "rgb(255, 255, 255)";
$fuzzFactor = 0.1;
$outlineImagick = clone $imagick;
$outlineImagick->transparentPaintImage( // transparentPaintImage() is used to make pixels transparent
$backgroundColor, 0, $fuzzFactor * Imagick::getQuantum(), false
);
$mask = clone $imagick;
$mask->setImageAlphaChannel(zImagick::ALPHACHANNEL_DEACTIVATE); // setImageAlphaChannel() is used to set Opacity / Transparency of an image
$mask->transformImageColorSpace(Imagick::COLORSPACE_GRAY); // transformImageColorSpace() is used to set new color parameter for image
$mask->compositeImage( // compositeImage() is used to make new image
$outlineImagick,
Imagick::COMPOSITE_DSTOUT,
0, 0
);
$mask->negateImage(false); // negateImage() is used to balance color of image
$fillPixelHoles = false;
if ($fillPixelHoles == true) {
$mask->blurimage(0, 1);
$mask->whiteThresholdImage("rgb(10, 10, 10)");
$mask->blurimage(0, 1);
$mask->blackThresholdImage("rgb(255, 55, 55)");
}
$mask->blurimage(0, 1); // blurimage() is used to add blur filter to the image
$contrast = 15;
$midpoint = 0.7 * Imagick::getQuantum();
$mask->sigmoidalContrastImage(true, $contrast, $midpoint); // sigmoidalContrastImage() is used to adjust contrast of an image
$imagick->compositeimage(
$mask,
Imagick::COMPOSITE_COPYOPACITY,
0, 0
);
$canvas = new Imagick();
$canvas->newPseudoImage(
$imagick->getImageWidth(),
$imagick->getImageHeight(),
"xc:none" // it is for transparent background
);
$canvas->compositeimage($imagick, Imagick::COMPOSITE_OVER, 0, 0);
$canvas->setImageFormat('png');
header("Content-Type: image/png");
file_put_contents(public_path('storage/'.$imageName), $canvas->getImageBlob());
}
That’s all the qr code to download in jpg file is over now…. if you have any questions please let me know in contact us page or via facebook etc…
1 thought on “Download Simple QR Code in JPG or Transparent Backgrounnd”