public static function save_images($file_tmps, $parent_id, $parent_id_field, $related_table, $public_flag = null, $type = 'img') { if (!in_array($type, array('img', 'file'))) { throw new InvalidArgumentException('Parameter type is invalid.'); } $moved_files = array(); $related_table_ids = array(); if (!$file_tmps) { return array($moved_files, $related_table_ids); } $model = Site_Model::get_model_name($related_table); $related_table_obj = $model::forge(); if (!($file_cate = $related_table_obj->get_image_prefix())) { throw new \InvalidArgumentException('Parameter $related_table is invalid.'); } $new_filepath_prefix = Site_Upload::get_filepath_prefix($file_cate, $parent_id); $new_filename_prefix = Site_Upload::convert_filepath2filename($new_filepath_prefix); $is_save_storage = conf('upload.storageType') != 'normal'; if (!$is_save_storage) { $new_file_dir = Site_Upload::get_uploaded_path('raw', 'img', true, false, $new_filepath_prefix); if (!Site_Upload::check_and_make_uploaded_dir($new_file_dir, conf('upload.check_and_make_dir_level'), conf('upload.mkdir_mode'))) { throw newFuelException('Failed to make save dirs.'); } } foreach ($file_tmps as $id => $file_tmp) { $old_file_path = Site_Upload::get_uploaded_file_path($file_tmp->name, 'raw', 'img', true); $moved_files[$file_tmp->id] = array('from' => $old_file_path); if (!$is_save_storage) { $new_file_path = $new_file_dir . $file_tmp->name; Util_file::move($old_file_path, $new_file_path); $moved_files[$file_tmp->id]['to'] = $new_file_path; $moved_files[$file_tmp->id]['filepath_prefix'] = $new_filepath_prefix; } if ($type == 'img') { $moved_files[$file_tmp->id]['from_thumbnail'] = Site_Upload::get_uploaded_file_path($file_tmp->name, 'thumbnail', 'img', true); } $file = Model_File::move_from_file_tmp($file_tmp, $new_filename_prefix, $type); $related_table_obj = $model::forge(); $related_table_obj->{$parent_id_field} = $parent_id; $related_table_obj->file_name = $file->name; $related_table_obj->name = $file_tmp->description; $related_table_obj->shot_at = !empty($file->shot_at) ? $file->shot_at : date('Y-m-d H:i:s'); if (!is_null($public_flag)) { $related_table_obj->public_flag = $public_flag; } $related_table_obj->save(); $related_table_ids[] = $related_table_obj->id; } return array($moved_files, $related_table_ids); }
public function save($uploaded_file_path = null) { try { if (!$uploaded_file_path) { $uploaded_file_path = $this->upload_file(); } $this->set_file_properties($uploaded_file_path); if (!Site_Upload::check_and_make_uploaded_dir($this->options['upload_dir'], null, $this->options['path_chmod'])) { throw new FuelException('ディレクトリの作成に失敗しました。'); } $tmp_file_path = APPPATH . 'tmp/' . $this->file->name; if (!Util_file::move($uploaded_file_path, $tmp_file_path)) { throw new FuelException('Save raw file error.'); } // exif データの取得 $exif = array(); if ($this->options['is_save_exif_to_db'] && $this->file->type == 'image/jpeg') { $exif = \Util_Exif::get_exif($tmp_file_path, $this->options['exif_accept_tags'], $this->options['exif_ignore_tags']); } $is_resaved = false; // 回転状態の補正 if ($this->options['auto_orient'] && !empty($exif['Orientation'])) { Util_file::correct_orientation($tmp_file_path, $exif['Orientation']); $is_resaved = true; } // 大きすぎる場合はリサイズ & 保存ファイルから exif 情報削除 $file_size_before = $this->file->size; if ($max_size = Site_Upload::get_accepted_max_size($this->options['member_id'])) { $this->file->size = Site_Upload::check_max_size_and_resize($tmp_file_path, $max_size); $is_resaved = $this->file->size != $file_size_before; } if ($this->options['is_clear_exif_on_file'] && !$is_resaved) { Site_Upload::clear_exif($tmp_file_path); $this->file->size = File::get_size($tmp_file_path); } $this->save_model_file($exif); $this->save_file_bin($tmp_file_path); } catch (\FuelException $e) { if (isset($this->file->file_path) && file_exists($this->file->file_path)) { Util_file::remove($this->file->file_path); $this->file->size = filesize($this->file->file_path); } $this->file->error = $e->getMessage(); } return $this->file; }
private function make_image($original_file_path, $target_file_dir, $target_filename) { $target_file_path = $target_file_dir . $target_filename; Site_Upload::check_and_make_uploaded_dir($target_file_dir); Util_file::resize($original_file_path, $target_file_path, $this->width, $this->height, $this->resize_type); }
protected function handle_file_upload($uploaded_file, $original_name, $size, $type, $error, $index = null, $content_range = null) { $file = new \stdClass(); $file->is_tmp = $this->options['is_tmp']; $file->original_name = $original_name; $file->size = $this->fix_integer_overflow(intval($size)); $file->type = $type; if (!($extention = Util_file::check_file_type($uploaded_file, \Site_Upload::get_accept_format($this->options['upload_type']), $type, $this->options['upload_type']))) { $file->error = $this->get_error_message('accept_file_types'); return $file; } if (!($filename_with_prefix = Site_Upload::make_unique_filename($extention, $this->options['filename_prefix'], $original_name))) { $file->error = 'ファイル名の作成に失敗しました。'; return $file; } $file->name = $this->remove_filename_prefix($filename_with_prefix); $file->name_prefix = $this->options['filename_prefix']; if (!\Site_Upload::check_and_make_uploaded_dir($this->options['upload_dir'], null, $this->options['mkdir_mode'])) { $file->error = 'ディレクトリの作成に失敗しました。'; return $file; } if (!$this->validate($uploaded_file, $file, $error, $index)) { return $file; } if ($this->options['upload_type'] == 'img') { $file->thumbnail_uri = $this->options['image_versions']['thumbnail']['upload_url'] . $file->name; } $this->handle_form_data($file, $index); $upload_dir = $this->get_upload_path(); $file_path = $this->get_upload_path($file->name); $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path); if ($uploaded_file && is_uploaded_file($uploaded_file)) { // multipart/formdata uploads (POST method uploads) if ($append_file) { file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND); } else { $res = move_uploaded_file($uploaded_file, $file_path); } } else { // Non-multipart uploads (PUT method support) file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0); } $file_size = $this->get_file_size($file_path, $append_file); if ($file_size === $file->size) { $file->url = $this->get_download_url($file->name); if ($this->is_valid_image_file($file_path)) { $this->handle_image_file($file_path, $file); } } else { $file->size = $file_size; if (!$content_range && $this->options['discard_aborted_uploads']) { $this->delete_file($filename_with_prefix, $this->options['storage_type']); $file->error = 'abort'; } } $this->set_additional_file_properties($file); // exif データの取得 $exif = array(); if ($this->options['is_save_exif_to_db'] && $extention == 'jpg') { $exif = \Util_Exif::get_exif($file_path, $this->options['exif_accept_tags'], $this->options['exif_ignore_tags']); } if ($this->options['upload_type'] == 'img') { // 大きすぎる場合はリサイズ & 保存ファイルから exif 情報削除 $file_size_before = $file->size; if ($this->options['member_id'] && $this->options['user_type'] === 0 && ($max_size = Site_Upload::get_accepted_max_size($this->options['member_id']))) { $file->size = Site_Upload::check_max_size_and_resize($file_path, $max_size); } // Exif情報の削除 $is_resaved = $file->size != $file_size_before; if ($this->options['is_clear_exif_on_file'] && !$is_resaved) { Site_Upload::clear_exif($file_path); $file->size = File::get_size($file_path); } if (!empty($this->options['accept_sizes'])) { $file->accept_sizes = $this->options['accept_sizes']; } } try { if ($this->options['storage_type'] != 'normal') { $this->save_file2storage($file_path, $filename_with_prefix); $this->delete_file($filename_with_prefix, $this->options['storage_type'], false, false); } $file->id = $this->save_file($file, $exif); } catch (\Exception $e) { if ($this->options['is_output_log_save_error']) { \Util_Toolkit::log_error(sprintf('file save error: %s', $e->getMessage())); } $this->delete_file($filename_with_prefix, $this->options['storage_type']); $file->error = 'ファイルの保存に失敗しました。'; } return $file; }