Unable to successfully crop member profile photo (avatar) #303
emaralive
started this conversation in
Coding Related
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Note
This discussion is experimental such that it tests the notification within Slack for discussions from this repository (bp-documentation) and a way for me to externalize my notes, such that others can eyeball this and make comment, if they inclined to do so. This may lead to an issue submitted to create documentation for the Avatar process and lead to a Trac ticket such that this anomaly can be fixed.
The inability to successfully crop an image to be used as a profile photo via the front end (change profile photo) or the back end (extended profile) occurs when the Modern Image Formats plugin is installed, activated and configured to convert uploaded images to the WordPress media library as the AVIF image output format.
This was made apparent by the Any hope for BuddyPress to support AVIF file type soon? support forum topic. There may be other through lines such as the I cannot crop the profile photo support forum topic and Trac ticket 9208.
PHP AVIF support for the GD extension appears to be v8.1+ (see PHP 8.1: GD: AVIF image support). However, the Modern Image Formats plugin allows for selection of AVIF with v8.0.30 (maybe this is through the imagick extension - note this is unconfirmed and speculation). When AVIF is configured via the aforementioned plugin any attempt to upload and crop an image results with the following user defined message:
There was a problem cropping your profile photo.
With PHP v8.0.30 error warnings are generated, e.g,:
With PHP v8.0.30 & 8.1.31 there are other observable symptoms, see the following screenshot:
Upon further review, PHP v8.1.31 produces a user-defined error message of:
The aspect ratio of the image you selected is too great compared to the profile photo one. For best results, upload a picture having an aspect ratio closer to 1.00.
The following screenshot illustrates the aforementioned user-defined error message:
With PHP v8.2.27, v8.3.17 & v8.4.4 the cropping page appears to be normal, see the following screenshot:
Furthermore, it appears the image that is used for an avatar (original image) has the file extension of avif with the aforementioned plugin installed, activated and configured for AVIF. For example, the original is disc.png and in the avatar upload folder the image is disc-450x450.avif, meaning without the plugin, uploading disc.png would result in disc-450x450.png within the avatar upload folder.
Tip
The AVIF mime type is, currently, not an allowed mime type for BuddyPress.
As an example to the tip:
The environment used to investigate can be viewed by expanding the details for "Environment"
Environment
Observations - Two things can be true at the same time
The missing bounding box for cropping appears to be related to the original size of the uploaded image, e.g. images larger than 450 x 450 cause the bounding box issue due the triggering of a resize process, this appears to only apply to PHP versions - 8.0.30 & 8.1.31. For the indicated PHP versions, when an image that is less than or equal to 450 x 450 the bounding box appears and the image can be cropped, as normal.
The threshold for when to resize an uploaded image appears to be based on the
BP_AVATAR_ORIGINAL_MAX_WIDTH
constant which has a default value of 450 (in pixels). The following Cases provide some level of detail as to what is occurring.Cases
Case 1
Case 2
Case 3
Array of supported image types when there is a failed crop attempt for the mime type of image/avif
Array of supported image types when there is a successful crop for the mime type of image/avif
Given:
$file = '/wp-content/uploads/avatars/1/disc-450x450.avif'
PHP 8.0.30
PHP 8.1.31
PHP 8.2.27, 8.3.17 & 8.4.4
wp_getimagesize()
functionWhat was not previously mentioned
Neither complainant mentioned they had issues with Group profile photos (avatar). Fortunately, this situation also applies to the Group profile photo (avatar), therefore the solution will solve for both, i.e., for Members and for Groups.
Furthermore during the group creation process the step for cropping an image does not remain at that step after a failed crop attempt nor indicates that there was a failure e.g.:
There was a problem cropping your profile photo.
Meaning, the process just moves on to the next step. However, if an attempt is made after the group has been created (Manage > Photo) then the indicated user-defined message presents itself.
Special note to self
WordPress utilizes the
wp_image_editor_supports()
function (Tests whether there is an editor that supports a given mime type of methods) to determine, e.g., does the server support a given mime type. This appears to be independent of the PHP version and may take into account of the imagick extension capability - this is pure speculation at this point.The point is that I'm observing behavior on a different server that allows for PHP version 7.4.33 to crop the image/avif mime type. There are differences in Environment, e.g.:
Environment 2
The difference may be in the build for imagick extension, e.g., includes avif support for a given PHP version, albeit, imagick versions could be the same or there is some unknown factors that dictate the difference. That stated, as long as the
wp_image_editor_supports()
is used to determine if the image/avif mime type is supported then, all should work out, accordingly.The
bp_attachments_get_allowed_types()
function could be modified or thebp_attachments_get_allowed_types
could be hooked with a callback to include a conditional such as, see https://github.com/buddypress/buddypress/blob/14.3.3/src/bp-core/bp-core-attachments.php#L163-L222):The above resolves the cropping issue for the image/avif mime type. To resolve the crop bounding box issue is to replace the
getimagesize()
function with thewp_getimagesize()
function, see https://github.com/buddypress/buddypress/blob/14.3.3/src/bp-core/bp-core-avatars.php#L1187, e.g.:From:
To:
Additionally, this change eliminates 2 PHP error warnings associated with v8.0.30 & v7.4.33 (
bp-core-avatars.php
lines 1201 & 1202) .Furthermore, to resolve the 3rd PHP error warning associated with v8.0.30 & v7.4.33 (
class-bp-attachment-avatar.php
line 311) is to replace thegetimagesize()
function with thewp_getimagesize()
function, see https://github.com/buddypress/buddypress/blob/14.3.3/src/bp-core/classes/class-bp-attachment-avatar.php#L310, e.g.:From:
To:
The next area to look at is the REST API since the
getImagesize()
function is used by V1, see https://github.com/buddypress/BP-REST/blob/0.8/includes/bp-attachments/classes/trait-attachments.php#L315 and for V2, see https://github.com/buddypress/buddypress/blob/master/src/bp-core/classes/trait-attachments.php#L316:One other note to make, if there is an unsuccessful crop attempt or is an image is uploaded and the user leaves the upload tab, the image that was uploaded (original) remains. The original image is only removed after a successful crop, therefore there should be a cleanup/tidy up check/routine to make sure that there are not residual original images remaining.
Other image mime types allowed by WordPress
BuddyPress is not keeping up with WordPress in regards to allowed mime types for images and as it stands the full complement as of WP 6.7.2 is:
Perhaps, the heic and heif mime types should be considered as allowable for BuddyPress. Additionally, for WP 6.7+ there is automatic conversion of hiec images to jpeg.
Beta Was this translation helpful? Give feedback.
All reactions