user6369603
user6369603

Reputation:

How to reduce the size and crop an image in larval 5.2

I have users uploading profile pictures. These pictures only look good on the page when they are 300x300 pixels. I do not want the users to have to crop their own images. I need to somehow crop the images myself in the back-end to make them square.

Also, I have set a restriction to the file size in verification, but I am unsure if there is a way to change the file size after uploading. For example, I will displaying several users on the homepage with a smaller version of their profile pictures.Is there any way to reduce the file size on this particular page so the page loads faster.

Summary: I have two issues with images in laravel:

  1. I would like to crop profile pictures during user registration.
  2. I would like to lower the size of images while they are thumbnails.

Here is my code for uploading the profile pictures.

   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');

Here is my view in which I show the user's profile picture.

  <img src={{$image->path}} alt={{$image->path}} style="max-width:300px;max-height:300px;border-radius:10px;"> 

Upvotes: 1

Views: 954

Answers (1)

codefocus
codefocus

Reputation: 76

You'd be best off using a composer package such as InterventionImage for this.

It will use either GD2 or ImageMagick under the hood, depending on what is available on your server.

Upvotes: 3

Related Questions