How to Upload & Resize a PHP MySQL Image

Written by mark whitney
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Upload & Resize a PHP MySQL Image
It's often useful to produce different sizes of an image. (Thinkstock/Comstock/Getty Images)

When dealing with a large amount of images, it is often useful to store them in a database. This makes accessing and changing the pictures easy and streamlined, even when you need to change more than one at once. One common task performed on stored images is to resize the image. Uploading and resizing an image using PHP and MySQL can be very simple, and it allows you to do many things, like create a batch of thumbnails for your image gallery, or just display a given image in a size other than the original.

Skill level:

Things you need

  • Web hosting with PHP and MySQL installed

Show MoreHide


  1. 1

    Create your database and table in MySQL. Using your MySQL interface, run the following commands:


    USE test;

    CREATE TABLE images (

    image_id serial,

    filename varchar(255) not null,

    mime_type varchar(255) not null,

    file_size int not null,

    file_data longblob not null,

    primary key (image_id),

    index (filename)


  2. 2

    Use a text editor to create a file named "image_upload.html" and fill it with the code below. This will be the form to select your image with.


        &lt;title&gt;Upload an Image&lt;/title&gt;
            &lt;h1&gt;Upload an Image&lt;/h1&gt;
            &lt;form method="post" action="image_insert.php" enctype="multipart/form-data"&gt;
                    &lt;input type="file" name="image" /&gt;
                    &lt;input type="submit" value="Upload Image" /&gt;


  3. 3

    Create a file named "connect.php" and fill it with the code below. This will be the script that allows your other files to use the database. Use your own username and password in place of the "$un" and "$pw" values included.


    $host="localhost"; $un="root"; $pw="password"; $dB = "test";

    $link = mysqli_connect($host, $un, $pw, $dB);

    if(!$link){die('Damnit, link not established.'); exit;}

    $dB = "test";

    $dB = mysqli_select_db($link, $dB);

    if (!$link) {

    die('Connect Error (' . mysqli_connect_errno() . ') '

    . mysqli_connect_error());

    if (!$link) {

    echo "Unable to establish connection to database server";



    if (!mysqli_select_db($link, 'test')) {

    echo "Unable to connect to database";




  4. 4

    Create a file named "image_insert.php" and fill it with the code below. This file will take the picture data we send from the form, and place it in your images table. If there is no image passed to this file, it will return an error for you.



    if (!$_FILES['image']){die('No Image!');}

        $image = $_FILES['image'];

    print_r ($image);

        $query = sprintf(
            "INSERT INTO images (filename, mime_type, file_size, file_data)
                VALUES ('%s', '%s', %d, '%s')",
        if (!$result = mysqli_query($link, $query)){die('Shit happened when trying to insert into the table' .mysqli_error($link));}

        $id = (int) mysqli_insert_id($link);
        header('Location: image_view.php?id=' . $id);



            &lt;p&gt;There was an error in your code, or the file could not be uploaded for another reason.            &lt;/p&gt;
                &lt;a href="image_upload.php"&gt;Go Back&lt;/a&gt;


  5. 5

    Create a file named "image_view.php" and save the code below into it. This file will pull the image from the database, and show it to you in whatever size you wish. The code below has the image displayed at 500 pixels by 500 pixels.



    $desired_width = 500;

    $desired_height = 500;

    $id = (int) $_GET['id'];

    $query = "SELECT * FROM images WHERE image_id = $id";

    $result = mysqli_query($link, $query);

    $image = mysqli_fetch_array($result);


    $blobcontents = $image['file_data'];

    $im = imagecreatefromstring($blobcontents);

    $new = imagecreatetruecolor($desired_width, $desired_height);

    $x = imagesx($im);

    $y = imagesy($im);

    imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);


    header('Content-type: image/jpeg');

    imagejpeg($new, null, 85);


  6. 6

    Browse to the image_upload.html file and click "Choose File." Select the file from your computer that you want to upload. Click "Upload File." The software saves it to the database, and then displays the image to you in the size you have requested.

Tips and warnings

  • Changing the values of $desired_height and $desired_width in the image_view.php file will change the size of the image when it's displayed.
  • Alternatively, you could create fields for these values in the upload form, and pass them to the final page to make the script more dynamic.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.