2010-09-09 158 views
0

我無法獲得下面的代碼來上傳大圖片。它適用於圖像小於1000像素×1000像素的情況,但會打破更大的任何圖像。任何幫助/想法非常感謝。PHP圖片上傳問題

注:我試圖增加'$ memoryNeeded> = 10000000'到'7700000000',但仍然沒有喜悅。

if (!$error && is_uploaded_file($_FILES['galleryFile']['tmp_name'])) { 
     $format = strtolower(substr(strrchr($_FILES['galleryFile']['name'],"."),1));    
     $str = strtolower(trim($_FILES['galleryFile']['name'])); 
     $str = preg_replace('/[^a-z0-9-]/', '-', $str); 
     $str = preg_replace('/-+/', "-", $str);  
     $filename=$str.'.'.$format;     
     $uploadGallery=$origFileDir.$filename; 
     foreach ($allowedImgFormats as $key => $value) { 
      $value==$format ? $imgFormatOK='1' : NULL; 
     } 
     $imgFormatOK=='0' ? $error='You are attempting to upload an image with an invalid format!<br />Please only upload images with ".gif", ".jpg" or ".jpeg" extensions.' : NULL; 
     if (!$error && move_uploaded_file($_FILES['galleryFile']['tmp_name'], $uploadGallery)){ 
      $galleryW='944'; $galleryH='733';       
      $galleryInfo = getimagesize($uploadGallery); 
      $memoryNeeded = Round(($galleryInfo[0] * $galleryInfo[1] * $galleryInfo['bits'] * $galleryInfo['channels']/8 + Pow(2, 16)) * 1.65); 
      if ($memoryNeeded>=10000000) { 
       unlink($uploadGallery); $error='The chosen image is too large to process.<br />Please upload a smaller image (lower dimensions and/or resolution).'; 
      } else { 
       list($wOrig, $hOrig) = getimagesize($uploadGallery); 
       $ratio_orig = $wOrig/$hOrig; 
       if ($wOrig > $galleryW) { $galleryW = $galleryH*$ratio_orig; $galleryH = $galleryW/$ratio_orig; } else { $galleryW=$wOrig; $galleryH=$hOrig; }  
       if ($galleryH > $galleryH) { $galleryH = $galleryW*$ratio_orig; $galleryW = $galleryH/$ratio_orig; } 
       $galleryP = imagecreatetruecolor($galleryW, $galleryH); 

       switch($format) { 
        case 'gif' : $thisGallery = imagecreatefromgif($uploadGallery); break; 
        case 'jpg' : $thisGallery = imagecreatefromjpeg($uploadGallery); break; 
       } 
       imagecopyresampled($galleryP, $thisGallery, 0, 0, 0, 0, $galleryW, $galleryH, $wOrig, $hOrig); 

       switch($format) { 
        case 'gif' : $createGallery=imagegif($galleryP, $galleryFileDir.$filename, 88); break; 
        case 'jpg' : $createGallery=imagejpeg($galleryP, $galleryFileDir.$filename, 88); break; 
       } 
       imagedestroy($galleryP); imagedestroy($thisGallery); unlink($uploadGallery); 

       if (!$createGallery) { 
        $error='The chosen image failed to transfer correctly.<br />Please try again, or attempt to upload an alternative image.'; 
        file_exists($galleryFileDir.'/'.$filename) ? unlink($galleryFileDir.'/'.$filename) : NULL; 
       } else {    
        $_POST['imagename']=$filename; 
        mysql_query("INSERT INTO isgallery(imagename, assoc_object) VALUES('".$_POST['imagename']."', '".$_POST['id']."')");       
       } 
      }   
     } else { 
      !$error ? $error='The chosen image failed to upload correctly.<br />Please try again, or attempt to upload an alternative image.' : NULL; 
      file_exists($uploadGallery) ? unlink($uploadGallery) : NULL; 
     } 
    } 
+0

請*總是,總是*引用您收到的錯誤消息。這很可能是內存問題,在這種情況下,這是一個很好的重複:http://stackoverflow.com/questions/1722352/php-memory-error-when-resizing-a-png-image – 2010-09-09 16:33:45

+0

嚴正,任何錯誤信息會好的?這是最有可能的,當你用GD處理圖像時內存不足.. – halfdan 2010-09-09 16:34:29

+1

[mysql_real_escape_string()](http://php.net/mysql_real_escape_string)*咳嗽*('mysql_query(「INSERT INTO isgallery(imagename,assoc_object) VALUES(''。$ _ POST ['imagename']。'','「。$ _ POST ['id']。」')「);') – Lekensteyn 2010-09-09 16:36:10

回答

0

一個1000×1000的圖像,需要在最不 3000000字節的內存,如果你正在處理的真實色彩。如果你正在做Alpha透明度,則爲4,000,000。如果您的$memoryNeeded變量設置爲大於PHP的memory_limit,則該變量無用。它會愉快地嘗試創建一個圖像,並因超出限制而失敗。

您可以檢查ini_get('memory_limit')的限制,儘管您很可能無法直接將此值用於沒有按摩的計算,因爲它可能會返回類似'32M'(32兆字節限制) ,而不是33554432

+0

感謝您的回覆。檢查內存限制,它回來爲128M!? – ss888 2010-09-10 08:14:51

+0

好的,應該有足夠的內存來處理圖像。檢查你的計算是否正確。記錄/回顯$ galleryW和$ galleryH以確保您不會創建令人難以置信的巨大新圖像。然後開始在函數的各個階段放入調試語句,以確切地查看它在哪裏死亡。可能是一些其他問題,如腐敗的圖像。 – 2010-09-10 13:08:35

+0

將嘗試。非常感謝。 – ss888 2010-09-10 16:14:24