在PHP中显示生成的PNG?

时间:2018-07-06 20:38:55

标签: php qr-code barcode

因此,我试图获取生成的UPC条形码以显示在索引页面上。但是它所做的只是输出PNG内容,而不是显示PNG本身。 我不太确定为什么要这么做。我猜想我需要添加一些愚蠢的小东西,但是我不知道它会是什么。因此,任何帮助将不胜感激。

索引代码

    <form method="POST">
<head>UPC Barcode and QRcode Generator</head><br>
<label for="">Type 11 Didgits Here => </label>
<input type="text" class="form-control" name="text_code">
<button type="submit" class="btn btn-primary" name="generate">Generate</button>
<hr>
<?php
//QR CODE
if(isset($_POST['generate'])){
$code = $_POST['text_code'];
echo "<img src='https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=$code&choe=UTF-8'>";}
//Barcode
include "generate.php";
$upc = new BC(null,4);
$number = $_POST['text_code'];
$upc->build($number);
echo "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
?>

生成代码

<?php
class BC{//
    private $height;
    private $width;
    private $barheight;
    private $barwidth;
    private $lable;
    private $border;
    private $padding;
    private $mapcode;
    private $rightmap;
    private $code;
    function __construct($lable=null,$barwidth=2) {//
        $this->set_width($barwidth);
        $this->set_lable($lable);
        $this->padding = $barwidth*5;
        $this->border =2;
        $this->mapcode = array
        ('0' => '0001101', '1' => '0011001', '2' => '0010011', '3' => '0111101', '4' => '0100011',
         '5' => '0110001', '6' => '0101111', '7' => '0111011', '8' => '0110111', '9' => '0001011',
         '#' => '01010', '*' => '101');
        $this->rightmap = array
        ('0' => '1110010', '1' => '1100110', '2' => '1101100', '3' => '1000010', '4' => '1011100',
         '5' => '1001110', '6' => '1010000', '7' => '1000100', '8' => '1001000', '9' => '1110100',
         '#' => '01010', '*' => '101');
    }
public function set_width($barwidth) {//
        if(is_int($barwidth)) {//
            $this->barwidth = $barwidth;
        }
        else{//
            $this->barwidth = 2;
        }
    }
public function set_lable($lable) {//
        if(is_null($lable)) {//
            $this->lable = "none";
        }
        else{//
            $this->lable = $lable;
            }
        }
public function build($code = null) {//
        if(is_null($code)) {//
            $this->code = "00000000000";
        }
        else{//
            $this->code = $code;
            }

        $this-> code = substr($code, 0, 11);

        if(is_numeric($code)){//
            $checksum = 0;
            for($digit = 0; $digit < strlen($code); $digit++) {
                if($digit%2 == 0) {//
                    $checksum += (int)$code[$digit] * 3;
                }
                else{//
                    $checksum += (int) $code[$digit];
                }
            }
            $checkdigit = 10 - $checksum % 10;
            $code .= $checkdigit;
            $code_disp = $code;
            $code = "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
            $this->width = $this-> barwidth*95 + $this->padding*2;
            $this->barheight = $this->barwidth*95*0.75;
            $this->height = $this->barwidth*95*0.75 + $this->padding*2;
            $barcode = imagecreatetruecolor($this->width, $this->barheight);
            $black = imagecolorallocate($barcode, 0, 0, 0);
            $white = imagecolorallocate($barcode, 255, 255, 255);
            imagefill($barcode, 0, 0, $black);
            imagefilledrectangle($barcode, $this->border, $this->width - $this->border, -1, $this->barheight - $this->border - 1, $white);
            $bar_pos = $this->padding;
            for($count = 0; $count < 15; $count++) {
                $character = $code [$count];
                for($subcount = 0; $subcount < strlen($this->mapcode[$character]); $subcount++) {//
                    if($count < 7) {
                        $color = ($this->mapcode[$character][$subcount] == 0) ? $white : $black;
                        }
                        else{
                        $color = ($this->rightmap[$character][$subcount] == 0) ? $white : $black;
                        }
                    if(strlen($this->mapcode[$character]) == 7) {
                        $height_offset = $this->height * 0.05;
                    }
                    else {
                        $height_offset = 0;
                        }
                        imagefilledrectangle($barcode, $bar_pos, $this->padding, $bar_pos+$this->barwidth - 1, $this->barheight - $height_offset - $this->padding, $color);
                        $bar_pos += $this->barwidth;
                        }
                        imagepng($barcode);
                    }
                }
            }
        }
