0

I'm using PHP/MySQL to upload multiple images for photo album. The information associated with the images (filenames, extensions, descriptions etc.) is stored in database as base64 encoded data. I'm also able to edit the album images. My problem now is that I also want to create thumbnails for each of the images when uploading them on insert and edit mode and also store the information for the thumbnails the same way I do for the images (base64 encoded). What's the best way to do that? Thanks Here's my code:

<?php

//Code for the editing albums in database   
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {

        //select photo album and get photos and descriptions to be merged with new.
        $album_id           = $_REQUEST['album_id'];    
        $old_photos         = null;
        $old_descriptions   = null;

        $getAlbumQ = mysql_query("select * from albums where id='$album_id'");

        while ($old_album = mysql_fetch_array($getAlbumQ)) {
            $old_photos         = unserialize(base64_decode($old_album['photos']));
            $old_descriptions   = unserialize(base64_decode($old_album['descriptions']));
            $old_timestamp=$old_album['timestamp'];
        }

        if (isset($_POST['album_name']) && isset($_POST['desc'])) {
            $name       = mysql_real_escape_string($_POST['album_name']);
            $desc       = mysql_real_escape_string($_POST['desc']);
            $idesc      = array();
            $target_path = "/uploads/albums/";



            foreach ($_FILES as $key => $value) {
                 foreach ($value as $k => $v) {
                         if ($k === 'name' && $v !== '') {
                             break;
                         } else {
                             unset($_FILES[$key]);
                         }

                    }

                //first upload photos
                $path = $target_path . basename($old_timestamp.$value['name']); 
                if(move_uploaded_file($value['tmp_name'], $path)) {

                  $hasUpload = true;

                }   

            }


            for ($j = 1; $j < 21; $j++) {
                    $img_index  = $j;
                    $img_desc   = $_POST['desc_' . $img_index];
                    array_push($idesc, $img_desc);

            }


             foreach ($_FILES as $key => $value) {

                    foreach ($value as $k => $v) {
                         if ($k=='name' && $v!='') {
                             break;
                         } else {
                             unset($_FILES[$key]);
                           }
                   }
            }

function merge(&$a, &$b){
     $keys = array_keys($a);
     foreach($keys as $key){
          if(isset($b[$key])){
              if(is_array($a[$key]) and is_array($b[$key])){
                 merge($a[$key],$b[$key]);
              }
              else{
                  $a[$key] = $b[$key];
              }
          }
     }
    $keys = array_keys($b);
    foreach($keys as $key){
        if(!isset($a[$key])){
            $a[$key] = $b[$key];
        }
    }
}

            for ($i = 1; $i < 21; $i++) {

                $file_index     = $i;
                $file_number    = $_POST['image_' . $file_index];

                if($_FILES['image_'.$i]['name']!= ''){

                  $hasUpload = true;

                  $presults         = merge($old_photos, $_FILES);
                  $dresults     = array_merge($old_descriptions, $idesc);

                  $images       = base64_encode(serialize($presults));
                  $descriptions = base64_encode(serialize($dresults));
                  $posted       = date("Y-m-d H:i:s");
                }
                else {
                  $hasUpload = false;

                  $presults         = $old_photos;
                  $dresults     = $idesc;

                  $images       = base64_encode(serialize($presults));
                  $descriptions = base64_encode(serialize($dresults));
                  $posted       = date("Y-m-d H:i:s");
                }
            }

}

    if (mysql_query("update albums set description = '$desc', name = '$name', photos='$images', descriptions='$descriptions' where id='$album_id'")) {
        $hasAlbums = true;
    } else {
        $hasErrors = true;
    }
} else {

    //Code for the inserting albums in database
    if (isset($_POST['album_name'])) {


        if (isset($_POST['album_name']) && isset($_POST['desc'])) {
            $name       = mysql_real_escape_string($_POST['album_name']);
            $desc       = mysql_real_escape_string($_POST['desc']);
            $idesc      = array();
            $timestamp = time();
            $target_path = "/uploads/albums/";


            foreach ($_FILES as $k => $v) {
                //first upload photos
                $path = $target_path . basename($timestamp.$v['name']); 
                if(move_uploaded_file($v['tmp_name'], $path)) {

                    $img_index  = explode('_', $k);
                    $img_index  = $img_index[1];
                    $img_desc   = $_POST['desc_' . $img_index];
                    array_push($idesc, $img_desc);

                    $file_name  = $timestamp.$v['name'];
                    $hasUpload = true;
                }   
            }

            $images         = base64_encode(serialize($_FILES));
            $descriptions   = base64_encode(serialize($idesc));
            $posted         = date("Y-m-d H:i:s");


            $query = mysql_query("
        INSERT INTO albums (description, posted, photos, name, descriptions, timestamp) 
                    VALUES ('$desc', '$posted', '$images', '$name', '$descriptions', '$timestamp')
            ");
        }
    }
}

?>
Kokos Koka
  • 93
  • 3
  • 12
  • You could create the thumbnail in the part where you `move_uploaded_file` and append the string `thumb_` to it.. then all you need to do is something along the lines of `` – Dale Jul 27 '12 at 07:50

3 Answers3

2

I tried a simple thumbnail script found on this tutorial and it worked perfectly: http://net.tutsplus.com/articles/news/how-to-dynamically-create-thumbnails/

Thanks for your suggestions

Kokos Koka
  • 93
  • 3
  • 12
1

You can use the Thumbnail class from

http://freecode.com/projects/easyphpthumbnailclass

Sumesh TG
  • 430
  • 1
  • 4
  • 15
0

You may use ImageMagick's resizeImage method.

explained in a tutorial here http://coffeeshopped.com/2009/01/creating-image-thumbnails-using-php-and-imagemagick

Also, you may try phpThumb() function and is available at sourceforge here http://phpthumb.sourceforge.net/

Hope this helps..

Alfred
  • 19,306
  • 58
  • 155
  • 232