Skip to content

How to control the zoom level with automatic face detection based image cropping

Many websites now offer their users the ability to upload images and profile pictures, making it a challenge for web designers to maintain a certain graphic design and style when subsequently displaying these images. The profile pictures may need to be smartly cropped to focus on the faces, with some sites that prefer close-ups of faces and others that prefer including more background when displaying images of people.

Cloudinary’s automatic Face Detection feature is an extremely useful method for identifying the face(s) in uploaded images and then making it a simple matter to intelligently crop, position and resize the picture accordingly. However, the resulting image is cropped at a default zoom level, so to be able to control how much of the original image surrounding the face to keep, Cloudinary has introduced the Zoom parameter that can be added to dynamic image delivery URLs with on-the-fly cloud-based transformation.

The zoom parameter (z for URLs) accepts a decimal value that sets the new zoom level as a multiple of the default zoom setting: a value less than 1.0 zooms out and a value greater than 1.0 zooms in. For example, z_0.5 halves the default zoom to 50% and zooms out to include more of the background around the face, while z_2.0 doubles the default zoom to 200% and zooms in to include less of the background around the face. The zoom parameter works together with either the crop or thumb cropping modes while setting the gravity parameter to focus on either a single face (g_face), multiple faces (g_faces) or custom coordinates (g_custom) manually defined by the user when uploading the image. Here are a few examples to show how to use the zoom parameter:

The following example demonstrates how the zoom parameter works together with the thumb cropping mode. This mode generates a thumbnail of an image with the exact given width and height dimensions, while identifying the face and making it the center of the image.

The original proportions are retained, but the image might be scaled to fit in the given dimensions. The zoom parameter will now determine how much to scale the face within the given width and height, with a zoom value less than 1.0 shrinking the face, and a zoom value greater than 1.0 enlarging the face within the image.

Original photo

The following URL dynamically generates a 200×200 thumbnail focusing on the face in the image shown above with the default zoom level.

Loading code examples 200x200 thumbnail with default zoom photo

To include more of the background around the face and decrease the relative size of the face within the image, we can decrease the zoom level to 70% of the default by setting the zoom parameter to 0.7:

Loading code examples 200x200 thumbnail with 70% zoom photo

To include less of the background around the face and increase the relative size of the face within the image, we can increase the zoom level to 130% of the default by setting the zoom parameter to 1.3:

Loading code examples 200x200 thumbnail with 130% zoom photo

The following example demonstrates how the zoom parameter works together with the crop mode. This mode is used to extract a section out of the original image, while retaining the size of the graphics, and together with the gravity parameter set to faces, cropping will center the derived image on the detected faces in the image. The zoom parameter will now determine the dimensions of the extracted image, resulting in a bigger image when the zoom value is less than 1.0, or a smaller image when the zoom value is greater than 1.0.

Original photo

The following URL dynamically crops the image shown above to only display the detected faces at the default zoom level:

Loading code examples Cropped to detected faces with default zoom

To include more of the background around the faces and increase the size of the resulting image, we can decrease the zoom level to 75% of the default by setting the zoom parameter to 0.75:

Loading code examples Cropped to detected faces with 75% zoom

To include less of the background around the faces and decrease the size of the resulting image, we can increase the zoom level to 150% of the default by setting the zoom parameter to 1.5:

Loading code examples Cropped to detected faces with 150% zoom

The following URL dynamically crops the lady.jpg image to only display the face at a zoom level of 90% and with rounded corners, then overlays a circular 150×150 thumbnail of the young_couple.jpg image with a zoom level of 75%. The overlay is also adjusted to display 10 pixels away from the north east corner by using the fl_layer_apply parameter, which allows multiple transformations to be applied on the overlay (see the blog post on transforming overlays for more info).

Loading code examples Cropped to the detected face with 90% zoom and rounded corners, with 150x150 circular thumbnail overlay at 75% zoom and 10 pixels away from the north east corner

As shown in the example above, any of Cloudinary’s image transformations can also be applied to the delivered image.

When automatically cropping images to focus on the detected face(s), the zoom parameter is used to control how much of the original image surrounding the face to keep, and is a powerful method to modify and normalize user generated content to match the graphic design and style of your site.

The new Zoom feature is available to all our free and paid plans. If you don’t have a Cloudinary account, you are welcome to sign up to our free account and try it out.

Back to top

Featured Post