Support » Plugin: Avatar Privacy » Accessing avatar source

  • Resolved defomicron

    (@defomicron)


    Is there a way to get the URL of the original source of the avatar upload, where it is stored in wp-content/avatar-privacy/user-avatar?

    I’d like to get it from a User ID, if possible.

    I’ve noticed that it sometimes takes a moment for avatars to update, and I think it’s because of resizing taking time in the background. I’d like to be able to call the URL of the full sized source of the avatar, based on the user ID, if possible. To avoid the delay. Thanks!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author pepe

    (@pputzer)

    Hi @defomicron! Do you need a programmatic way to access the uploaded avatar or do just want to make sure that the full size image is served? The latter can be done easily via this snippet. Although normally, the resizing should not be an issue, as it is only done once per avatar and size in a customizable period of time (clean-up is done once per week by default, for all images older than 7 days, customizable via avatar_privacy_all_images_max_age and avatar_privacy_all_images_cleanup_interval).

    You can access the raw data using \Avatar_Privacy\Core::get_instance()->get_user_avatar( $user_id );. It will return an array with the keys file (the image path) and type (the MIME type).

    Thread Starter defomicron

    (@defomicron)

    Thanks for the fast reply!

    I am trying to serve the full sized image, but I confess I do not know how to make use of that snippet. I was hoping for a way to modify this:

    <?php echo get_avatar_url(bbp_get_displayed_user_id(), array('size' => 500)); ?>

    to display the original image, instead of a resized image.

    Sometimes in testing when I update the profile picture of a user, it doesn’t immediately display with the above code. It will sometimes take 30 seconds or so, and a refresh. I want to avoid this on the edit profile screen, so it isn’t confusing for users who have updated their profile image.

    Plugin Author pepe

    (@pputzer)

    Try this:

    <?php
    $icon_filter = function( $url, $hash, $size, $args ) {
       return \get_site_url() . '/' . \str_replace( ABSPATH, '', $args['avatar'] );
    };
    
    \add_filter( 'avatar_privacy_user_avatar_icon_url', $icon_filter, 20, 4 );
    echo \get_avatar_url( \bbp_get_displayed_user_id(), [ 'size' => 500 ] );
    \remove_filter( 'avatar_privacy_user_avatar_icon_url', $icon_filter, 20 );
    ?>

    But I’d like to know more about the server environment you are using. Is this a shared hosting situation? If not, I’m pretty sure that there is some browser caching issue and the image generation is not taking 30 seconds (also it is done in the general execution flow, not in the background, so if there was a problem, the whole page would stall/white screen).

    In the past, I’ve noticed that sometimes the avatar was not refreshed on uploading a new one in my dev environment, but not on a production site. I’ve since tried to find a way to reproduce the issue (in order to find a proper fix), but it hasn’t happened again since then. (I’m currently working on 2.4.0 which includes some big internal changes, so I might already have fixed whatever was happening here by accident.)

    Thread Starter defomicron

    (@defomicron)

    That snippet works great, thank you so much.

    It’s a VPS with CentOS 7, mariadb 10.1 and php 7.4. When I encounter the error, I always reload without caching to check, and it doesn’t fix it. That said, it could just be a bug in caching. I have found that when it happens, going to the User section of WordPress admin always shows the correct avatar.

    Edit: If I change a user’s profile image 3-4 times in a row (unrealistic in practice, but this is the easiest way I’ve found to reproduce the error), the snippet you’ve just given me will always show the current profile image. The snippet I was using before will sometimes show a profile image the user had before, not necessarily the last one before it was changed. Verified by having both snippets on the same page.

    • This reply was modified 1 year, 6 months ago by defomicron.
    • This reply was modified 1 year, 6 months ago by defomicron.
    • This reply was modified 1 year, 6 months ago by defomicron.
    Plugin Author pepe

    (@pputzer)

    But this is all in series with the same browser, not with several clients in parallel? Because it sounds a lot like a race condition, which I don’t see when it’s all the same browser. I’ll try if I can reproduce it again in MAMP with several uploads in a row.

    Thread Starter defomicron

    (@defomicron)

    All one browser.

    If I get it to show two different images, then actually visit the source URL of each image, both are correct.

    If I upload a png, then a jpeg, the issue never comes up. Only when it’s two pngs, or two jpegs, which leads me to believe it is the browser cache.

    Plugin Author pepe

    (@pputzer)

    @defomicron Could you try inserting a line into the method send_imagein includes/avatar-privacy/components/class-image-proxy.php (relative to Avatar Privacy’s plugin directory)?

    \header( "Content-Length: {$length}" );
    \header( 'Expires: ' . \gmdate( 'D, d M Y H:i:s \G\M\T', \time() + $cache_time ) );

    should become

    \header( "Content-Length: {$length}" );
    \header( 'Last-Modified: ' . \gmdate( 'D, d M Y H:i:s \G\M\T', \filemtime( $file ) ) );
    \header( 'Expires: ' . \gmdate( 'D, d M Y H:i:s \G\M\T', \time() + $cache_time ) );
    

    I still can’t reproduce this with my current development version, but that line might help.

    Plugin Author pepe

    (@pputzer)

    Sorry @defomicron, I think I had too little sleep last night. Forget the code snippets. I meant an E-tag header that I don’t have in the version I’m working on.

    Forget about the previous post, I’ll do this properly tomorrow.

    Thread Starter defomicron

    (@defomicron)

    Sounds good, thank you for all your help and your excellent plugin

    Plugin Author pepe

    (@pputzer)

    @defomicron Sorry for taking so long, can you try to add this line after \header(...) block shown in my last posting (just add it after the Expires: header) to see if that helps in your situation?

    \header( 'ETag: ' . \md5( $image ) );

    Plugin Author pepe

    (@pputzer)

    Hi @defomicron, have you been able to test that snippet?

    Plugin Author pepe

    (@pputzer)

    @defomicron Did you get the chance to to test the modified version?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Accessing avatar source’ is closed to new replies.