BP_Attachment::edit_image( string $attachment_type, array $args = array() )

Edit an image file to resize it or rotate it

Description

Parameters

$attachment_type

(Required) The attachment type (eg: avatar or cover_image). Required.

$args

(Optional)

  • 'file'
    (string) Absolute path to the image file (required).
  • 'max_w'
    (int) Max width attribute for the editor's resize method (optional).
  • 'max_h'
    (int) Max height attribute for the editor's resize method (optional).
  • 'crop'
    (bool) Crop attribute for the editor's resize method (optional).
  • 'rotate'
    (float) Angle for the editor's rotate method (optional).
  • 'quality'
    (int) Compression quality on a 1-100% scale (optional).
  • 'save'
    (bool) Whether to use the editor's save method or not (optional).

Default value: array()

Return

(string|WP_Image_Editor|WP_Error) The edited image path or the WP_Image_Editor object in case of success, an WP_Error object otherwise.

Source

File: bp-core/classes/class-bp-attachment.php

628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
public static function edit_image( $attachment_type, $args = array() ) {
    if ( empty( $attachment_type ) ) {
        return new WP_Error( 'missing_parameter' );
    }
 
    $r = bp_parse_args( $args, array(
        'file'   => '',
        'max_w'   => 0,
        'max_h'   => 0,
        'crop'    => false,
        'rotate'  => 0,
        'quality' => 90,
        'save'    => true,
    ), 'attachment_' . $attachment_type . '_edit_image' );
 
    // Make sure we have to edit the image.
    if ( empty( $r['max_w'] ) && empty( $r['max_h'] ) && empty( $r['rotate'] ) && empty( $r['file'] ) ) {
        return new WP_Error( 'missing_parameter' );
    }
 
    // Get the image editor.
    $editor = wp_get_image_editor( $r['file'] );
 
    if ( is_wp_error( $editor ) ) {
        return $editor;
    }
 
    $editor->set_quality( $r['quality'] );
 
    if ( ! empty( $r['rotate'] ) ) {
        $rotated = $editor->rotate( $r['rotate'] );
 
        // Stop in case of error.
        if ( is_wp_error( $rotated ) ) {
            return $rotated;
        }
    }
 
    if ( ! empty( $r['max_w'] ) || ! empty( $r['max_h'] ) ) {
        $resized = $editor->resize( $r['max_w'], $r['max_h'], $r['crop'] );
 
        // Stop in case of error.
        if ( is_wp_error( $resized ) ) {
            return $resized;
        }
    }
 
    // Use the editor save method to get a path to the edited image.
    if ( true === $r['save'] ) {
        return $editor->save( $editor->generate_filename() );
 
    // Need to do some other edit actions or use a specific method to save file.
    } else {
        return $editor;
    }
}

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.