?>

这就是输出

It just displays in some weird text

添加新代码后

Here is the output now

2 个答案:

答案 0 :(得分:2)

要在HTML页面中显示图像,您需要使用<img />标签。要在<img />标签中显示图像内容,您需要使用Data URI Scheme

您最终会得到这样的东西:

echo '<img src="data:image/png;base64,', base64_encode($the_png_contents), '" />';

答案 1 :(得分:0)

我检查了您的问题,并在您的评论中说它仍然不起作用,所以我决定与您一起看看。我在发布时使用了您的代码,并在我自己的小环境中使用了它以使其正常工作。我所做的是:

我在您的index.php中所做的更改:

// Check if request method is post
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Check if both Generate and Text_code have been set
    if(isset($_POST['generate']) && isset($_POST['text_code'])) {

        // Set code used through the if statement
        $code = $_POST['text_code'];
        echo '<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl='. $code . '&choe=UTF-8">';

        // Include file; generate.php
        include "generate.php";

        // New instance of class; BC
        $upc = new BC(null, 4);

        // Set barcode base64 to variable and display
        $barcodeBase64 = $upc->build($code);
        echo '<img src="data:image/png;base64,' . base64_encode($barcodeBase64) . '" />';
    }
}

此外,您的generate.php需要在功能构建中进行一些更改:

public function build($code = null) {
    $this->code = is_null($code) ? "00000000000" : $code;

    $code = substr($code, 0, 11);
    $code = str_pad($code,  11, "00000000000");

    if(is_numeric($code)){//
        $checksum = 0;
        for($digit = 0; $digit < strlen($code); $digit++) {
            if($digit%2 == 0) {//
                $checksum += (int)$code[$digit] * 3;
            }
            else{//
                $checksum += (int) $code[$digit];
            }
        }
        $checkdigit = 10 - $checksum % 10;
        $code .= $checkdigit;
        $code_disp = $code;
        $code = "*".substr($code, 0,6)."#".substr($code, 6,6)."*";
        $this->width = $this-> barwidth*95 + $this->padding*2;
        $this->barheight = $this->barwidth*95*0.75;
        $this->height = $this->barwidth*95*0.75 + $this->padding*2;
        $barcode = imagecreatetruecolor($this->width, $this->barheight);
        $black = imagecolorallocate($barcode, 0, 0, 0);
        $white = imagecolorallocate($barcode, 255, 255, 255);
        imagefill($barcode, 0, 0, $black);
        imagefilledrectangle($barcode, $this->border, $this->width - $this->border, -1, $this->barheight - $this->border - 1, $white);
        $bar_pos = $this->padding;
        for($count = 0; $count < 15; $count++) {
            $character = $code[$count];
            for($subcount = 0; $subcount < strlen($this->mapcode[$character]); $subcount++) {//
                if($count < 7) {
                    $color = ($this->mapcode[$character][$subcount] == 0) ? $white : $black;
                }
                else{
                    $color = ($this->rightmap[$character][$subcount] == 0) ? $white : $black;
                }
                if(strlen($this->mapcode[$character]) == 7) {
                    $height_offset = $this->height * 0.05;
                }
                else {
                    $height_offset = 0;
                }
                imagefilledrectangle($barcode, $bar_pos, $this->padding, $bar_pos+$this->barwidth - 1, $this->barheight - $height_offset - $this->padding, $color);
                $bar_pos += $this->barwidth;
            }

        }

        ob_start();
        imagepng($barcode);
        $barcodeImage = ob_get_contents();
        ob_clean();

        return $barcodeImage;
    }
}

我还看到您用黑色填充图像,通常用白色填充,因为代码是黑白图像。要更改此设置,只需替换:

imagefill($barcode, 0, 0, $black); > imagefill($barcode, 0, 0, $white);