3

I use this codes for making qr code with custom text below. I tried several methods but I always failed. I need to 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 blank page.

eneskomur
  • 91
  • 1
  • 13
  • ``imagecreatefrompng`` says "A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename.". An alternative may be to use ``$qr_img_as_string = file_get_contents...`` and use ``imagecreatefromstring``. – alistaircol Jul 06 '17 at 15:51
  • You can check using ``var_dump((bool) ini_get('allow_url_fopen'));`` – alistaircol Jul 06 '17 at 15:54

2 Answers2

2

You've got an error, but you can't view it in browser because you've set the Content-Type: image/png so for debugging just comment out that line or check your server logs.

First thing I'm reminded of is this answer using a relative path to your font. This is enough to throw a warning that if output to screen would garble your image, and not to mention you won't have the font you needed. I would fix this line.

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

For me, the fatal error was:

Call to undefined function imagecopymerge_alpha()

I'm not sure where you got that 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 the color white was labeled as black and set as both the background and text color - so regardless of which color it was, it wouldn't be visible. So I changed these lines. (- means deleted line and + means added line.)

 // 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 set that 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);
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
0

You can create text as simply , create the qr code first and add the text in that image. see my codes.

Download the qr code generator library from http://phpqrcode.sourceforge.net/ https://sourceforge.net/projects/phpqrcode/

include 'phpqrcode/qrlib.php';
$path = 'qr_codes/';
        $file = $path."myimage.".png";
        $ecc = 'L';
        $pixel_Size = 10;
        $frame_Size = 7;
        $text = "hello";
        $text_down = "Scan me";
    
        QRcode::png($text, $file, $ecc, $pixel_Size, $frame_Size);// create the qr code with data hello.
    
        $white = imagecolorallocate($im, 255, 255, 255);
        $black = imagecolorallocate($im, 0, 0, 0);
        
    
        
        $image = imagecreatefrompng($file);// from that qr image create new image for insert the text
        
        $newfile = "qr_codes/".$text.".png";
    
        $white = imagecolorallocate($image, 255, 255, 255);
        $black = imagecolorallocate($image, 0, 0, 0);
        $color = imagecolorallocate($image, 255, 255, 255);
    
        imagefill( $image, 0, 0, $white);
        
        imagestring($image, 3, 90, $img_height=290, $text_down, $black );// insert the text with positioning as x, y z parameters
        imagepng($image,$newfile);// new image created
        imagedestroy($image);
ALEESHA
  • 1
  • 2