tags:

views:

595

answers:

3

This question has been asked before mentioning the lib or includes that provides a functional gallery, But i want to create one from scratch. So any ideas on the following

  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

Requirments

  • Only PHP and MySql

Any ideas? Please let me know if it cant be done as well :D

Thanks

A: 

You're almost definitely going to want to store the images in the filesystem and then just reference the filename\path in the DB entry - it keeps your query result sizes down, especially if you want to pull the info for multiple images. It also makes it far easier to invoke things like imagemagick if you want to use that to create thumbnails.

Amber
Does imagemagick support PHP?
Shahmir Javaid
http://us.php.net/imagick
Amber
+2  A: 

I'm going to attempt to answer your questions:


Question 1

That part is actually simple. To create a file upload form, your HTML needs to look like that:

 <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";
     }
 }


Question 2

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.

Andrew Moore
am i right in saying that The insert Query Datatype is TEXT?
Shahmir Javaid
**@Shahmir Javiad:** No, it needs to be `BLOB` as specified in my answer. `TEXT` is location aware and while it is great for large texts, it will cause you data corruption problems for binary objects.
Andrew Moore
10/4 Com, After carefully reading the answer i can see it working therefore i will accept it
Shahmir Javaid
I meant **locale aware** in my original comment. I guess I need some sleep.
Andrew Moore
+1  A: 

try this:

http://www.sitepoint.com/article/php-gallery-system-minutes/

inakiabt
That is a good link i looked at a tutorial.. If the accepted answer is not for you try this instead :D
Shahmir Javaid