2016-02-23 80 views
0

我有這個代碼,它會在屏幕上生成條形碼。但它太小而不能打印。所以,我想縮放尺寸,但爲什麼屏幕上不顯示圖像?只有$ image顯示(原始圖像)。我在這裏錯過了什麼?謝謝調整GD生成的圖像的大小,但失敗

<?php 
    //-- bunch of codes here -- 
    //-- then generate image -- 

    // Draw barcode to the screen 
    header ('Content-type: image/png'); 
    imagepng($image); 
    imagedestroy($image); 

    // Then resize it 
    // Get new sizes 
    list($width, $height) = getimagesize($image); 
    $newwidth = $width * 2; 
    $newheight = $height * 2; 

    // Resize 
    imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    // Output and free memory 
    header ('Content-type: image/png'); 
    imagepng($thumb); 
    imagedestroy($thumb); 
?> 
+1

你打電話'imagedestroy($圖像)'並繼續嘗試讓'$ image' – RamRaider

+0

@RamRaider的特性:除'imagedestroy($圖像);'仍然沒有改變結果,兄弟 –

+0

除了在試圖獲取它的高度和寬度之前銷燬'$ image'之外,您還要輸出'$ image',然後輸出'$ thumb'。選擇一個顯示,而不是在同一個文件中都不起作用。 – Kalkran

回答

2

雖然沒有所有的代碼工作與我模擬部件 - 最終的結果是一個新的形象,是原來的兩倍。最主要的不是最初發送標題,而是將生成的圖像保存到臨時文件,然後處理。

<?php 
    //-- bunch of codes here -- 
    //-- then generate image -- 


    /* You will already have a resource $image, this emulates that $image so you do not need this line */ 
    $image=imagecreatefrompng('c:/wwwroot/images/maintenance.png'); 



    /* define name for temp file */ 
    $tmpimgpath=tempnam(sys_get_temp_dir(), 'img'); 

    /* do not send headers here but save as a temp file */ 
    imagepng($image, $tmpimgpath); 


    // Then resize it, Get new sizes (using the temp file) 
    list($width, $height) = getimagesize($tmpimgpath); 
    $newwidth = $width * 2; 
    $newheight = $height * 2; 

    /* If $thumb is defined outwith the code you posted then you do not need this line either */ 
    $thumb=imagecreatetruecolor($newwidth, $newheight); 

    // Resize 
    imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    // Output and free memory 
    header ('Content-type: image/png'); 
    @imagedestroy($image); 
    @imagepng($thumb); 
    @imagedestroy($thumb); 
    @unlink($tmpimgpath); 
?> 
相關問題