Unable to create QR with below text

I am using these codes to generate qr code with custom text below. I've tried several methods, but I always fail. I need help.

// Set the content-type
header('Content-Type: image/png');
header("Content-Disposition: filename='sample.png'");
$main = imagecreatetruecolor(150, 180);
$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=sample");
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
$black = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 5, 25, $black, $font, 'sample');
imagecopymerge_alpha($main, $qr, 0, 0, 0, 0, 150, 150, 100);
imagecopymerge_alpha($main, $im, 0, 150, 0, 0, 150, 30, 100);
imagepng($main);
imagedestroy($main);

      

And I just see a blank page.

+3


source to share


1 answer


You have an error, but you cannot view it in the browser because you installed Content-Type: image/png

, so just comment out this line for debugging or check your server logs.

The first thing I was reminded of was this answer using a relative path to your font. This is enough to warn you that if the output on the screen would distort your image, and not to mention that you will not have the required font. I would fix this line .

$font = realpath(__DIR__.'/arial.ttf');

      

Fatal error for me:

Function call undefined Function imagecopymerge_alpha ()

I'm not sure where you got this code, but I found this question , so I assumed it might be related.

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
    // creating a cut resource
    $cut = imagecreatetruecolor($src_w, $src_h);

    // copying relevant section from background to the cut resource
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

    // copying relevant section from watermark to the cut resource
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}

      

Then I noticed that white was marked as black and set both the background and the text color - so no matter what color it was, it won't be visible. So I changed these lines . ( -

means line deleted, and +

means line added.)



 // Create the image
 $im = imagecreatetruecolor(150, 30);
 // Create some colors
-$black = imagecolorallocate($im, 255, 255, 255);
+$white = imagecolorallocate($im, 255, 255, 255);
+$black = imagecolorallocate($im, 0, 0, 0);
 imagefilledrectangle($im, 0, 0, 399, 29, $black);
 // Font path
 $font = realpath(__DIR__.'/arial.ttf');
 // Add the text
-imagettftext($im, 20, 0, 5, 25, $black, $font, 'sample');
+imagettftext($im, 20, 0, 5, 25, $white, $font, 'sample');
 imagecopymerge_alpha($main, $qr, 0, 0, 0, 0, 150, 150, 100);
 imagecopymerge_alpha($main, $im, 0, 150, 0, 0, 150, 30, 100);
 imagepng($main);

      

And finally, instead of hardcoding the word sample

, just for fun I am setting it to the query string.

+$text = $_GET['qr'] ?? 'sample';
+
 // Set the content-type
 header('Content-Type: image/png');
 header("Content-Disposition: filename='sample.png'");
 $main = imagecreatetruecolor(150, 180);
-$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=sample");
+$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=$text");
 // Create the image
 $im = imagecreatetruecolor(150, 30);
 // Create some colors
@@ -14,7 +16,7 @@ imagefilledrectangle($im, 0, 0, 399, 29, $black);
 // Font path
 $font = realpath(__DIR__.'/arial.ttf');
 // Add the text
-imagettftext($im, 20, 0, 5, 25, $white, $font, 'sample');
+imagettftext($im, 20, 0, 5, 25, $white, $font, $text);

      

GET /

sample qr

GET /? qr = hello

hello qr

<?php

$text = $_GET['qr'] ?? 'sample';

// Set the content-type
header('Content-Type: image/png');
header("Content-Disposition: filename='sample.png'");
$main = imagecreatetruecolor(150, 180);
$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=$text");
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = realpath(__DIR__.'/arial.ttf');
// Add the text
imagettftext($im, 20, 0, 5, 25, $white, $font, $text);
imagecopymerge_alpha($main, $qr, 0, 0, 0, 0, 150, 150, 100);
imagecopymerge_alpha($main, $im, 0, 150, 0, 0, 150, 30, 100);
imagepng($main);
imagedestroy($main);

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
    // creating a cut resource
    $cut = imagecreatetruecolor($src_w, $src_h);

    // copying relevant section from background to the cut resource
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);

    // copying relevant section from watermark to the cut resource
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);

    // insert cut resource to destination image
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}

      

+2


source







All Articles