繁体   English   中英

上载的图像未保存到文件夹

[英]Uploaded image is not save to a folder

我有html上传表单,该表单由image-crop.php处理。 上传完成后,我想将图像保存到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>

邮递区号:

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

谢谢。

您错误地传递了第二个参数:

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

可能您需要传递一个绝对路径,而不是相对路径,因此请尝试以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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