2014-09-29 100 views
0

我使用梨条码2生成gif条码。梨条码2 - 保存到磁盘

这个目前的作品,我可以看到图像作为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是字符串格式的事实。

任何建议将受到欢迎。一如既往感谢

回答

1

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

我从here得到的一个例子。

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

谢谢,我没有看到这篇文章,但不能确定如何修改现有的'$ productserial_img = Image_Barcode2 ::平局($ productserial_str,$ productserial_type,$ productserial_imgtype,$ productserial_bSendToBrowser,$ productserial_height,$ productserial_width) ;'给你的答案 – Smudger 2014-09-29 13:27:28

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'; 
+0

谢谢asbb,文件被正确创建,并且当调用php页面时文件正确显示。问题是虽然图像不会从磁盘显示。在记事本中打开图像的错误是:'

A PHP Error was encountered

Severity: Warning

Message: imagegif(): 56 is not a valid Image resource

Filename: controllers/capture.php

Line Number: 2873

' – Smudger 2014-09-29 13:10:36

+0

尝试使用imagepng代替imagegif。 – asbb 2014-09-29 13:18:59

+0

对不起,我真的imagepng intially但同样的错误,所以尝试imagegif。将立即发布imagepng错误。 – Smudger 2014-09-29 13:23:46