php - Symfony2 : Spaces in image when using GD

标签 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;
}

提前致谢。


编辑: 问题已经解决了。 为此,我要感谢迭戈·阿古洛 (Diego Agulló) 和 hacfi。

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

再次感谢您。

最佳答案

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

正确返回图像作为 Symfony\Component\HttpFoundation\Response 替换

imagepng($image);

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

关于php - Symfony2 : Spaces in image when using GD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15373346/

相关文章:

php - 如何将 SQL 查询的所有结果存储在多维数组中?

php - Symfony FormType 测试处理 EntityType

php - Symfony 5 如何存储氩气的盐?

php - 保存实体时捕获 Doctrine 异常

php - 良好的图像调整框架

php - 如何从数据库中读取数据更改它们并更新数据库

php - 使用原则存储序列化对象

php - 向图像添加空格并将文件保存到服务器

php - Laravel 使用碳获取当前季度

javascript - 有没有一种方法可以使用网络语言根据用户名生成图像,例如iOS7收藏夹?