Trước tiên, bạn cần lưu hình ảnh từ đầu ra file_get_contents vào cơ sở dữ liệu của bạn. Và sau đó bạn đặt nó vào imagecreatefromstring và hiển thị hình ảnh của bạn.
Đây là ví dụ đơn giản. Có lẽ điều này sẽ giúp bạn :)
$data = file_get_contents("ACL.jpg");
$img = imagecreatefromstring($data);
header("Content-Type: image/jpeg");
imagejpeg($img);
CHỈNH SỬA:
bạn chỉ cần đặt mã này:
$statement->bind_result($imageid, $title, $image)
while ($statement->fetch()) {
if ($image == NULL) {
echo "Image data does not exist!";
} else {
$img = imagecreatefromstring($image);
header("Content-Type: image/jpeg");
imagejpeg($img);
}
}
CHỈNH SỬA SỬA:
uploads.php
Trong tệp này, bạn cần thay đổi $statement->bind_param('sb', $title, $content); trở thành $statement->bind_param('ss', $title, $content);
if (isset($_POST['submit'])) {
$title = $_FILES['image']['name'];
$data = $_FILES['image']['tmp_name'];
$content = file_get_contents($data);
$query = "INSERT INTO images (title, image) VALUES (?, ?)";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('ss', $title, $content);
$statement->execute();
$statement->store_result();
$creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
if ($creationWasSuccessful)
{
echo "Works!";
} else {
echo 'failed';
}
}
showimage.php và sau đó trong bạn hiển thị nó bằng cách sử dụng cái này:
$img = imagecreatefromstring($image);
header("Content-Type: image/jpeg");
imagejpeg($img); trong tuyên bố cuối cùng của bạn
if (isset($_GET['id'])) {
$id = $_GET['id'];
$query = "SELECT id,title,image FROM images WHERE id = ?";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('i', $id);
$statement->execute();
$statement->store_result();
if ($statement->num_rows >= 1)
{
$statement->bind_result($imageid, $title, $image)
while ($statement->fetch()) {
if ($image == NULL) {
echo "Image data does not exist!";
} else {
$img = imagecreatefromstring($image);
header("Content-Type: image/jpeg");
imagejpeg($img);
}
}
}
}
Hy vọng điều này cũng hoạt động tốt, tôi đã thử nghiệm nó và nó chạy tốt ... :)