2014-09-29 63 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