Make an image gallery website using PHP and Mysql

How do I create a PHP/MySQL powered image gallery from scratch?

  1. Galleries need to be uploaded using Form and Browse(This i can find no Problem, just need it to be there to outline the steps)
  2. Need to have a thumbnail image created when a file is uploaded.
  3. How should it be structured in the Database, For example stored in DB as image or filename
<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>
     File: <input name='picture' type='file'/>
     <input type='submit' value='Upload'/>
 </form>

Your form needs to have enctype='multipart/form-data' and the method needs to be POST. Then, to read the upload file, you can simply use the following. I’ve also added some basic validation to make sure that the file is an image:

 if(isset($_FILES['picture'])) {
     echo "File has been uploaded under temp file " . $_FILES['picture']['tmp_name'];
     // Let's check if the file is an image:
     $fileData = file_get_contents($_FILES['picture']['tmp_name']);
     // Using imagecreatefromstring, that way you don't need to guess the image format.
     if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
         echo " and is a valid image";
     } else {
         echo " and is not a valid image";
     }
 }

imagecreatefromstring() returns an image identifier representing the image obtained from the given image. These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.

To create a thumbnail image, you can use GD (or ImageMagick, but it is not included in the default configuration) as such… Let’s continue from the imagecreatefromstring if statement:

if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
    // Let's create a 100x100 thumbnail
    $width = imagesx($img);
    $height = imagesy($img);
    $boxSize = min($width,$height);
    $boxX = ($width / 2) - ($boxSize / 2);
    $boxY = ($height / 2) - ($boxSize / 2);
    $thumb = imagecreatetruecolor(100, 100);
    imagecopyresampled($thumb, $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
    //$thumb is now a 100x100 thumbnail
}

Question 3

Here you have 2 options. You can either store your images in the file system or in the database. To store your image in the file system, you can do the following:

if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
    move_uploaded_file($_FILES['picture']['tmp_file'], 'somefile.jpg');
    // the code from the previous example
    imagejpeg($thumb, 'somefile_thumb.jpg');
}

I personally prefer using the database to store the images as it is easier to keep referential integrity and makes backup simpler (backup the database and you are done). It’s a bit slower, but the difference is really not that great:

if(($img = @imagecreatefromstring($fileData)) !== FALSE) {
    // the code from the previous example
    $tmp_thumb = tempnam(sys_get_temp_dir(), 'thumb');
    imagejpeg($thumb, $tmp_thumb);
    $thumbData = file_get_contents($tmp_thumb);
    mysql_query("INSERT INTO images (original, thumb) VALUES ('" . mysql_real_escape_string($fileData) . "', '" . mysql_real_escape_string($thumbData) . "');");
}

The fields needs to be BLOB.

mysql> CREATE TABLE tbl_images (
> id tinyint(3) unsigned NOT NULL auto_increment,
> image blob NOT NULL,
> PRIMARY KEY (id)
> );
A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.

Reasons to store your images (binary files) in a database:

  • Storing in a database allows better security for your files and images.
  • Eliminates messy directory and file structures (this is especially true when users are allow to contribute files).
  • Files/Images can be stored directly linking to user or advertiser information.

You may want to reconsider storing your binary files in the database. Here are some reason you may not want to:

  • You can’t directly access your files using standard applications such as FTP.
  • If you database becomes corrupt, so do your files.
  • Migrating to a new database is made more difficult
  • Pulling images from a database and displaying them is slower than using file access.
 

create an image gallery website using php mysql

Simple PHP Photo Gallery Using Mysql

  • Mysql database to log uploads
  • Short description for the photos
  • Automatic thumbnail generation and resize on upload
  • ability to show the full sized pictures (used jQuery thickbox)

1. Sample database photo table contains three columns: id,photo_name and date_added

CREATE TABLE IF NOT EXISTS `photos` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `photo_name` varchar(200) NOT NULL,
  `date_added` datetime NOT NULL,
  PRIMARY KEY (`id`)
  )

Build An Automated PHP Gallery System In Minutes

I know jQuery. Now what?

http://fredhq.com/projects/roundabout#/download


http://www.scriptiny.com/2011/03/javascript-modal-windows/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: