【问题标题】:imagestring can not write barcode stringimagestring 无法写入条形码字符串
【发布时间】:2021-10-27 13:49:24
【问题描述】:

我有一个条形码生成器 PHP 代码。它工作得很好,并在浏览器中显示条形码。但是当我想通过imagestring将其转换为PNG时,图像不包含条形码!我还使用echo gettype($i) 知道什么是字符串变量的类型,这表明它是字符串。我使用的条形码生成器如下。

<style>
div.b128{
 border-left: 1px black solid;
 height: 30px;
} 
</style>
<?php
global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; 
$char128wid = array(
 '212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9 
 '221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19 
 '221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29 
 '212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39 
 '231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49 
 '231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59 
 '314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69 
 '112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79 
 '111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89 
 '214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
 '114131','311141','411131','211412','211214','211232','23311120' ); // 100-106

////Define Function
function bar128($text) { // Part 1, make list of widths
 global $char128asc,$char128wid; 
 $w = $char128wid[$sum = 104]; // START symbol
 $onChar=1;
 for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
 if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
 $w.= $char128wid[$pos];
 $sum += $onChar++ * $pos;
 } 
 $w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
 //Part 2, Write rows
 $html="<table cellpadding=0 cellspacing=0><tr>"; 
 for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
 $html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>"; 
 return "<tr><td colspan=".strlen($w)." align=center><font family=arial size=2>$text</td></tr></table>$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=2>$text</td></tr></table>"; 
}
?>

我使用下面的代码生成条形码并转换为 PNG:

<?php

    $code='ab1234cd';
    include 'barcode128.php';
    $bar_code=bar128($code);

$font  = 20;
$text=$bar_code;
$width  = imagefontwidth($font) * strlen($text);
$height = imagefontheight($font);

// Create a 100*30 image
$im = imagecreate($width, $height);

// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write the string at the top left
imagestring($im, $font, 0, 0, $text, $textcolor);

// Output the image
#header('Content-type: image/png');
imagepng($im, 'f.png');
imagedestroy($im);
?>

我认为问题在于条形码生成器生成的字符串,因为 PNG 文件包含条形码生成器代码的最后一行!!!看附图。

如您所见,它是return 之后的条形码生成器的最后一行

【问题讨论】:

  • 您不能将 HTML 代码写到图像上,并期望得到 解释
  • @CBroe 我不明白你的意思是什么 HTML 代码。当我echo 字符串$text 它被清楚地显示并且是一个字符串。你能解释一下吗?我还使用strval($bar_code); 将任何类型的变量更改为字符串,但结果相同
  • 你的函数返回的 HTML 代码,return "&lt;tr&gt;&lt;td colspan="... “当我回显字符串 $text 时,它清楚地显示并且是一个字符串。” - 因为那时你的浏览器,这是期望 HTML,解释该HTML代码。但这并不意味着您的字符串不包含 HTML 标记,您只是无法在该上下文中看到它们
  • @CBroe 我有点理解,那么用我的代码解决这个问题的解决方案是什么?
  • 我尝试使用ob_start(); echo $bar_code; $content = ob_get_clean(); ,但也没有用。

标签: php barcode


【解决方案1】:

如果它只是关于生成条形码和保存生成的图像,那你为什么不试试this

该示例非常易于解释,代码是开源的,因此欢迎您进行自定义。 :D

<?php
    include('../src/Code128Barcode.php');
    ob_start();
    imagepng(Code128Barcode::generate('some text'));
    $img = base64_encode(ob_get_clean());    
    echo '<img src="data:image/png;base64,' . $img . '" />'; //render barcode image in page
    file_put_contents('barcode.png', base64_decode($img)); //save barcode as image file
?>

在对您的代码进行多次实验后,我得出以下结果

  1. 您可以使用纯 PHP 在服务器端将条形码转换为图像 使用工具,例如wkhtmltoimage 示例如下

    wkhtmltoimage http://localhost/barcode.php?q=my%20text test.png
    
  2. 您可以使用在客户端将条形码转换为图像 JavaScript 和其他一些画布处理工具我修改了你的 编写代码并使用 html canvas 将您的条形码转换为 PNG 图像 和 html 到画布渲染库可用here

修改后的代码如下

barcode.php

此文件中只有很小的更改从此处删除了样式标签并将其添加到旁边列出的索引(消费者文件)中

<?php
global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; 
$char128wid = array(
 '212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9 
 '221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19 
 '221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29 
 '212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39 
 '231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49 
 '231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59 
 '314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69 
 '112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79 
 '111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89 
 '214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
 '114131','311141','411131','211412','211214','211232','23311120' ); // 100-106

////Define Function
function bar128($text) { // Part 1, make list of widths
 global $char128asc,$char128wid; 
 $w = $char128wid[$sum = 104]; // START symbol
 $onChar=1;
 for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
 if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
 $w.= $char128wid[$pos];
 $sum += $onChar++ * $pos;
 } 
 $w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
 //Part 2, Write rows
 $html="<table cellpadding=0 cellspacing=0><tr>"; 
 for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
 $html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>"; 
 return "<tr><td colspan=".strlen($w)." align=center><font family=arial size=2>$text</td></tr></table>$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=2>$text</td></tr></table>"; 
}
?>

index.php

消费者文件,我添加了一些 html 和 javascript 以使其工作

<?php include('barcode.php'); ?>

<html>
<head>
    <title>Barcode Test</title>
    <style>
        div.b128{
            border-left: 1px black solid;
            height: 30px;
        } 
    </style>
    <script src="./htmltocanvas.js"></script>
</head>
<body>
<form method="GET" action="#">
<input type="text" value="<?php echo $_GET['q']; ?>" name="q">
<input type="submit" value="Update Text">
<input onclick="SaveImage()" type="button" value="Save Image">
</form>
<label>Actual Barcode Rendered</label>
<div id="capture" style="padding: 10px;">
    <?php echo bar128($_GET['q']); ?>
</div>

<script>
function SaveImage()
{
    html2canvas(document.querySelector("#capture")).then(canvas => {
        var newlabel = document.createElement("Label");
        newlabel.innerHTML = "Barcode after Converting to Image";
        document.body.appendChild(newlabel);
        document.body.appendChild(canvas)
    });
}
</script>

</body>
</html>

输出如下,一旦将画布附加到 html,您就可以轻松地从画布下载、处理或发送 png 图像到服务器。

【讨论】:

  • 谢谢@Zain,这是一个很好的建议,但我真的很想解决这个问题并学习。 :)
  • @SaeedEisakhani 检查我的更新答案
  • 谢谢@Zain 我不喜欢使用库,因为当我将代码移动到另一台服务器时,我遇到了严重的问题。有没有办法将echo 输出保存到变量中?或任何其他方式将打印在浏览器页面中的条形码转换为可识别的变量!!!
  • 你不能,因为你的条形码没有被生成为图像,该函数正在生成一个伪装成图像的 html 代码,你必须找到一种方法来转换你的 html 代码的呈现要图像,没有任何库是不可能的,但是我仍在尝试纯 js 或 php 代码
  • 如何将条码生成器更改为不将条码生成为 HTML 代码?
猜你喜欢
  • 2015-12-16
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2017-03-05
  • 2016-08-29
相关资源
最近更新 更多