创建新图像 - 目标文件夹为空

时间:2018-04-20 14:03:33

标签: php image

完全混淆了PHP图像功能。无法获得新的图像。

$file = $_FILES['inpfile'];  // input

$tempname = $file['tmp_name'];

$info = getimagesize($tempname);

$mime = $info['mime'];

if ($mime == 'image/jpg') {
    $newimg = imagecreatefromjpeg($tempname);
}
elseif ($mime == 'image/png') {
    $newimg = imagecreatefrompng($tempname);
}
elseif ($mime == 'image/gif') {
    $newimg = imagecreatefromgif($tempname);
}

imagejpeg($newimg, 'test.jpg', 70);

$uniqname = uniqid() . '.jpg';
$targ = 'test/' . $uniqname;
move_uploaded_file('test.jpg', $targ);

我希望test.jpg文件夹中有一个新图片(test),但它是空的。

没有错误,假设此处缺少某些内容。

任何帮助。

1 个答案:

答案 0 :(得分:0)

试试这个代码:),我制作了一个简单的系统,可以上传图像并将图像存储在1个目录名称上传中,还可以在图库中查看图像

<?php

require('db.php');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (isset($_POST['submit']))
{
    $name = $_POST['Name'];
    $Descrp = $_POST['Descrp'];
    if (empty($name)) {
        echo "<script>alert('Name was empty');
                                window.location='index.php';
                            </script>";
    }
    else
    {
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {

            echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');
                                window.location='index.php';
                            </script>";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
            echo "<script>alert('Sorry, your file was not uploaded.');
                                window.location='index.php';
                            </script>";
        // if everything is ok, try to upload file
        } 
        else 
        {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                $imgName = $_FILES["fileToUpload"]["name"];
                mysqli_query($con,"INSERT INTO users (`id`, `name`, `img`,`descrp`) VALUES (NULL, '$name','uploads/$imgName','$Descrp')");
                echo "<script>alert('Successfully Added');
                                window.location='gallery.php';
                            </script>";
            } 
            else 
            {
                echo "<script>alert('Sorry, there was an error uploading your file.');
                                window.location='index.php';
                            </script>";
            }
        }

    }


}
?>

Github Repo:Upload Image Into Upload Directory With Image Gallery