如何使用干预图像制作上传的图片?
这是我保存用户头像的当前代码:
/**
* 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个文件和大文件和原始文件,但默认情况下会将其保存为jpegs,但如果我希望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();
}
答案 0 :(得分:0)
这是我为特定项目保存不同大小图像的代码,我认为它可以回答您的问题。
/**
* Save image. Generate Different Size versions.
*
* @param Request $request
* @param null $id
* @return mixed
*/
public function storeImages(Request $request, $id = null)
{
try {
$project = Project::find($id);
$imageInfo = pathinfo(Input::file('image')->getClientOriginalName());
//append image name with project id
$imageName = $imageInfo['filename'] . '_' . $id . '.' . $imageInfo['extension'];
$smallImageName = $imageInfo['filename'] . '_' . $id . '_small' . '.' . $imageInfo['extension'];
$thumbImageName = $imageInfo['filename'] . '_' . $id . '_thumb' . '.' . $imageInfo['extension'];
if (Image::where('name', '=', $imageName)->count()) { //if already have this image, delete it
Image::where('name', '=', $imageName)->delete();
}
$prefix = Image::getPathPrefix();
$uploadPath = config('app.image_paths.PROJECT') . $id . DIRECTORY_SEPARATOR;
if (!Storage::exists($uploadPath)) {
Storage::makeDirectory($uploadPath);
}
$image = \Image::make(Input::file('image'))->save($prefix . $uploadPath . $imageName);
$image->fit(config('app.image_size.SMALL.height'),
config('app.image_size.SMALL.width'))->save($prefix . $uploadPath . $smallImageName);
$image->fit(config('app.image_size.THUMB.height'),
config('app.image_size.THUMB.width'))->save($prefix . $uploadPath . $thumbImageName);
$imageObj = Image::create([
'name' => $imageName,
'small_image' => $smallImageName,
'thumb_image' => $thumbImageName,
'original_filename' => $imageInfo['basename'],
'mime' => $image->mime(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
'upload_path' => $uploadPath
]);
$project->images()->save($imageObj);
return json_encode([
'jsonrpc' => '2.0',
'result' => 'OK',
'id' => $imageObj->id
]);
} catch (\Exception $e) {
return json_encode([
'jsonrpc' => '2.0',
'result' => false,
'id' => $e->getMessage()
]);
}
}
答案 1 :(得分:0)
对于任何偶然发现这一点的人:
Intervention\Image\Image::save()
图像类型将由文件扩展名定义。例如,如果传递foo.jpg,图像将保存为JPG文件。 如果没有扩展名 可用时,库将首先尝试使用图像的MIME类型来定义编码,如果这也失败,则图像将被编码为JPEG。
http://image.intervention.io/api/save
此功能仅将图像编码为正确的mime类型,并且不添加扩展名。您仍然需要检测文件的扩展名并自行附加。
答案 2 :(得分:0)
我认为问题在于你是双重“制造”。
这应解决问题:
/**
* 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)->save(public_path($v), 100);
}
else
{
$encoded = Imagine::make($image['avatar']->getRealPath())
->resize(100, 100)->save(public_path($v), 100);
}
}
}