I make a simple uploading and storing picture in mysqli database. I can upload image and store it in database but when displaying it, the picture will be displayed in actual size and it is too large. Can somebody help me out adding some coding to resize pictures uploaded by user? Just resize for view purpose. I don't know how to code it. Here is my coding for index.php
.
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" /> <input type="submit" value="Upload">
</form>
<?php
$db = "databaseimage";
//connect to database
$con = mysqli_connect("127.0.0.1", "root", "MyNewPass") or die (mysqli_error());
mysqli_select_db($con, $db) or die (mysqli_error($db));
//file properties
//tmp_name is a temporary file to store image
// Turn off error reporting
error_reporting(0);
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select an image.";
else
{
//addslashes to prevent any mysql injection
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
// getimagesize will get the image size from the temporary files. this will return false value if it is not image.
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE)
echo "That's not an image.";
else
{
if (!$insert = mysqli_query ($con,"INSERT INTO store VALUES ('', '$image_name', '$image')"))
echo "Problem uploading image!";
else
{
$lastid = mysqli_insert_id($con);
// id=1 will become a unique id for image. but we are using $lastid so that it can become like 1 or 2 or 3 and so on refer to image id stored in database
echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
Here is my coding to get image get.php
<?php
$db = "databaseimage";
//connect to database
$con = mysqli_connect("127.0.0.1", "root", "MyNewPass") or die (mysqli_error());
mysqli_select_db($con, $db) or die (mysqli_error($db));
$id = addslashes ($_REQUEST['id']);
//image given out to the user
$image = mysqli_query($con, "SELECT * FROM store WHERE id=$id"); //solved
$image = mysqli_fetch_assoc($image); //solved
$image = $image['image'];
// type of file changed to an image
header ("Content-type: image/jpeg");
echo $image;
?>