Learn how to programmatically crop images in your PHP applications using Cloudinary's AI-powered gravity settings. Instead of manually cropping images one by one, this tutorial shows you how to automate the process while ensuring that the important parts of your images remain in focus.
This video is brought to you by Cloudinary's video player - embed your own!
Use the controls to set the playback speed, navigate to chapters of interest and select subtitles in your preferred language.
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.
|
|
0:00 |
Manually cropping batches of images is tedious, if not impossible, even with the best of tools. It's better to use automatic smart cropping. |
|
|
0:07 |
Crop your images programmatically in your PHP app with Cloudinary and rest assured that the important parts of the image always remain in focus using intelligent gravity settings. |
|
|
0:20 |
You should have a sample image in your product environment. Find the sample image by its public ID, cld-sample. |
|
|
0:30 |
Copy the image URL and open it in a new browser window to see the image before applying transformations. Note that the original image is big. Cropping and resizing the image also optimizes it to save bandwidth. |
|
|
0:44 |
This tutorial assumes your app is already configured with the PHP SDK. If you still need to configure your app, see Configure the PHP SDK. To resize your images in your PHP app, import the library with use Cloudinary\Transformation\Resize;, reference the image with $cld->image('cld-sample'), and apply the resize transformation with ->resize(Resize::scale()->width(600)). Store the transformation in the $scaleURL variable, then print the URL. |
|
|
1:16 |
Run the code and open the URL in a new browser window. The browser displays the resized image. |
|
|
1:22 |
To fit the image in a bounding box with a specific aspect ratio, update the code to add a height in addition to width, and change the function from scale to crop. |
|
|
1:38 |
Run the code and open the URL in a new browser window. This is a blind crop which cuts out important parts of the image. We need to add automatic gravity to rely on the programmatic crop. |
|
|
1:53 |
Automatic gravity (gravity(Gravity::auto) in the SDK and g_auto in the URL) uses deep learning and saliency algorithms to identify and retain the most important object when cropping an image. To add this feature, import the library with use Cloudinary\Transformation\Gravity;, change Resize::crop() to Resize::auto(), and add the gravity(Gravity::auto) parameter. |
|
|
2:13 |
Run the code and open the URL in a new browser window. The crop works automatically, giving confidence that your images look good when cropped programmatically. |
|
|
2:27 |
To crop around the face, such as when creating a portrait thumbnail, use the face parameter. |
|
|
2:51 |
To make the most of this feature, save time by automatically cropping batches of images at once. Add an array of public IDs and loop through, applying gravity(Gravity::auto()) to each one. |
Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.