我有用户上传个人资料照片。这些图片只有300x300像素时才能在页面上看起来不错。我不希望用户必须裁剪自己的图像。我需要以某种方式自己在后端裁剪图像,使它们成为正方形。如何减少大小和裁剪幼虫图像5.2
此外,我在验证中对文件大小设置了限制,但我不确定是否有方法在上传后更改文件大小。例如,我会在主页上显示多个用户,并且他们的个人资料图片的版本较小。有什么方法可以减少此特定页面上的文件大小,以便页面加载速度更快。
摘要:我有laravel与图像的两个问题:
- 我想在用户注册过程裁剪资料图片。
- 我想在缩略图的同时降低图像的大小。
这是我上传档案图片的代码。
public function register(Request $request)
{
$this->validate($request,[
'first_name' => 'required|max:255',
'family_name' => 'required|max:255',
'password' => 'required|min:6|confirmed',
'picture' => 'mimes:jpeg,png|required|max:500',
// MAKE DESCRIPTION REQUIRED ON DEPLOY 'description' => 'required|min:50'
]);
$user = new User;
$user->first_name = $request->first_name;
$user->family_name = $request->family_name;
$user->password = $request->password;
$user->save();
$file = Input::file('picture');
$file->move(public_path().'/images/',$user->id.'.jpg');
$image = new Image;
$image->path='/images/'.$user->id.'.jpg';
$image->user_id = $user->id;
$image->save();
flash('Thank you for registering. Please verify your email address');
这是我在其中显示用户的个人资料图片的视图。
<img src={{$image->path}} alt={{$image->path}} style="max-width:300px;max-height:300px;border-radius:10px;">
可能的重复http://stackoverflow.com/questions/14649645/resize-image-in-php和http://stackoverflow.com/questions/1855996/crop-image-in-php(只是寻找一般PHP的解决方案,它不一定是一个Laravel的问题) – herrjeh42
在添加缩略图方面,你可以使用像@codefocus提到的包,但它可能是矫枉过正的,特别是如果你使用的jpgs通常会在300x300上很好地压缩,所以它取决于拇指的大小(文件大小的好处和清晰度)以及用户最终看到较大图像的可能性(缓存优势)。 – ldg