bp_attachments_cover_image_generate_file( array $args = array(), BP_Attachment_Cover_Image|null $cover_image_class = null )

Generate the cover photo file.

Description

Parameters

$args

(Optional)

  • 'file'
    (string) The absolute path to the image. Required.
  • 'component'
    (string) The component for the object (eg: groups, xprofile). Required.
  • 'cover_image_dir'
    (string) The cover photo dir to write the image into. Required.

Default value: array()

$cover_image_class

(Optional) The class to use to fit the cover photo.

Default value: null

Return

(false|array) An array containing cover photo data on success, false otherwise.

Source

File: bp-core/bp-core-attachments.php

1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
function bp_attachments_cover_image_generate_file( $args = array(), $cover_image_class = null ) {
    // Bail if an argument is missing.
    if ( empty( $args['file'] ) || empty( $args['component'] ) || empty( $args['cover_image_dir'] ) ) {
        return false;
    }
 
    // Get advised dimensions for the cover photo.
    $dimensions = bp_attachments_get_cover_image_dimensions( $args['component'] );
 
    // No dimensions or the file does not match with the cover photo dir, stop!
    if ( false === $dimensions || $args['file'] !== $args['cover_image_dir'] . '/' . wp_basename( $args['file'] ) ) {
        return false;
    }
 
    if ( ! is_a( $cover_image_class, 'BP_Attachment_Cover_Image' ) ) {
        $cover_image_class = new BP_Attachment_Cover_Image();
    }
 
    $upload_dir = bp_attachments_cover_image_upload_dir();
 
    // Make sure the file is inside the Cover Photo Upload path.
    if ( false === strpos( $args['file'], $upload_dir['basedir'] ) ) {
        return false;
    }
 
    // Resize the image so that it fit with the cover photo dimensions.
    $cover_image  = $cover_image_class->fit( $args['file'], $dimensions );
    $is_too_small = false;
 
    // Image is too small in width and height.
    if ( empty( $cover_image ) ) {
        $cover_file = $cover_image_class->generate_filename( $args['file'] );
        @rename( $args['file'], $cover_file );
 
        // It's too small!
        $is_too_small = true;
    } elseif ( ! empty( $cover_image['path'] ) ) {
        $cover_file = $cover_image['path'];
 
        // Image is too small in width or height.
        if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) {
            $is_too_small = true;
        }
    }
 
    // We were not able to generate the cover photo file.
    if ( empty( $cover_file ) ) {
        return false;
    }
 
    // Do some clean up with old cover photo, now a new one is set.
    $cover_basename = wp_basename( $cover_file );
 
    if ( $att_dir = opendir( $args['cover_image_dir'] ) ) {
        while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) {
            // Skip directories and the new cover photo.
            if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) {
                @unlink( $args['cover_image_dir'] . '/' . $attachment_file );
            }
        }
    }
 
    // Finally return needed data.
    return array(
        'cover_file'     => $cover_file,
        'cover_basename' => $cover_basename,
        'is_too_small'   => $is_too_small
    );
}

Changelog

Changelog
Version Description
BuddyPress 2.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.