Cropping images is a common task and an everyday routine for producing content. We believe image crop should happen right in your browser and be intuitive and simple. Firing a dedicated image editor to do the cropping is an overkill.
This tutorial is about enabling your users to crop images in any browser with Uploadcare, no matter the device or browser they use.
There are three simple steps to take:
You will only need the public API Key that points to a target project where the images will go to.
Pick a form in your web app where you want users to crop images. It could be a profile editing form, image upload form, etc.
Note, our file uploader enables your users to upload images from their camera, social media, cloud storage, and more. We call those “Upload Sources,” and you can learn more about them in our docs.
You install the file uploader by adding a couple of
<script> elements to your page
<head>. It comes in a variety of bundles and is
usually served from our CDN.
Here’s how it goes. First, add this to your page
<script>UPLOADCARE_PUBLIC_KEY = 'YOUR_PUBLIC_KEY';</script> <script src="https://ucarecdn.com/libs/widget/3.7.3/uploadcare.full.min.js" charset="utf-8"></script>
YOUR_PUBLIC_KEY with your public API key.
Next, place this
<input> where you want our widgfile uploaderet to appear on your page:
<input name="your_form_field_name" value="" type="hidden" role="uploadcare-uploader" data-images-only data-crop />
The file uploader looks for inputs with
role="uploadcare-uploader" and places its
value attribute may either be empty or contain a CDN URL or UUID of
an image on Uploadcare. The non-empty
value will preload a file into your
file uploader dialog. Otherwise, your users will be prompted to load a file from one
of the enabled Upload Sources. You can learn more about default files in the
file uploader dialog in our docs.
That’s what you should get by now:Loading...
Once you’re done cropping, your image CDN URL gets updated with the
image processing operation.
The file uploader then passes the updated CDN URL to the input
value, and from there you
can use it for other routines.
That’s how the updated CDN URL may look like:
You can define the file uploader crop presets by specifying the
data-crop attribute. This is useful when you want
specific aspect ratios, image dimensions, etc.
You’ve just enabled image crop in your app via Uploadcare without breaking a sweat. Great job!
If you’d like to customize the file uploader to your app’s look, check out the docs section on file uploader styling.
Our file uploader is open source, here's its GitHub repo. Issues and PRs are welcome.