Home PHP

How To Display Image from Database in PHP?

PHP Quick Tip: Someone asked me how to display image from database in PHP. Here’s how I did it. In this code, we will use two files – index.php and source.php and a database table with sample image data stored.

Please note that I used BLOB data type for storing my images, it can handle up to 64KiB of data. If you want larger storage for each of your images, you can use LONG BLOB that can handle up to 2,048 KiB of data.

Step 1. Create a “test” database and run the following SQL code on your PhpMyAdmin.

CREATE TABLE `images` (
  `id` int(11) NOT NULL,
  `name` varchar(32) NOT NULL,
  `data` longblob NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Go to PhpMyAdmin’s insert tab to add a sample data in the images table.

  • Enter ID as “1”
  • Enter Name as “sample image”.
  • Enter Data by choosing a sample image from your computer.

Step 2. Create index.php file and place the following code. This file will call the image rendered by source.php file and put it as the source of img tag.

<html>
    <head>
        <title>PHP Tutorial</title>
    </head>
<body>

    <div>Here's the image from the database:</div>

    <!– "1" is the database id of the image to be selected –>
    <img src=”source.php?id=1” />
</body>
</html>

Step 3. Create db_connect.php and place the following code. This file will connect our program to the database.

<?php
$host = "localhost";
$db_name = "test";
$username = "root";
$password = "";

try{
	$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
}

catch(PDOException $exception){
	//to handle connection error
	echo "Connection error: " . $exception->getMessage();
}
?>

Step 4. Create source.php file and place the following code. This will do the query of selecting the image based on the given ID parameter.

<?php 
// include database connection 
include "db_connect.php"; 

// select the image 
$query = "select * from images WHERE id = ?"; 
$stmt = $con->prepare( $query );

// bind the id of the image you want to select
$stmt->bindParam(1, $_GET['id']);
$stmt->execute();

// to verify if a record is found
$num = $stmt->rowCount();

if( $num ){
    // if found
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
   
    // specify header with content type,
    // you can do header("Content-type: image/jpg"); for jpg,
    // header("Content-type: image/gif"); for gif, etc.
    header("Content-type: image/png");
   
    //display the image data
    print $row['data'];
    exit;
}else{
    //if no image found with the given id,
    //load/query your default image here
}
?>

Step 5. The output will look like the following.

PHP: Display Image from Database

Step 6. How to resize this image? You can just do it by specifying the width and height properties of of the img tag.

<img src="source.php?id=1" width="300" height="300" />

Step 7. If you want to download the code of this tutorial, use the following button.

THANK YOU!

has been added to your cart!

Powered by Easy Digital Downloads

Thank you!

have been added to your cart!

Powered by Easy Digital Downloads

Thank you for using our tutorial about how to display image from database in PHP!