1

我目前正在从事一个项目,我必须渲染条形码才能打印它们。为此,我使用了我在网上获得的代码,如果我在单独的文件中使用它,它就可以正常工作。唯一的问题是我必须在 symfony 2.1 中使用它。

该脚本使用 GD 绘制条码。问题是当我尝试使用 imagepng 函数显示图像时,出现错误。在尝试将 png 作为附件文件下载后,我注意到 png 文件中的二进制数据之前有 4 个空格。它是这样开始的:

    ‰PNG

如果我手动删除空格,则会显示图像,但我需要直接在浏览器中显示 png。

您是否知道为什么将空格添加到图像文件中?

这是用于生成 png 的代码。

 public function barcodeAction($text) {
    $response = new Response();
    $response->headers->set('Content-Type', 'image/png');
    $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s.png"', $text));
    $size = "40";
    $code_string = "";

    $chksum = 104;

    $code_array = array(" " => "212222", "!" => "222122", "\"" => "222221", "#" => "121223", "$" => "121322", "%" => "131222", "&" => "122213", "'" => "122312", "(" => "132212", ")" => "221213", "*" => "221312", "+" => "231212", "," => "112232", "-" => "122132", "." => "122231", "/" => "113222", "0" => "123122", "1" => "123221", "2" => "223211", "3" => "221132", "4" => "221231", "5" => "213212", "6" => "223112", "7" => "312131", "8" => "311222", "9" => "321122", ":" => "321221", ";" => "312212", "<" => "322112", "=" => "322211", ">" => "212123", "?" => "212321", "@" => "232121", "A" => "111323", "B" => "131123", "C" => "131321", "D" => "112313", "E" => "132113", "F" => "132311", "G" => "211313", "H" => "231113", "I" => "231311", "J" => "112133", "K" => "112331", "L" => "132131", "M" => "113123", "N" => "113321", "O" => "133121", "P" => "313121", "Q" => "211331", "R" => "231131", "S" => "213113", "T" => "213311", "U" => "213131", "V" => "311123", "W" => "311321", "X" => "331121", "Y" => "312113", "Z" => "312311", "[" => "332111", "\\" => "314111", "]" => "221411", "^" => "431111", "_" => "111224", "\`" => "111422", "a" => "121124", "b" => "121421", "c" => "141122", "d" => "141221", "e" => "112214", "f" => "112412", "g" => "122114", "h" => "122411", "i" => "142112", "j" => "142211", "k" => "241211", "l" => "221114", "m" => "413111", "n" => "241112", "o" => "134111", "p" => "111242", "q" => "121142", "r" => "121241", "s" => "114212", "t" => "124112", "u" => "124211", "v" => "411212", "w" => "421112", "x" => "421211", "y" => "212141", "z" => "214121", "{" => "412121", "|" => "111143", "}" => "111341", "~" => "131141", "DEL" => "114113", "FNC 3" => "114311", "FNC 2" => "411113", "SHIFT" => "411311", "CODE C" => "113141", "FNC 4" => "114131", "CODE A" => "311141", "FNC 1" => "411131", "Start A" => "211412", "Start B" => "211214", "Start C" => "211232", "Stop" => "2331112");
    $code_keys = array_keys($code_array);
    $code_values = array_flip($code_keys);
    for ($X = 1; $X <= strlen($text); $X++) {
        $activeKey = substr($text, ($X - 1), 1);
        $code_string .= $code_array[$activeKey];
        $chksum = ($chksum + ($code_values[$activeKey] * $X));
    }
    $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];

    $code_string = "211214" . $code_string . "2331112";

    // Pad the edges of the barcode
    $code_length = 10;
    for ($i = 1; $i <= strlen($code_string); $i++) {
        $code_length = $code_length + (integer) (substr($code_string, ($i - 1), 1));
    }
    $img_width = $code_length;
    $img_height = $size;

    $image = imagecreate($img_width + 10, $img_height);
    $black = imagecolorallocate($image, 0, 0, 0);
    $white = imagecolorallocate($image, 255, 255, 255);

    imagefill($image, 0, 0, $white);

    $location = 10;
    for ($position = 1; $position <= strlen($code_string); $position++) {
        $cur_size = $location + (substr($code_string, ($position - 1), 1) );
        imagefilledrectangle($image, $location, 0, $cur_size, $img_height, ($position % 2 == 0 ? $white : $black));
        $location = $cur_size;
    }
    imagepng($image);
    imagedestroy($image);
    return $response;
}

提前致谢。


编辑: 问题解决了。为此,我要感谢 Diego Agulló 和 hacfi。

当然,我在发布之前检查了包含该函数的 php 文件中是否有任何前导空格,所以我无法找出问题的根源。hacfi 的最后一条消息让我意识到空格可能来自在当前文件之前发送的文件。我在用于验证我的用户(验证提供程序和侦听器)的文件中发现了换行符。

所以,再一次,谢谢你。

4

1 回答 1

1

您的代码在我的服务器上运行良好(OS X,nginx 1.2.7 和 php-fpm 5.4.11)。前导空格必须来自控制器之前包含/加载的文件。

正确地将图像作为 Symfony\Component\HttpFoundation\Response 替换

imagepng($image);

ob_start();
imagepng($image);
$imagevariable = ob_get_contents();
ob_end_clean();
$response->setContent($imagevariable);

于 2013-03-13T02:00:30.897 回答