public function scanPicture($scandir) { $this->info(""); if (!is_dir($scandir)) { $this->info("skip scan {$scandir}[" . json_encode(is_dir($scandir)) . "]"); return 0; } $count = 0; $scanned_directorys = array_values(array_diff(scandir($scandir), array('..', '.'))); $picture_info_array = array(); foreach ($scanned_directorys as $key => $filepath) { $filepath = $scandir . self::PATH_SEPERATOR . $filepath; $filepath = str_replace('\\', '/', $filepath); $this->info("process {$filepath}"); if (is_dir($filepath)) { $count += $this->scanPicture($filepath); } else { if (!$this->isImage($filepath)) { $this->info("skip not image file! {$filepath} !"); } else { // ignore exif_read_data(CIMG0147_副本.jpg): Illegal IFD size: x016C + 2 + x03E8*12 = x304C > x0184 $exif_data = @exif_read_data($filepath); if (isset($exif_data['DateTimeOriginal'])) { $datetime_original = DateTime::createFromFormat('Y:m:d H:i:s', $exif_data['DateTimeOriginal']); $created_at = $datetime_original->format('Y-m-d H:i:s'); } else { $created_at = date("Y-m-d H:i:s", filectime($filepath)); } // Log::debug('create_time = '.json_encode($created_at)); // Log::debug('exif_data = '.json_encode($exif_data)); // die; $md5 = md5_file($filepath); $title = basename($filepath, '.' . pathinfo($filepath)['extension']); $path = substr($filepath, strlen(storage_path())); $caption = ""; $picture_info_array[] = array('id' => $md5, 'name' => $title, 'path' => $path, 'created_at' => $created_at); $count += 1; } } } $this->info("found {$count} pictures in directory {$scandir}!"); if (0 == count($picture_info_array)) { return 0; } $ablum_info = array('title' => basename($scandir), 'tips' => basename($scandir), 'picture_id' => $picture_info_array[0]['id']); $ablum_info_real = tb_ablums::updateOrCreate(array('title' => $ablum_info['title']), $ablum_info)->toArray(); tb_ablum_picture::where('ablum_id', $ablum_info_real['id'])->delete(); $this->info("updateOrCreate ablum_info: " . $this->grab_dump($ablum_info)); $this->info("updateOrCreate tb_ablums: " . $this->grab_dump($ablum_info_real)); foreach ($picture_info_array as $picture_info) { $id = $picture_info['id']; $picture_info_real = tb_pictures::updateOrCreate(array('id' => $id), $picture_info)->toArray(); $this->info("updateOrCreate tb_pictures: " . $this->grab_dump($picture_info_real)); $ablum_picture_info_real = tb_ablum_picture::create(array('ablum_id' => $ablum_info_real['id'], 'picture_id' => $picture_info_real['id']))->toArray(); // $this->info("create tb_ablum_picture: ".$this->grab_dump($ablum_picture_info_real)); } $this->info(""); return $count; }
public function getAblums() { $user_id = UserAuthController::getLoginUserid(); $page = intval(Input::get('page', '1')); $rows = intval(Input::get('rows', '6')); $total = tb_ablums::select()->count(); $offset = ($page - 1) * $rows; $items = tb_ablums::select()->orderBy('created_at', 'desc')->skip($offset)->take($rows)->get()->toArray(); foreach ($items as $key => &$value) { $picture_of_ablum = tb_pictures::find($value['picture_id']); if (null == $picture_of_ablum) { Log::error('Not found picture for id=' . $value['picture_id']); continue; } $value['picture_id'] = $picture_of_ablum->toArray(); $value['likes'] = self::getLikesOfAblum($value['id']); $value['likeit'] = self::isLikeIt($value['id'], $user_id); // $value['comments'] = self::getCommentsOfAblum($value['id'], 1, 10); // TODO 分页 } return $items; }