2017-10-14 72 views
1

我的意圖是將生成的qr代碼圖像保存到本地。PHP將png文件保存爲0字節到磁盤

我已經簽出了關於它的整個stackoverflow問題。但是他們並沒有幫我解決這個錯誤。

<?php 
    header('Content-type: image/png'); 

    $filename = "./qrs/qr-6234/qr.png"; 
    $link = "https://stackoverflow.com"; 
    $size = 250; 
    $url = urlencode ($link); 
    $qr_url = "http://chart.googleapis.com/chart?chs=$sizex$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8"; 
    $qr = file_get_contents($qr_url); 
    $imgIn = imagecreatefrompng ($qr); 
    $imgOut = imagecreate ($size, $size); 
    imagecopy ($imgOut, $imgIn, 0, 0, 0, 0, $size, $size); 
    imagepng ($imgOut, $filename, 9); 
    imagedestroy ($imgIn); 
    imagedestroy ($imgOut); 
?> 

我不知道爲什麼,但是,它給了我零字節的PNG文件。

編輯:感謝ishagg,我得到了我的錯誤日誌。這些是 ;

Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ./qr/qr_txt_test.php on line 10 

Warning: file_get_contents(http://chart.googleapis.com/chart?chs=250&cht=qr&chld=L|0&chl=https%3A%2F%2Fstackoverflow.com&choe=UTF-8): failed to open stream: no suitable wrapper could be found in ./qr/qr_txt_test.php on line 10 

Warning: imagecreatefromstring(): Empty string or invalid image in ./qr/qr_txt_test.php on line 11 

Warning: imagecopy() expects parameter 2 to be resource, boolean given in ./qr/qr_txt_test.php on line 13 

Warning: imagepng(): gd-png error: no colors in palette in ./qr/qr_txt_test.php on line 14 

Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ./qr/qr_txt_test.php on line 15 

回答

1

如果您刪除了header()調用,您將能夠在腳本中看到錯誤。

在你的,有兩種:

  • 變量$ SIZEX,在$qr_url使用的是不確定的。
  • 要從外部資源創建新圖像,您需要使用imagecreatefromstring()

有了固定的這兩個錯誤,代碼工作正常:

<?php 
header('Content-type: image/png'); 
$filename = "qr.png"; 
$link = "https://stackoverflow.com"; 
$size = 250; 
$url = urlencode ($link); 
$qr_url = "http://chart.googleapis.com/chart?chs=$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8"; 
$qr = file_get_contents($qr_url); 
$imgIn = imagecreatefromstring($qr); 
$imgOut = imagecreate ($size, $size); 
imagecopy ($imgOut, $imgIn, 0, 0, 0, 0, $size, $size); 
imagepng ($imgOut, $filename, 9); 
imagedestroy ($imgIn); 
imagedestroy ($imgOut); 

結果:

qr image

+0

感謝您的反饋意見。我已經改變了你的代碼。不幸的是,結果是一樣的。它給了我零字節qr.png文件。 – ziLk

+0

嘗試再次移除'header()'調用,你會得到什麼錯誤? – ishegg

+0

我看不到我的頁面中有任何錯誤日誌。也許管理員,禁用顯示錯誤日誌。我沒有訪問php.ini文件:/ – ziLk