如何在存储到mysql BLOB字段之前使用PHP更改上传的图像分辨率

时间:2016-07-25 16:41:20

标签: php mysql image

我创建了一个MySQL数据库' tutorial_db'在那个数据库中,我创建了一个包含3列的表:

The image table in my tutorial_db

我使用PHP和HTML编写了以下代码来上传图像文件并将其存储在longblob'图像中。列并在同一页面中显示数据库中的所有图像:

<?php
//connect to database
$con = mysqli_connect('localhost', 'root', '', 'tutorial_db') or die('Could not connect to database: '.mysqli_connect_error());
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Upload Image</title>
</head>
<form action="image.php" method="post" enctype="multipart/form-data">
<p><input type="file" name="image"></p>
<p><input type="submit" name="upload_image" value="Upload"></p>
</form>
<?php
error_reporting(0);
if(isset($_POST['upload_image'])) {
    //check if an image file is uploaded or not.
    if(getimagesize($_FILES['image']['tmp_name']) == FALSE) {
        //this block is executed if a non-image file or no file is uploaded
        echo 'Please upload an image!'; 
    } else {
        //this block is executed if an image is uploaded
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name = addslashes($_FILES['image']['name']);

        //query to insert images in the table
        if(!mysqli_query($con, "INSERT INTO image(image_name, image) VALUES('$image_name', '$image')")) {
            echo "Failed to upload image!";
        }
    }
    //query to display all the images stored in the image database
    $record = mysqli_query($con, "SELECT * FROM image");
    while($rows = mysqli_fetch_assoc($record)) {
        echo '<img width = "20%" height = "200px" src="data:image/jpeg;base64,'.base64_encode( $rows['image'] ).'"/>';
    }
    mysqli_close($con);
}

?>
<body>
</body>
</html>

分辨率低于1920x1080的图像效果非常好。但是当我尝试上传分辨率的图像时,例如:3840x2160它会显示上传失败的错误。我猜这是由于图像分辨率,显示错误。有没有办法在将这些图像存储到MySQL数据库之前降低这些图像的分辨率?

0 个答案:

没有答案