2011-03-10 95 views
4

我正在編寫一個腳本,它將從用戶輸入上傳文件,將其調整爲縮略圖並將兩個新文件名添加到數據庫中。使用PHP GD庫來調整​​大小和保存圖像HELL

但是,我不能爲我的生活弄清楚如何讓PHP檢測圖像的MIME類型,然後將其提供給標題。下面是代碼,我已經把意見,試圖使它儘可能明確:

 $picture = $_FILES['picture']['name']; 

     /*original file location*/     
     $file = 'picture/'.$picture.''; 
     /*save thumbnail location*/ 
     $save = 'thumb/tn-'.$picture.''; 
     /*append thumbnail filename with tn-*/ 
     $thumb = 'tn-'.$picture.''; 
     /*get original file size*/ 
     list($width, $height) = getimagesize($file); 
     /*get image MIME type*/ 
     $size = getimagesize($file); 
     $fp = fopen($file, "r"); 
     if ($size && $fp) 
     { 
     header("Content-type:".$size['mime']); 
     fpassthru($fp); 
     exit; 
     } 
     else 
     { 
     echo 'Error getting filetype.'; 
     } 
     /*define thumbnail dimensions*/ 
     $modwidth = 300; 
     $modheight = 200; 
     /*check to see if original file is not too small*/ 
     if (($width > 301) || ($height > 201)) 
     { 
     /*create shell for the thumbnail*/ 
     $tn = imagecreatetruecolor($modwidth, $modheight); 

     /*HERE IS WHERE I HAVE TROUBLE*/ 

     /*if MIME type is PNG*/ 
     if ($size['mime'] == "image/png") 
      { 
     /*try to create PNG thumbnail*/ 
     $image = imagecreatefrompng($file); 
     imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
     imagepng($tn, $save, 100); 
      } 

     /*if MIME type is JPEG*/ 

     if ($size['mime'] == "image/jpeg") 
      { 
     $image = imagecreatefromjpeg($file); 
     imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
     imagejpeg($tn, $save, 100); 
      } 

     /*if MIME type is GIF*/ 

     if ($size['mime'] == "image/gif") 
      { 
     $image = imagecreatefromgif($file); 
     imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 
     imagegif($tn, $save, 100); 
      } 
     } 
     else { echo 'Your file is too small.'; } 

因此,這裏是我不瞭解的部分:代碼工作正常時,我上傳.JPEG,但是如果它是PNG或GIF,它會顯示一個頁面,說'圖像'127.0.0.1:8888'由於包含錯誤而無法顯示'。我假設它不一定有正確的Header MIME類型,但它可能是別的嗎?

當我選擇一個.jpeg時,它會很好地上傳到我的圖像文件夾,它會像我想要的那樣生成縮略圖文件夾的縮略圖。

但是,只要我嘗試使用PNG和GIF,它就會失敗。我必須錯過明顯的東西?

這段代碼對我所要完成的工作有什麼好處?

在此先感謝,

+0

我冒昧編輯以降低信噪比。非常具體的信息並沒有幫助這個問題。我做了投票,因爲我認爲這是一個有趣的問題,祝你好運! – Trufa 2011-03-10 01:17:59

+0

我有幾本關於PHP的書,我正在穩步學習。我不'堅持複製粘貼在一起',這可能是一種誇大,批准。你花了5分鐘閱讀代碼並理解我的問題?這是在絞盡腦汁。我喜歡PHP,但我真的覺得我需要得到這樣一個人的幫助,這個人比我長得多。 – Seraw 2011-03-10 01:20:34

+1

@coreyward:我完全不同意,在他們確信他們甚至想用語言編碼之前,沒有人必須購買和閱讀整本書,他可能將其作爲一種業餘愛好,現在可能不會有一個真正的項目,我都是爲了書,但只有當你決心掌握一門語言!畢竟這是一個問答網站,我認爲這不是一個微不足道的問題,實際上它已經付出了一些努力,而且可能很有趣。 – Trufa 2011-03-10 01:21:23

回答

6

使用PHP的GD擴展可能真的很痛苦。就個人而言,我會建議採用一個抽象庫,如WideImage。這將大大簡化你的代碼:

$picture = $_FILES['picture']['name']; 

/*original file location*/     
$file = 'picture/'.$picture; 
move_uploaded_file($_FILES["userfile"]["tmp_name"], $picture); 

/*save thumbnail location*/ 
$save = 'thumb/tn-'.$picture; 

// Magic Begins Here ...... 

require "path-to/WideImage.php"; 

$image = WideImage::load($picture); 
$thumb = $image->resizeDown(300, 200, 'inside'); 

$thumb->saveToFile($save); 

是的,所有代碼以上減少到四個車道。


幾個要點:

  • 從不依靠$_FILES[]['mime']。這是由用戶代理髮送的,不能被信任。
  • 出於同樣的原因,我不會根據$_FILES[]['name']的文件名。

此外,您的原始代碼不起作用,因爲您從未使用過move_uploaded_file()

+0

謝謝安德魯,我會研究這個WideImage魔法。另外,我確實使用了move_uploaded_file,只是沒有包含它,因爲我不覺得它是完全有必要了解編碼問題。 另外,你提到輸出?你想介紹一下嗎? 最後,我想有PHP功能,可以讓我檢測服務器端的文件信息?我已經使用getimagesize(),那種事情? – Seraw 2011-03-10 01:31:45

+0

@Seraw:沒關係,只是重新檢查你的代碼...沒關係。 – 2011-03-10 01:35:37

+0

@AndrewMoore我嘗試使用WideImage並用文件測試它,它工作。但是,我將它應用到我的網站,我從數據庫中加載了url,但是我給了我很多亂七八糟的字符。你知道如何解決這個問題嗎? – Marciano 2014-08-29 12:37:31

0

得到了更好的解決方案,實際上使用Thumbnailer類的哈靈圖像可能很有趣。

function callb(& $image) { 
    $image->thumbFixed(300,200)->save('/some/location/'.$image->filename); 
} 

// myFile refers to $_FILES['myFile'] 
// upload image and handle it 
Thumbnailer::upload('myFile', 'callb'); 
+0

@ user644602:這看起來像是一種創建縮略圖的好方法,但Wide-Image看起來像全面更有用於圖像處理。看一看? – Seraw 2011-03-10 15:39:57