This is a simple implementation of this lightweight image cropping library Pure JavaScript Image Crop Component - wouldn't be hard to implement this free cropping library
Be sure to include the required files cropper.css
, cropper.min.js
, [functions.js](https://gist.github.com/Kryptonit3/f3e7e29707ed791087c6#file-functions-js)
This assumes you are using bootstrap with modals, and have a button with the ID of showAvatar
somewhere on your document to invoke the modal.
When processing on the backend you will have the following inputs to work with:
x & y = the starting crop points
width & height = the size of the crop window
img-width & img-height = the size that the full size image needs to be resized to
before cropping due to the use of width:100% and how it
affects the size/x,y points of the crop
You will need to add your action/url to the form and process it how you choose to.
If you are using Laravel you can add this method to your user model
A nice way to manage it on laravel using Intervention Image package Also, there needs to be a 'avatar' varchar/string column on users table add this to a new column migration or your user migration
$table->string('avatar')->nullable();
you should also have an /uploads/avatar
directory in your public
folder
with the following file in it for when pushing to git
https://gist.github.com/Kryptonit3/f3e7e29707ed791087c6#file-gitignore
public function avatar()
{
$avatar = ($this->avatar) ? asset($this->avatar) : asset('img/default-avatar.png');
return $avatar;
}
Be sure to set the default avatar url ('img/default-avatar.png') for users who have not set theirs.
Then in your loops you can do
@foreach($users as $user)
<img src="{{ $user->avatar() }}" alt="{{ $user->username }}" />
@endforeach
That way if you ever change the structure of how your avatars are fetched, you only need to update your method on your User model.
Here is a method for uploading the image to the filesystem
If you have any questions, ask away in the comments! :)
Nice!