2014-09-12 25 views
0

我用下面的PHP腳本處理的圖片上傳:PHP:圖片不得到保存在指定的路徑

// receive image data 
$imgUrl = $_POST['imgUrl']; 
$imgInitW = $_POST['imgInitW']; 
$imgInitH = $_POST['imgInitH']; 
$imgW = $_POST['imgW']; 
$imgH = $_POST['imgH']; 
$imgY1 = $_POST['imgY1']; 
$imgX1 = $_POST['imgX1']; 
$cropW = $_POST['cropW']; 
$cropH = $_POST['cropH']; 

// set image quality 
$jpeg_quality = 100; 

// define output name 
$output_filename = "uploads/users/croppedImg_".rand(); 

// make sure only specific types of images get uploaded 
$what = getimagesize($imgUrl); 
// get extension of image url 
$imgUrlParts = explode(".", $imgUrl); 
$imgExtension = end($imgUrlParts); 
switch(strtolower($imgExtension)) { 
    case 'png': 
     $img_r = imagecreatefrompng($imgUrl); 
     $source_image = imagecreatefrompng($imgUrl); 
     $type = '.png'; 
     break; 
    case 'jpeg': 
    case 'jpg': 
     $img_r = imagecreatefromjpeg($imgUrl); 
     $source_image = imagecreatefromjpeg($imgUrl); 
     $type = '.jpeg'; 
     break; 
    case 'gif': 
     $img_r = imagecreatefromgif($imgUrl); 
     $source_image = imagecreatefromgif($imgUrl); 
     $type = '.gif'; 
     break; 
    default: die('image type not supported'); 
} 

$resizedImage = imagecreatetruecolor($imgW, $imgH); 
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH); 

$dest_image = imagecreatetruecolor($cropW, $cropH); 
imagecopyresampled($dest_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH); 

imagejpeg($dest_image, $output_filename.$type, $jpeg_quality); 

$response = array(
    "status" => 'success', 
    "url" => $output_filename.$type 
); 

print json_encode($response); 

我的問題是,畫面從未到達在$ output_filename指定的目錄。有誰知道爲什麼?也許有人知道需要在這裏使用一些額外的代碼。

+0

是否文件夾中? webserver(apache?)是否具有寫入權限? – ggutenberg 2014-09-12 15:09:54

回答

0

,首先要確保你有錯誤報告打開,看看是否有周圍拋出一些錯誤:

// Add these to the top of your php script if you can't turn it on in php.ini 
error_reporting(E_ALL); 
ini_set('error_reporting', E_ALL); 

然後我會檢查是否被正確創建的映像。要做到這一點,你可以用NULL替換imagejpeg中的文件名參數,以將圖像直接輸出到瀏覽器。如果它正在工作,那麼它可能是一個權限問題。將輸出位置作爲絕對路徑,並用於調試使用固定的文件名。

如果它仍然無法正常工作,請張貼出現的任何錯誤。