php generates two dimensional code without saving the server and downloading the implementation code of the function

  • 2021-11-01 02:23:18
  • OfStack

There is a big hole: QRimage:: png redefines header to header in the phpqrcode class ('content-type: img/png')

This header header can't parse the html code, so the code that is directly a 2-dimensional code after returning can't play any role, so it is necessary to re-declare that header under 1 is header ('Content-Type: text/html; charset=utf-8 '); I am famous in the following code, and the red one is

1. Introducing phpqrcode class (php operation 2D code class)

2. Call the encodePNG method

3. Modify encodePNG method (add ob cache, return) base64 encrypted picture stream

4. Return the encrypted picture stream to the front end

5. Front end output through img tag

6. Download function: hyperlink directly plus download= "picture name"

The code for calling phpqrcode is as follows


/**
 *  Activities 2 Dimension code generation 
 * @param $text  Parameter $text Representation generation 2 Information text of bits; 
 * @param $outfile  Parameter $outfile Indicates whether to output 2 Dimension code picture   File, default no; 
 * @param string $level  Parameter $level Indicates the fault tolerance rate, that is, the covered areas can still be identified, which are  L ( QR_ECLEVEL_L , 7% ), M ( QR_ECLEVEL_M , 15% ), Q ( QR_ECLEVEL_Q , 25% ), H ( QR_ECLEVEL_H , 30% ); 
 * @param int $size  Parameter $size Represents the generated picture size, and the default is 3 ; Parameter $margin Denote 2 Spacing value of blank area of border around dimension code; 
 * @param int $margin
 * @param bool $saveandprint  Parameter $saveandprint Indicates whether to save 2 Dimension code and display. 
 */
include_once(ROOT_PATH . '/protected/components/phpqrcode.php');
$link = LinkUrlHelper::getUrl(Yii::app()->user->eid,'ACTIVE_DETAIL',$id);
$h5Url = $link['h5'];
$baseImg = QRimage::encodePNG($h5Url,false);
$this->renderPartial('training_code',['img'=>$baseImg]);

Modified encodePNG method


public function encodePNG($intext, $outfile = false,$saveandprint=false) 
{
  try {
    ob_start();
    $tab = $this->encode($intext);
    $err = ob_get_contents();
    ob_end_clean();
    if ($err != '')
      QRtools::log($outfile, $err);
    $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
    ob_start();// Open ob Cache 
    QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
    $imageString = base64_encode(ob_get_contents());// Save the picture stream in the cache and encrypt it and assign it to the variable 
    ob_end_clean();// Clear ob Cache 
    header('Content-Type:text/html;charset=utf-8');// QRimage::png  Overrides this header Head, so you have to re-declare 1 Otherwise, it can't be resolved html Code. 
    return "data:image/png;base64,".$imageString;// Returns the spliced string 
  } catch (Exception $e) {
    QRtools::log($outfile, $e->getMessage());
  }
}

Summarize


Related articles: