> ## Documentation Index
> This page is part of the Image and Video APIs product. Fetch the complete documentation index for Image and Video APIs at: https://cloudinary.com/documentation/llms-image-and-video-apis.txt?referrer=docpage and then use it to discover all relevant pages before exploring further.
> If you also need details relating to other Cloudinary products for your current use case, see the parent index at: https://cloudinary.com/documentation/llms.txt?referrer=docpage

# Crop images in PHP (video tutorial)


## Overview

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.

## Video tutorial

  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.
{videoTranscript:publicId=training/Crop_Images_in_PHP_-_Dev_Hints}

## Tutorial contents
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.### Introducing the problem
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=0 :sec=00 :player=cld} | Manually cropping batches of images is tedious, if not impossible, even with the best of tools. It's better to use automatic smart cropping. |

### The Cloudinary solution
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=0 :sec=07 :player=cld} | 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. |

### Getting started with the demo
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=0 :sec=20 :player=cld} | You should have a sample image in your product environment. Find the sample image by its public ID, `cld-sample`. |

### Viewing the image in a browser
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=0 :sec=30 :player=cld} | 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. |

### Applying automatic cropping
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=0 :sec=44 :player=cld} | 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](php_configuration_tutorial). 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. |

```php
use Cloudinary\Transformation\Resize;

//Create the image tag with the transformed image
$scaleURL = $cld->image('cld-sample')
    ->resize(Resize::scale()
        ->width(600)
);

echo "Resized sample image:" . "\n";
echo $scaleURL . "\n";
echo "\n";
```

### Run the code and view the image
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=1 :sec=16 :player=cld} | Run the code and open the URL in a new browser window. The browser displays the resized image. |

### Add crop
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=1 :sec=22 :player=cld} | 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`. |

```php
//Create the image tag with the transformed image
$regularCropURL = $cld->image('cld-sample')
    ->resize(Resize::crop()
        ->width(600)
        ->height(600)
);

echo "Sample image cropped without AI:" . "\n";
echo $regularCropURL . "\n";
echo "\n";
```

### Run the code and check the image
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=1 :sec=38 :player=cld} | 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. |

### Smart crop
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=1 :sec=53 :player=cld} | 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. |

```php
use Cloudinary\Transformation\Gravity;

//Create the image tag with the transformed image
$autoCropURL = $cld->image('cld-sample')
    ->resize(Resize::auto()
        ->width(600)
        ->height(600)
        ->gravity(Gravity::auto)
);

echo "Sample image cropped with AI:" . "\n";
echo $autoCropURL . "\n";
echo "\n";
```

### Smart cropping allows you to crop programmatically in bulk
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=2 :sec=13 :player=cld} | 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. |

### Auto crop around the face
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=2 :sec=27 :player=cld} | To crop around the face, such as when creating a portrait thumbnail, use the `face` parameter. |

```php
use Cloudinary\Transformation\Gravity;

//Create the image tag with the transformed image
$faceCropURL = $cld->image('cld-sample')
    ->resize(Resize::auto()
        ->width(600)
        ->height(600)
        ->gravity(Gravity::face())
);

echo "Sample image automatically cropped around the face:" . "\n";
echo $faceCropURL . "\n";
echo "\n";
```

### Batch crop
{table:class=tutorial-bullets}|  |
| --- | --- |
|{videotime:id=media :min=2 :sec=51 :player=cld} | 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. |

```php
// Array of public IDs
$publicIDs = ['winter-fashion', 'girl-leaves', 'leather-bag'];

// Loop through each public ID and apply transformation
foreach ($publicIDs as $publicID) {

    //Create the image tag with the transformed image
    $autoCropURL = $cld->image($publicID)
        ->resize(Resize::auto()
            ->width(600)
            ->height(600)
            ->gravity(Gravity::auto())
    );

    echo "Cropped image for $publicID:" . "\n";
    echo $autoCropURL . "\n";
    echo "\n";
}
```
## Keep learning

> **READING**:
>
> * Watch more [Dev Hints videos](https://www.youtube.com/playlist?list=PL8dVGjLA2oMpaTbvoKCaRNBMQzBUIv7N8) on the [Cloudinary YouTube channel](https://www.youtube.com/cloudinary).

> * Explore the [Transformation reference](transformation_reference) for all available parameters.

> * Learn about [smart cropping](image_automatic_gravity) with gravity settings.

> * Discover [other crop modes](image_crop_modes) for precise image cropping.

## If you like this, you might also like...

  
  
  
    Configure the PHP SDK
    Install and configure the Cloudinary PHP SDK 
  

  
  
  
    Upload Images in PHP
    Upload images to Cloudinary using the PHP SDK 
  

  
  
  
    Gravity Crops for Images
    Indicate which areas to keep when cropping 
  

&nbsp;

&nbsp;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.
&nbsp;