2013-01-22 52 views
0

目前我正在使用Shay Anderson的類生成條形碼(http://www.shayanderson.com/php/php-barcode-generator-class-code-39.htm),而我能夠在瀏覽器中成功地顯示所生成的條碼如下:PHP覆蓋JPEG上的GIF

$bc = new PrintBarcode('DARP CODE'); 
$bc->drawVoucher(); 
$src = $bc->getVoucher(); 

// Output and free from memory 
header('Content-Type: image/jpeg'); 
imagejpeg($src); 

,但現在我想修改我的腳本覆蓋在另一個圖像的頂部的條形碼來創建一個憑證,但我似乎無法得到它工作,我只是在Chrome中得到了破碎的圖像圖標和在控制檯中的以下警告:

Resource interpreted as Document but transferred with MIME type image/jpeg

$bc = new PrintBarcode('DARP CODE'); 
$bc->drawVoucher(); 
$src = $bc->getVoucher(); 

header('Content-Type: image/jpeg'); 

// create actual voucher with barcode overlayed on voucher background 
$bg = imagecreatefromjpeg('images/voucher.jpg'); 

imagecopymerge($bg, $src, 0, 0, 0, 0, imagesx($bg), imagesy($bg), 75); 

imagejpeg($bg, null, 100); 

imagedestroy($bg); 

錯誤報告已打開,我沒有收到通知,通知或致命錯誤。任何幫助讚賞。

我能想到的唯一的事情就是從條形碼類的文檔中,它生成條形碼作爲gif,所以不知道我是否缺少幾個步驟。

+0

你可以看到標題發送?你的瀏覽器可能有一個插件。也許有錯誤發生,因此確實發送了錯誤的標題或其他內容。 –

+0

'將資源解釋爲文檔,但使用MIME類型image/jpeg進行傳輸意味着您要在圖像輸出之前輸出非圖像數據的內容 - 可能是某些雜散HTML或輸出內容。 'var_dump()',但可能只是空白。從URL下載文件,將其保存到硬盤驅動器並檢查它的字節,你會在文件的開頭找到不應該在那裏的數據。一旦你知道哪些輸出不應該被創建,你只需要瀏覽代碼,直到你找到它。 – DaveRandom

+0

在我的主要應用程序中,我將通過AJAX生成它,因此只檢查了響應頭,它說'Content-Type:text/html',但不知道這是問題,因爲當我只是做條形碼本身時,它是相同的頭和它作品,我認爲這可能是因爲我的ajax請求中的dataype參數是'html'。 – martincarlin87

回答

0

原來問題出在我使用的圖像作爲合併的基礎並不完全正確,所以我重新將其從png轉換爲jpg(第一次我下載了png,一個文件保存爲JPEG格式)使用Photoshop和它很好,澄清,這裏是代碼:

$bc = new PrintBarcode('DARP CODE'); 
$bc->drawVoucher(); 
$src = $bc->getVoucher(); 

header('Content-Type: image/jpeg'); 

$bg = imagecreatefromjpeg('images/voucher.jpg'); 

imagecopymerge($bg, $src, 40, 380, 0, 0, imagesx($bg), imagesy($bg), 100); 

imagejpeg($bg, null, 100); 

imagedestroy($src); 
imagedestroy($bg);