我将如何使用干预图像使上传的图像保留其扩展名?
这是我保存用户头像的当前代码:
/**
* Saves avatar in filesystem.
*
* @param UploadedImage $image
* @param string $path
* @return void
*/
public function saveAvatar($image, array $paths)
{
foreach ($paths as $k => $v)
{
//delete any previous user avatars
File::delete(public_path($v));
if ($k == 'big')
{
$encoded = Imagine::make($image['avatar']->getRealPath())
->resize(300, 300);
Imagine::make($encoded)->save(public_path($v), 100);
}
else
{
$encoded = Imagine::make($image['avatar']->getRealPath())
->resize(100, 100);
Imagine::make($encoded)->save(public_path($v), 100);
}
}
}
此代码将文件拆分为 2 个文件和一个大文件和一个原始文件,但默认情况下它们会保存为 jpeg,但是如果我希望 jpeg 和 png 不仅保存一个扩展名怎么办。
这是通过Controller上传的过程(最近改了):
/**
* Uploads and associates user avatar.
*
* @param string $username
* @return void
*/
public function avatar($name)
{
$input = ['avatar' => Input::file('avatar')];
$inputExt = pathinfo( $input->getClientOriginalName() );
if ( ! $this->validator->setRules('avatar')->with($inputExt)->passes())
{
return Redirect::back()->withErrors($this->validator->errors());
}
$this->user->uploadAvatar($inputExt, $name);
return Redirect::back()->withSuccess( trans('users.uploaded avatar success') );
}
这是 UserRepository
/**
* Uploads provided avatar and associates with user.
*
* @param array $input
* @param string $id
* @return void
*/
public function uploadAvatar(array $input, $id)
{
$user = User::find($id);
$paths['big'] = "assets/uploads/avatars/$id";
$paths['small'] = "assets/uploads/avatars/$id.small";
$this->images->saveAvatar($input, $paths);
$user->avatar = $paths['big'];
$user->save();
}