You want to enjoy life, don't you? If you get your job done quickly and your job is fun, that's good isn't it? That's the purpose of life, partly. Your life is better.
—Yukihiro Matsumoto
Needing to rely on support for the library.
Implementing with technologies such as Vue and Rails may not be straightforward.
Customization can sometimes be difficult.
Bloat of a library may include unneeded/unwanted features or behavior.
Support is typically not an issue with libraries like Vue and Ruby on Rails because of the overwhelming community support, but it can be an issue with many JavaScript libraries. One feature common in web apps is an image-cropping tool, which there are certainly a few libraries out there that accomplish this. In this tutorial, we will roll our own image cropper using Vue and integrate it with direct uploads and the latest feature in Rails as of version 5.2 which is Active Storage. The biggest advantage of rolling your own is the learning experience, and that’s what we will do in this chapter.
The Avatar
The avatar is one of the most common features in social media and other types of web applications. Often you would need to prepare a good image for yourself ahead of time because it is common for applications to automatically crop your avatar. This tutorial will show you how to build an interactive cropper with two basic goals in mind. It will accept any size image and allow you to pan and scale the image so that the area that is cropped is easily customized for the user.
It can be accomplished by supporting an original size uploaded image, allowing the user to pan the image to the appropriate location and a range slider to allow them to scale the image appropriately. Once these bounds are defined, the original image and bounds can be saved and processed as a variant as supported by Active Storage.
Active storage help and dependencies
This tutorial requires the web_processing ruby gem and activestorage npm package to be installed. More info and documentation about Active Storage can be found at https://edgeguides.rubyonrails.org/active_storage_overview.html
The User Profile
Direct uploads
Direct uploads is a feature supported by Active Storage that allows you to save some server resources by having image and file uploads go directly from a user’s browser to a cloud object storage service such as Amazon S3.
Vue Cropper Component
Loading the Image
Panning the Image
Scaling the Image
ImageMagick Processing
We mentioned earlier that we would use the avatar_crop attribute to store the image geometry we want to use. To get it in the correct format, we can define it as a computed property and bind it to a value on a hidden input field. When the form is submitted, the dimensions are saved, and we can apply the geometry as a variant when displaying the avatar. The computed method, input field, and avatar link can be seen in the following code.
And that concludes the image-cropping tutorial. We can see how to achieve a very effective cropping tool with very little effort and avoid the frontend development emergency off ramp, and it was a lot of fun in the process.
Wrap-up and the Next Step
That concludes our hands-on part of the book. We find it best to learn by example and hope that these tutorials provided a lot of insight into what can be accomplished with Vue and Ruby on Rails. In the next chapter, we shift our gears to turbo mode for your Vue on Rails project with deployment, testing, and troubleshooting without losing your mind.