【发布时间】:2015-02-24 16:26:03
【问题描述】:
我正在使用 Laravel 4 创建一个网站,它允许管理员一次上传大量图片。目前,我能够做到这一点,其中每个图像都有自己的唯一 ID,并放入以相同 ID 命名的自己的文件夹中。
问题是我还需要应用程序上传第二个调整大小(更小)的图像版本以及原始图像。我了解到您必须在客户端调整图像大小,所以我不确定如何保存原始图像以及较小的版本。较小的图像应使用相同的 ID 命名,名称末尾带有某种标识符,例如“-smaller”。
这是我当前的前端;
{{ Form::open(array('url' => 'imageUpload', 'files' => true, 'method' => 'post'))}}
<div class="form-group">
<label for="fileToUpload" class="col-sm-2 control-label">Choose All Images</label>
</br>
{{ Form::file('images[]', ['multiple' => true]) }}
</div>
<div class="col-sm-offset-2 col-sm-10">
{{ Form::submit('Add Photos', ['class' => 'btn btn-large btn-primary openbutton'])}}
<!--<button type="submit" class="btn btn-default">Sign in</button> -->
</div>
{{ Form::close() }}
这是我的控制器;
$files = Input::file('images');
foreach($files as $file) {
$rules = array(
'file' => 'required|mimes:png,gif,jpeg,txt,pdf,doc,rtf|max:9999999999'
);
$validator = \Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$id = Str::random(14);
$id = $race . "-" . $id;
$destinationPath = 'raceImages/' . $id;
//$filename = $id;
$filename = $file->getClientOriginalName();
$mime_type = $file->getMimeType();
$extension = $file->getClientOriginalExtension();
$upload_success = $file->move($destinationPath, $id);
);
} else {
return Redirect::back()->with('error', 'I only accept images.');
}
}
【问题讨论】:
-
为什么不让服务器调整图像大小?
-
这就是我想做的,但我只看到在前端调整大小。如何在服务器端调整大小?
-
您可以查看干预:image.intervention.io
标签: php image laravel upload resize