2013-10-13 54 views
0

所以我看了幾乎每個問題,並嘗試使用下面的PHP腳本來顯示我的圖像。但它不起作用。我也試着看看GD庫是否使用PHPInfo()來啓用,並且它也可以工作。我對PHP很陌生,但似乎無法得到這個工作。謝謝你的幫助!PHP imagejpeg無法正常工作

編輯︰我點擊上傳文檔按鈕後,我得到一個破碎的圖像圖標。

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 
if(isset($_FILES['photo']) 
     && is_uploaded_file($_FILES['photo']['tmp_name']) 
     && $_FILES['photo']['error'] == UPLOAD_ERR_OK) { 
      foreach ($_FILES['photo'] as $key => $value) { 
       echo "$key : $value<br />"; 
      } 
      if($_FILES['photo']['type'] == 'image/jpeg') { 
       $tmp_img = $_FILES['photo']['tmp_name']; 
       $image = imagecreatefromjpeg($tmp_img); 
       header('Content-Type: image/jpeg'); 
       imagejpeg($image,NULL); 
       imagedestroy($image); 
      } else { 
       echo "Uploaded file ewas not a jpg image."; 
      } 
       echo "no photo uploaded."; 
       } 
} 
?> 
<form enctype="multipart/form-data" action="book.php" method="post"> 
<input type="file" name="photo"> 
<input type="submit" value="upload a doc"> 
</form> 

回答

0

您可以嘗試imagedestroy($image);之後添加die();防止除了圖像數據的任何其他輸出。還將imagejpeg($image,NULL);更改爲imagejpeg($image);

+0

我只是試了兩種。都沒有工作。 :( –

+0

刪除這也'foreach($ _FILES ['photo']爲$ key => $ value){ echo「$ key:$ value
」; }' – ParaBellum

0

您無法直接訪問圖像tmp_name。首先,你應該將圖像保存到服務器後,您可以用imagecreatefromjpeg

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST') { 
if(isset($_FILES['photo']) 
     && is_uploaded_file($_FILES['photo']['tmp_name']) 
     && $_FILES['photo']['error'] == UPLOAD_ERR_OK) { 
      if($_FILES['photo']['type'] == 'image/jpeg') { 
       $tmp_img = $_FILES['photo']['tmp_name']; 
       $uniq_name = uniqid().".".explode(".",$tmp_img)[1]; 
       move_uploaded_file($_FILES['photo']['tmp_name'],$uniq_name); 
       $image = imagecreatefromjpeg($uniq_name); 
       header('Content-Type: image/jpeg'); 
       imagejpeg($image); 
       imagedestroy($image); 
      } else { 
       echo "Uploaded file ewas not a jpg image."; 
      } 
       echo "no photo uploaded."; 
       } 
} 
?> 
<form enctype="multipart/form-data" action="book.php" method="post"> 
<input type="file" name="photo"> 
<input type="submit" value="upload a doc"> 
</form> 
0

使用我認爲問題出在PHP部分(HTML代碼)後echo和輸出字符串。刪除foreach循環,並僅當它是新頁面或出現錯誤時才輸出HTML。

<?php 
    if($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if(isset($_FILES['photo']) 
     && is_uploaded_file($_FILES['photo']['tmp_name']) 
     && $_FILES['photo']['error'] == UPLOAD_ERR_OK) { 
      if($_FILES['photo']['type'] == 'image/jpeg') { 
       $tmp_img = $_FILES['photo']['tmp_name']; 
       $image = imagecreatefromjpeg($tmp_img); 
       header('Content-Type: image/jpeg'); 
       imagejpeg($image,NULL); 
       imagedestroy($image); 
       exit; // Add this to stop the program here. 
      } else { 
       echo "Uploaded file was not a jpg image."; 
      } 
    } else { 
     echo 'No file was sent.'; 
    } 
} 
?> 
<form enctype="multipart/form-data" action="book.php" method="post"> 
<input type="file" name="photo"> 
<input type="submit" value="upload a doc"> 
</form> 

還有一件事,如果您使用記事本進行編碼,請確保文件開頭處沒有隱藏字符。

+0

感謝您的解決方案。 。此外,我刪除了代碼開始和文件頂部之間的空白區域。空白區域如何創建錯誤?是「隱藏字符」嗎?此外,我正在使用Sublime Text。謝謝! –

+0

There有兩點:第一是輸出任何字符會因爲「header」函數而引發警告,這個警告可能會破壞圖像,第二點是發送給瀏覽器的任何額外字符將被假定爲圖片的一部分甚至空間。 –