Paste one prompt into your AI coding assistant and it sets up Cloudinary for you: installs the SDK, configures skills and MCP servers, and validates your setup.
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.
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.
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.
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.
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.
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.
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.
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.
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.
✔️ Feedback sent!
✖️
Error
Unfortunately there's been an error sending your feedback.