2013-11-24 92 views
0

我已經通過image-crop.php將html上傳表單處理到哪個進程。上傳完成後,我想將圖像保存到img文件夾。但它不是將圖像保存到img文件夾。任何建議爲什麼圖像不保存到該文件夾​​?已上傳的圖像不保存到文件夾

HTML表單:

<form action="image-crop-demo.php" method="post" enctype="multipart/form-data"> 
    Upload an image for processing<br> 
    <input type="file" name="Image1"><br> 
    <input type="submit" value="Upload"> 
</form> 

PHP代碼:

define('DESIRED_IMAGE_WIDTH', 200); 
define('DESIRED_IMAGE_HEIGHT', 200); 
$source_path = $_FILES['Image1']['tmp_name']; 
list($source_width, $source_height, $source_type) = getimagesize($source_path); 

switch ($source_type) { 
    case IMAGETYPE_GIF: 
     $source_gdim = imagecreatefromgif($source_path); 
     break; 
    case IMAGETYPE_JPEG: 
     $source_gdim = imagecreatefromjpeg($source_path); 
     break; 
    case IMAGETYPE_PNG: 
     $source_gdim = imagecreatefrompng($source_path); 
     break; 
} 

$source_aspect_ratio = $source_width/$source_height; 
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT; 

if ($source_aspect_ratio > $desired_aspect_ratio) { 
    $temp_height = DESIRED_IMAGE_HEIGHT; 
    $temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio); 
} else { 
    $temp_width = DESIRED_IMAGE_WIDTH; 
    $temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio); 
} 

$temp_gdim = imagecreatetruecolor($temp_width, $temp_height); 
imagecopyresampled(
    $temp_gdim, 
    $source_gdim, 
    0, 0, 
    0, 0, 
    $temp_width, $temp_height, 
    $source_width, $source_height 
); 

$x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2; 
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2; 
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 
imagecopy(
    $desired_gdim, 
    $temp_gdim, 
    0, 0, 
    $x0, $y0, 
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT 
); 

header('Content-type: image/jpeg'); 
imagejpeg($desired_gdim, "img/", $_FILES["file"]["name"]); 

感謝。

回答

0

你被錯誤地傳遞第二個參數:

imagejpeg($desired_gdim, "img/", $_FILES["file"]["name"]); 
           ^--- THE COMMA SHOULD NOT BE THERE 

,可能你需要傳遞一個絕對路徑,而不是相對人,所以試試這個:

imagejpeg($desired_gdim, $_SERVER["DOCUMENT_ROOT"]."img/".$_FILES["file"]["name"]); 
+0

它不保存圖像的img夾。 – Alex

+0

如果我使用this-imagejpeg($ desired_gdim);那麼上傳完成後顯示圖像,但我想保存到IMG文件夾。 – Alex

相關問題