上传图片时,我在数据库中获得了资源#7

时间:2018-04-14 21:11:38

标签: php mysql

我正在尝试使用php将数据上传到我的数据库时调整图像大小,但每当我尝试获取上传的数据时,我的数据库库都会显示resource id #7

我上传的图片,使用调整大小和原始图片上传到我的文件夹,但我似乎无法重新获取已调整大小的图片。下面是我用来上传,调整大小和存储图像的代码。

谢谢!

$fileuploaddetected = isset($_FILES['file']) && ($_FILES['file']['error'] === 0);
$uploaderrordetected = isset($_FILES['file']) && ($_FILES['file']['error'] > 0);

if ($fileuploaddetected) {
    if (is_array($_FILES)) {
        $file = $_FILES['file']['tmp_name'];
        $sourceProperties = getimagesize($file);
        $fileNewName = time();
        $folderPath = "uploads/";
        $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
        $imageType = $sourceProperties[2];

        switch ($imageType) {
            case IMAGETYPE_PNG:
                $imageResourceId = imagecreatefrompng($file);
                $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                imagepng($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                break;
            case IMAGETYPE_GIF:
                $imageResourceId = imagecreatefromgif($file);
                $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                imagegif($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                break;
            case IMAGETYPE_JPEG:
                $imageResourceId = imagecreatefromjpeg($file);
                $targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
                imagejpeg($targetLayer, $folderPath . $fileNewName . "_thump." . $ext);
                break;
            default:
                echo "Invalid Image type.";
                exit;
                break;
        }
        move_uploaded_file($file, $folderPath . $fileNewName . "." . $ext);
    }
}

function imageResize($imageResourceId, $width, $height) {
    $targetWidth = 300;
    $targetHeight = 300;
    $targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
    imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
    return $targetLayer;
}


$title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$content = filter_input(INPUT_POST, 'content', FILTER_SANITIZE_FULL_SPECIAL_CHARS);

$error = true;

if (!empty($title) && !empty($content)) {
    $userid = $_SESSION['id'];
    $query2 = "SELECT * FROM user WHERE id = :id";
    $statement2 = $db->prepare($query2);
    $statement2->bindValue(':id', $userid, PDO::PARAM_INT);
    $statement2->execute();
    $postys = $statement2->fetch();

    $userrname = $postys['username'];

    if ($fileuploaddetected) {
        $query = "INSERT INTO blog (title, content, picurl, userid, username) VALUES (:title, :content, :picurl, :userid, :username)";
        $statement = $db->prepare($query);
        $statement->bindValue(':title', $title);
        $statement->bindValue(':content', $content);
        $statement->bindValue(':picurl', $targetLayer);
        $statement->bindValue(':userid', $userid);
        $statement->bindValue(':username', $userrname);
        $statement->execute();

        $blog = $statement->fetchAll();

        $error = false;
        header('Location: index.php');
    }
}

0 个答案:

没有答案