Marketing API Version

Image Crops

Provide aspect ratios for images in different ad placements. Facebook crops your image according to your specifications given or if you provide no cropping we display it using defaults. See Ad Image. For example upload an image to use in ad creative`:

use FacebookAds\Object\AdImage;
use FacebookAds\Object\Fields\AdImageFields;

$image = new AdImage(null, 'act_<AD_ACCOUNT_ID>');
$image->{AdImageFields::FILENAME} = '<IMAGE_PATH>';

$image->create();
echo 'Image Hash: '.$image->{AdImageFields::HASH}.PHP_EOL;
from facebookads.adobjects.adimage import AdImage

image = AdImage(parent_id='act_<AD_ACCOUNT_ID>')
image[AdImage.Field.filename] = '<IMAGE_PATH>'
image.remote_create()

# Output image Hash
print(image[AdImage.Field.hash])
AdImage adImage = new AdAccount(act_<AD_ACCOUNT_ID>, context).createAdImage()
  .addUploadFile("filename", new File(<IMAGE_PATH>))
  .execute();
curl \
  -F 'filename=@<IMAGE_PATH>' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.10/act_<AD_ACCOUNT_ID>/adimages

Then, provide ad creative by referencing the image hash returned in the previous call along with cropping:

use FacebookAds\Object\AdCreative;
use FacebookAds\Object\AdCreativeLinkData;
use FacebookAds\Object\Fields\AdCreativeLinkDataFields;
use FacebookAds\Object\AdCreativeObjectStorySpec;
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields;
use FacebookAds\Object\Fields\AdCreativeFields;

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
  AdCreativeLinkDataFields::MESSAGE => 'Ad message',
  AdCreativeLinkDataFields::LINK => '<URL>',
  AdCreativeLinkDataFields::IMAGE_HASH => '<IMAGE_HASH>',
  AdCreativeLinkDataFields::IMAGE_CROPS => array(
    '191x100' => array(
      array(100, 10),
      array(433, 184),
    ),
    '100x72' => array(
      array(160, 20),
      array(371, 172),
    ),
  ),
));

$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
  AdCreativeObjectStorySpecFields::PAGE_ID => <PAGE_ID>,
  AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));

$creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');

$creative->setData(array(
  AdCreativeFields::NAME => 'Image crop creative',
  AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

$creative->create();
from facebookads.adobjects.adcreative import AdCreative
from facebookads.adobjects.adcreativelinkdata import AdCreativeLinkData
from facebookads.adobjects.adcreativeobjectstoryspec \
    import AdCreativeObjectStorySpec

link_data = AdCreativeLinkData()
link_data[AdCreativeLinkData.Field.message] = 'Ad message'
link_data[AdCreativeLinkData.Field.link] = '<URL>'
link_data[AdCreativeLinkData.Field.image_hash] = '<IMAGE_HASH>'
link_data[AdCreativeLinkData.Field.image_crops] = {
    '191x100': [
        [100, 10],
        [433, 184],
    ],
    '100x72': [
        [160, 20],
        [371, 172],
    ],
}

object_story_spec = AdCreativeObjectStorySpec()
object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = <PAGE_ID>
object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data

creative = AdCreative(parent_id='act_<AD_ACCOUNT_ID>')
creative[AdCreative.Field.name] = 'Image crop creative'
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative.remote_create()
curl \
  -F 'name=Image crop creative' \
  -F 'object_story_spec={ 
    "link_data": { 
      "image_crops": {"191x100":[[100,10],[433,184]],"100x72":[[160,20],[371,172]]}, 
      "image_hash": "<IMAGE_HASH>", 
      "link": "<URL>", 
      "message": "Ad message" 
    }, 
    "page_id": "<PAGE_ID>" 
  }' \
  -F 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.10/act_<AD_ACCOUNT_ID>/adcreatives

Crops contains key-value pairs, where the key is a crop key and value is the pixel dimensions of the crop. For all supported keys, see Ads Image Crops Reference.

Provide value as (x, y) coordinates for the upper-left and bottom-right corners of the cropping rectangle. crop key describes an aspect ratio. The aspect ratio of the box specified by width and height must be as close as possible to the aspect ratio in crop key.

An image's origin (0, 0) is at the upper-left corner. The point, (width - 1, height - 1) is at the bottom-right corner.

Requirements

This is an optional setting, but if you do provide it, you should do so for all placements where an ad may appear. For example, if provide it for Right Hand Column and you also want ad in Newsfeed, provide cropping for Newsfeed placement.

Limitations

Image crops are only supported for ad creatives with image_file or image_hash. Page posts are not supported. Values must adhere to these constraints:

  • Points specified by (x, y) must lie within the image. A rectangle that extends beyond the bounds of the image is invalid.
  • The rectangle must be the same aspect ratio as specified by the crop key.
  • Coordinates cannot contain negative values.

Example:

{
   "191x100": [ [100, 10], [433, 184] ],
   "100x72": [ [160, 20], [371, 172] ]
}