Skip to content

Focal point picker php code example doesn't work if x or y is 0 #21

@jjfer

Description

@jjfer

Focal point might be 0, 0. Checking x and y with isset() fails if value is 0.

This code works:

/**
 * Get css object-position attribute by focal point
 *
 * @param $focal_point array
 *
 * @return string
 */
function get_focal_point_style( $focal_point ) {
    if ( ! $focal_point || ! is_array( $focal_point ) ) {
        return '';
    }

    $focal_point_x = array_key_exists( 'x', $focal_point ) ? $focal_point['x'] * 100 : false;
    $focal_point_y = array_key_exists( 'y', $focal_point ) ? $focal_point['y'] * 100 : false;

    if ( $focal_point_x !== false && $focal_point_y !== false ) {
        return "object-position: {$focal_point_x}% {$focal_point_y}%";
    }

    return '';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions