梨条形码2 - 保存到磁盘

时间:2014-09-29 12:36:17

标签: php barcode pear gif disk

我正在使用Pear Barcode2生成一个gif条形码。

目前有效,我可以将图像看作gif图像文件。因此页面不以HTML格式显示,而是作为独立图像显示。

我的代码是:

$small_graph_height = 55;
        $small_graph_width = 1.2;
        $large_graph_height = 55;
        $large_graph_width = 1.14;
        $type = "gif";
        $code = "code39";
        $to_browser = TRUE;

          include_once "application/libraries/Image/Barcode2.php";

        $ticketno='TDN4993';
              $productserial_str = empty($productserial_str) ? ucfirst($ticketno) : $productserial_str;
              $productserial_type = $code;
              $productserial_imgtype = $type;
              $productserial_bSendToBrowser = $to_browser;
              $productserial_height = $large_graph_height;
              $productserial_width = $large_graph_width;

              $productserial_img = Image_Barcode2::draw($productserial_str, $productserial_type, $productserial_imgtype, $productserial_bSendToBrowser, $productserial_height, $productserial_width);

              ob_start();
                imagepng($productserial_img);
                $productserial_imgBase64 = base64_encode(ob_get_contents());
              ob_end_clean();

              imagedestroy($productserial_img);

               $image= '<img class="ProductSerial" src="data:image/' . $productserial_imgtype . ';base64,' . $productserial_imgBase64 . '">';
            echo $image;

我想要做的是将此图像直接保存到服务器硬盘而不是显示给用户。

我曾尝试使用PHP的imagegif,但它不喜欢$ image是字符串格式的事实。

欢迎任何建议。一如既往地谢谢

2 个答案:

答案 0 :(得分:1)

如果您需要将图像保存在磁盘上,则需要使用imgpng()功能。

我从here获得的一个例子。

imagepng($bc->draw($data, $type, 'png', false),'ur_image_location'); 

答案 1 :(得分:0)

$small_graph_height = 55;
$small_graph_width = 1.2;
$large_graph_height = 55;
$large_graph_width = 1.14;
$type = "gif";
$code = "code39";
$to_browser = TRUE;

include_once "application/libraries/Image/Barcode2.php";

$ticketno='TDN4993';
$productserial_str = empty($productserial_str) ? ucfirst($ticketno) : $productserial_str;
$productserial_type = $code;
$productserial_imgtype = $type;
$productserial_bSendToBrowser = $to_browser;
$productserial_height = $large_graph_height;
$productserial_width = $large_graph_width;

$productserial_img = Image_Barcode2::draw($productserial_str, $productserial_type, $productserial_imgtype, $productserial_bSendToBrowser, $productserial_height, $productserial_width);

ob_start();
imagepng($productserial_img);
$png = ob_get_contents();
ob_end_clean();

file_put_contents('barcode.png', $png);

echo 'image saved to file barcode.png';