使用php上传多个图像时创建缩略图图像

时间:2012-07-27 07:40:23

标签: php image upload thumbnails creation

我正在使用PHP / MySQL为相册上传多张图片。与图像相关联的信息(文件名,扩展名,描述等)作为base64编码数据存储在数据库中。我也可以编辑专辑图片。我现在的问题是,我还希望在插入和编辑模式下上传时为每个图像创建缩略图,并且还像我对图像一样存储缩略图的信息(base64编码)。最好的方法是什么?谢谢 这是我的代码:

<?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')
            ");
        }
    }
}

?>

3 个答案:

答案 0 :(得分:2)

我尝试了一个在本教程中找到的简单缩略图脚本,它运行良好:http://net.tutsplus.com/articles/news/how-to-dynamically-create-thumbnails/

感谢您的建议

答案 1 :(得分:1)

您可以使用

中的缩略图类

http://freecode.com/projects/easyphpthumbnailclass

答案 2 :(得分:0)

您可以使用 ImageMagick resizeImage方法。

在这里的教程中解释http://coffeeshopped.com/2009/01/creating-image-thumbnails-using-php-and-imagemagick

此外,您可以尝试phpThumb()功能,可在sourceforge http://phpthumb.sourceforge.net/

获取

希望这会有所帮助..