1

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;"> 
  • 2
    possible duplicate of http://stackoverflow.com/questions/14649645/resize-image-in-php and http://stackoverflow.com/questions/1855996/crop-image-in-php (just look for general PHP solutions, it's not necessarily a Laravel question) – herrjeh42 Jul 05 '16 at 04:18
  • In terms of adding thumbnails, you can use a package like the one mentioned by @codefocus but it may be overkill, particularly if you are using jpgs which will usually compress pretty nicely at 300x300, so it depends on how small the thumbs are (file-size benefit, and sharpness) and how likely the user is to eventually see the larger image (cache benefit). – ldg Jul 05 '16 at 04:42

1 Answers1

3

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.

codefocus
  • 76
  • 3
  • 1
    Agree that you should use a library for this. The one mentioned above apparently has hooks for Laravel, so that should make things even easier for you. While that would give you a nice function like "fit", you would still need to deal with cropping, with something like [this](https://fengyuanchen.github.io/cropper/) or at the high-end a service that does automatic intelligent cropping. – ldg Jul 05 '16 at 04:38