bp_attachments_cover_image_generate_file

Generate the cover image file.

Description

(bool|array) bp_attachments_cover_image_generate_file( (array) $args = array(), (null) $cover_image_class = null ); 

Returns (bool|array)

An array containing cover image data on success, false otherwise.

Parameters (2)

0. $args — Optional. (array) => array()
The args.

Options

  • file (string) => ''

    The absolute path to the image. Required.

  • component (string) => ''

    The component for the object (eg: groups, xprofile). Required.

array(

    /**
     * The absolute path to the image. Required.
     *
     * @type string
     * @default ''
     */
    'file' => '',

    /**
     * The component for the object (eg: groups, xprofile). Required.
     *
     * @type string
     * @default ''
     */
    'component' => ''
);        

1. $cover_image_class — Optional. (null) => null
The class to use to fit the cover image.

Usage

  1. if ( !function_exists( 'bp_attachments_cover_image_generate_file' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-attachments.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'file' => '', 
  7. 'component' => '' 
  8. ); 
  9.  
  10. // The class to use to fit the cover image. 
  11. $cover_image_class = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_attachments_cover_image_generate_file($args, $cover_image_class); 
  15.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-attachments.php  
  1. function bp_attachments_cover_image_generate_file( $args = array(), $cover_image_class = null ) { 
  2. // Bail if an argument is missing. 
  3. if ( empty( $args['file'] ) || empty( $args['component'] ) || empty( $args['cover_image_dir'] ) ) { 
  4. return false; 
  5.  
  6. // Get advised dimensions for the cover image. 
  7. $dimensions = bp_attachments_get_cover_image_dimensions( $args['component'] ); 
  8.  
  9. // No dimensions or the file does not match with the cover image dir, stop! 
  10. if ( false === $dimensions || $args['file'] !== $args['cover_image_dir'] . '/' . wp_basename( $args['file'] ) ) { 
  11. return false; 
  12.  
  13. if ( ! is_a( $cover_image_class, 'BP_Attachment_Cover_Image' ) ) { 
  14. $cover_image_class = new BP_Attachment_Cover_Image(); 
  15.  
  16. // Make sure the file is inside the Cover Image Upload path. 
  17. if ( false === strpos( $args['file'], $cover_image_class->upload_path ) ) { 
  18. return false; 
  19.  
  20. // Resize the image so that it fit with the cover image dimensions. 
  21. $cover_image = $cover_image_class->fit( $args['file'], $dimensions ); 
  22. $is_too_small = false; 
  23.  
  24. // Image is too small in width and height. 
  25. if ( empty( $cover_image ) ) { 
  26. $cover_file = $cover_image_class->generate_filename( $args['file'] ); 
  27. @rename( $args['file'], $cover_file ); 
  28.  
  29. // It's too small! 
  30. $is_too_small = true; 
  31. } elseif ( ! empty( $cover_image['path'] ) ) { 
  32. $cover_file = $cover_image['path']; 
  33.  
  34. // Image is too small in width or height. 
  35. if ( $cover_image['width'] < $dimensions['width'] || $cover_image['height'] < $dimensions['height'] ) { 
  36. $is_too_small = true; 
  37.  
  38. // We were not able to generate the cover image file. 
  39. if ( empty( $cover_file ) ) { 
  40. return false; 
  41.  
  42. // Do some clean up with old cover image, now a new one is set. 
  43. $cover_basename = wp_basename( $cover_file ); 
  44.  
  45. if ( $att_dir = opendir( $args['cover_image_dir'] ) ) { 
  46. while ( false !== ( $attachment_file = readdir( $att_dir ) ) ) { 
  47. // Skip directories and the new cover image. 
  48. if ( 2 < strlen( $attachment_file ) && 0 !== strpos( $attachment_file, '.' ) && $cover_basename !== $attachment_file ) { 
  49. @unlink( $args['cover_image_dir'] . '/' . $attachment_file ); 
  50.  
  51. // Finally return needed data. 
  52. return array( 
  53. 'cover_file' => $cover_file,  
  54. 'cover_basename' => $cover_basename,  
  55. 'is_too_small' => $is_too_small 
  56. );