简体   繁体   中英

Uploaded image is not save to a folder

I've html upload form which process by image-crop.php. I want to save the image to img folder after upload is done. But it's not save the image to img folder. Any suggestions why image is not save to that folder ?

Html form:

<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 code:

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"]);

Thanks.

You are incorrectly passing second parameter:

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

and probably you need to pass an absolute path, not a relative one, so try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM