Symfony2:使用GD时图像中的空格

时间:2013-03-12 22:18:08

标签: php symfony gd

我目前正在开展一个项目,我必须渲染条形码来打印它们。为此,我使用我上网的代码,如果我在一个单独的文件中使用它,它可以正常工作。唯一的问题是我必须使用它与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;
}

提前致谢。

<小时/> 修改: 问题已经解决了。 为此,我要感谢DiegoAgulló和hacfi。

当然,我在发布之前检查过,如果在包含该功能的php文件中有任何前导空格,那么我无法找出问题的根源。 hacfi的最后一条消息让我意识到这些空间可能来自在当前文件之前发送的文件。 我在用于验证用户身份的文件(验证提供者和监听者)中发现了换行符。

所以,再次,谢谢你。

1 个答案:

答案 0 :(得分: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);