Esempio n. 1
0
 function save_upload($album_id, $tmpfile, $filename, $new_photo = true, $photo_info = array())
 {
     $apath = str_pad($album_id, 6, '0', STR_PAD_LEFT);
     $apath = substr($apath, 0, 4) . '/' . substr($apath, 4, 2);
     $media_dirname = 'data/' . $apath;
     $thumb_dirname = 'data/t/' . $apath;
     $storlib =& loader::lib('storage');
     $imglib =& loader::lib('image');
     $exiflib =& loader::lib('exif');
     $fileext = file_ext($filename);
     $filepure = file_pure_name($filename);
     $key = str_replace('.', '', microtime(true));
     $tmpfs_lib =& loader::lib('tmpfs');
     $tmpfile_thumb = $tmpfs_lib->get_path($key . '_thumb.' . $fileext);
     if (file_exists($tmpfile)) {
         $setting =& Loader::model('setting');
         if (!$imglib->load($tmpfile)) {
             //如果不是有效的直接退出,并删除临时文件
             $tmpfs_lib->delete($tmpfile, true);
             return false;
         }
         if (!in_array($fileext, array('jpg', 'png', 'jpeg', 'gif', 'bmp'))) {
             $fileext = $imglib->getExtension();
         }
         if (isset($photo_info['cate_id'])) {
             $arr['cate_id'] = $photo_info['cate_id'];
         }
         if (!$new_photo) {
             $filepath = $photo_info['path'];
             $thumbpath = $photo_info['thumb'];
         } else {
             $use_old_img_name = $setting->get_conf('upload.use_old_imgname', false);
             //如果文件名是只包含数字
             if ($use_old_img_name && file_en_name($filepure)) {
                 //如果存在此图片,则生成唯一的名字
                 $filepath = $media_dirname . '/' . $filepure . '.' . $fileext;
                 $count = 1;
                 while (file_exists($filepath)) {
                     $filepath = $media_dirname . '/' . $filepure . '_' . $count . '.' . $fileext;
                     $count++;
                 }
             } else {
                 $filepath = $media_dirname . '/' . $key . '.' . $fileext;
             }
             $thumbpath = $thumb_dirname . '/' . $key . '.' . $fileext;
         }
         $arr['width'] = $imglib->getWidth();
         $arr['height'] = $imglib->getHeight();
         if ($fileext == 'jpg') {
             $exif = $exiflib->get_exif($tmpfile);
             if ($exif) {
                 $arr['exif'] = serialize($exif);
                 $taken_time = strtotime($exif['DateTimeOriginal']);
                 $arr['taken_time'] = $taken_time;
                 $arr['taken_y'] = date('Y', $taken_time);
                 $arr['taken_m'] = date('n', $taken_time);
                 $arr['taken_d'] = date('j', $taken_time);
             }
         }
         //如果开启自动裁剪大图片,将临时文件直接裁剪
         if ($setting->get_conf('upload.enable_cut_big_pic', false)) {
             $max_width = $setting->get_conf('upload.max_width', 1600);
             $max_height = $setting->get_conf('upload.max_height', 1600);
             if ($arr['width'] > $max_width || $arr['height'] > $max_height) {
                 $imglib->resizeScale($max_width, $max_height);
                 $imglib->save($tmpfile);
                 $arr['width'] = $imglib->getWidth();
                 $arr['height'] = $imglib->getHeight();
             }
         }
         //设置水印前生成缩略图
         $thumb_width = $setting->get_conf('upload.thumb_width', 180);
         $thumb_height = $setting->get_conf('upload.thumb_height', 180);
         if ($arr['width'] > $thumb_width || $arr['height'] > $thumb_height) {
             if ($setting->get_conf('upload.enable_thumb_cut', false)) {
                 $imglib->resizeCut($thumb_width, $thumb_height);
                 //裁剪
             } else {
                 $imglib->resizeScale($thumb_width, $thumb_height);
             }
             $imglib->save($tmpfile_thumb);
         } else {
             //图片过小的话则直接拷贝至缩略图
             @copy($tmpfile, $tmpfile_thumb);
         }
         $water_setting = $setting->get_conf('watermark');
         if ($water_setting['type'] != 0) {
             $imglib->load($tmpfile);
             if ($water_setting['type'] == 1) {
                 $water_setting['water_mark_type'] = 'image';
                 if (STORAGE_ENGINE != 'file') {
                     $ws_tmpfile = 'ws_tmp';
                     $ws_file_content = $storlib->read($water_setting['water_mark_image']);
                     if ($ws_file_content) {
                         $tmpfs_lib->write($ws_tmpfile, $ws_file_content);
                         $water_setting['water_mark_image'] = $tmpfs_lib->get_path($ws_tmpfile);
                         $imglib->waterMarkSetting($water_setting);
                         $imglib->waterMark();
                         $imglib->save($tmpfile);
                         $tmpfs_lib->delete($ws_tmpfile);
                     }
                 } else {
                     $imglib->waterMarkSetting($water_setting);
                     $imglib->waterMark();
                     $imglib->save($tmpfile);
                 }
             } elseif ($water_setting['type'] == 2) {
                 $water_setting['water_mark_type'] = 'font';
                 $water_setting['water_mark_font'] = $water_setting['water_mark_font'] ? ROOTDIR . 'statics/font/' . $water_setting['water_mark_font'] : '';
                 $imglib->waterMarkSetting($water_setting);
                 $imglib->waterMark();
                 $imglib->save($tmpfile);
             }
         }
         $imglib->close();
         if ($storlib->upload($filepath, $tmpfile)) {
             $arr['album_id'] = $album_id;
             $arr['path'] = $filepath;
             $arr['thumb'] = $thumbpath;
             if ($new_photo) {
                 $arr['name'] = isset($photo_info['name']) ? $photo_info['name'] : $filepure;
                 $arr['create_time'] = time();
                 $arr['create_y'] = date('Y');
                 $arr['create_m'] = date('n');
                 $arr['create_d'] = date('j');
             }
             $arr = $arr + $photo_info;
             //move thumb img
             $storlib->upload($thumbpath, $tmpfile_thumb);
             if ($new_photo) {
                 $photo_id = $this->save($arr);
                 if (!$photo_id) {
                     $storlib->delete($filepath);
                     $storlib->delete($thumbpath);
                 }
             } else {
                 $photo_id = $photo_info['id'];
                 $result = $this->update($photo_id, $arr);
             }
             //remove tmp files
             $tmpfs_lib->delete($tmpfile, true);
             $tmpfs_lib->delete($tmpfile_thumb, true);
             $plugin =& Loader::lib('plugin');
             $plugin->trigger('uploaded_photo', $photo_id);
             return $photo_id;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Esempio n. 2
0
 function save_import()
 {
     need_login('page');
     @set_time_limit(0);
     $timestamp = time();
     $dir = $this->getPost('dir');
     $autodel = $this->getPost('autodel') ? true : false;
     $savemode = intval($this->getPost('save_mode'));
     $album_id = intval($this->getRequest('aid'));
     if (substr($dir, 0, 1) == '/' || substr($dir, 1, 1) == ':') {
         //如果是绝对地址
         $dirpath = $dir;
     } else {
         $dirpath = ROOTDIR . $dir;
     }
     //判断扫描的文件夹是否存在
     if (!is_dir($dirpath)) {
         showError(lang('scan_dir_not_exists'));
     }
     if (!is_readable($dirpath)) {
         showError(lang('dir_cannot_read'));
     }
     //开始扫描文件夹
     $alldir = getdirlist($dirpath);
     if (!$alldir) {
         showError(lang('dir_has_no_files'));
     }
     $tmpfslib =& loader::lib('tmpfs');
     $imglib =& loader::lib('image');
     $supportType = $imglib->supportType();
     $album_num = 0;
     $photos_num = 0;
     foreach ($alldir as $dir => $files) {
         if (count($files) <= 0) {
             continue;
         }
         if ($savemode == 2) {
             $dirname = file_base($dir);
             $dirname = file_en_name($dirname) ? $dirname : date('Y-m-d', $timestamp) . '_' . rand(10, 99);
             $data = array('name' => $dirname, 'create_time' => $timestamp, 'enable_comment' => 1, 'cate_id' => 0);
             //创建相册
             $album_id = $this->mdl_album->save($data);
             $album_num++;
         }
         foreach ($files as $file) {
             $file_ext = file_ext($file);
             if (!in_array($file_ext, $supportType)) {
                 continue;
             }
             //将存储的图片读取到临时文件
             $tmpfile = time() . rand(1000, 9999) . '.' . $file_ext;
             $tmpfslib->write($tmpfile, file_get_contents($file));
             $tmpfilepath = $tmpfslib->get_path($tmpfile);
             if ($this->mdl_photo->save_upload($album_id, $tmpfilepath, file_base($file))) {
                 if ($autodel) {
                     @unlink($file);
                 }
                 $photos_num++;
             }
         }
         if ($savemode == 2) {
             $this->mdl_album->update_photos_num($album_id);
             $this->mdl_album->check_repare_cover($album_id);
         }
     }
     if ($savemode == 1) {
         $this->mdl_album->update_photos_num($album_id);
         $this->mdl_album->check_repare_cover($album_id);
     }
     $msg = lang('import_success', $album_num, $photos_num);
     $this->output->set('result_msg', $msg);
     $this->output->set('album_id', $album_id);
     //面包屑
     $crumb_nav = array();
     $crumb_nav[] = array('name' => lang('upload_photo'));
     $this->page_crumb($crumb_nav);
     $page_title = lang('upload_photo') . ' - ' . $this->setting->get_conf('site.title');
     $page_keywords = $this->setting->get_conf('site.keywords');
     $page_description = $this->setting->get_conf('site.description');
     $this->page_init($page_title, $page_keywords, $page_description);
     $this->render();
 }