【发布时间】:2020-03-23 16:01:33
【问题描述】:
这里是Laravel新手,想请教如何一张一张上传多张图片。多张图片可在一个选择中使用,但将图片一张一张上传不起作用,并且第一张图片仅在提交后出现。请帮忙。
控制器
if (\Input::file('photos')) {
$base_path =
dirname(__FILE__) . "/../../../../uploads/img/gallery/" . $add->id . "/";
ini_set('gd.jpeg_ignore_warning', 1);
//get last uploaded file name
$files = (\Input::file('photos'));
$list_image = $temp = explode(",", $request->list_image);
foreach ($files as $key => $value) {
if (!is_null($value)) {
$temp = explode(".", $value->getClientOriginalName());
$newfilename = $value->getClientOriginalName();
if (!file_exists($base_path)) {
mkdir($base_path);
chmod($base_path, 0777);
}
$key = array_search($newfilename, $list_image);
if ($key == 0) {
$newfilename = $add->id . '_primary' . '.' . end($temp);
} else {
$newfilename = chr(64 + $key) . '_' . $newfilename;
}
$image = \Image::make($value);
// perform orientation using intervention
$image->orientate();
$new_width = $image->width();
$new_height = $image->height();
if ($new_width > 800) {
$percent = 0.7;
$new_width = $new_width * $percent;
$new_height = $new_height * $percent;
}
// resize image to fixed size
$image->resize($new_width, $new_height);
// create a new Image instance for inserting
$watermark = \Image::make(
public_path() . '/assets/img/icons/watermark.png',
);
// insert watermark at bottom-right corner with 10px offset
$image->insert(
public_path() . '/assets/img/icons/watermark.png',
'bottom-right',
10,
10,
);
$image->save($base_path . $newfilename);
}
}
}
【问题讨论】:
-
你用的是什么版本的 laravel?
-
使用 Laravel 5.1
-
你不必为了上传多个文件而写这么多代码,如果你更新你的 laravel 版本会变得容易得多。