Timestamps on images circumvent caching
-
The uploaded avatar and cover photos are nice in Ultimate Member, however the server side code add a current timestamp to both image types (regardless of image date). This cause lots of extra image loading.
Skipping the timestamp will load images from cache instead of fetching new ones.
The right thing would be to add a timestamp from the file date, i.e. call filemtime() on the file instead of just current_time(‘timestamp’).
In includes/um-short-functions.php both um_get_cover_uri and um_get_avatar_uri could be improved. There is a filter um_filter_avatar_cache_time in the latter, but it appear to only have the user ID as argument, not the actual avatar file name.
(will try my own patch in the file as I see no way to override these functions, but don’t really like to maintain my own code changes inside Ultimate Member)
-
Hi @erikltz
You can try this code snippet to remove the timestamp from avatar URLs.
add_filter("um_filter_avatar_cache_time","__return_empty_string");
Regards,
Removing the timestamp could cause profile images not updating depending on cache settings in visitor browsers. I got around to modify the mentioned functions. It’s a general improvement of the code and it would be a good improvement in the main distribution for anyone displaying cover and avatar images…! ??
Note: I have pages listing groups of members with their profile photo that caused many lines in the web server log for each page view. I also accept rather large cover images that was fetched again for every page view. These are now fetched ONCE per visitor with the files time stamp – and ONCE again if image is updated. DRASTIC IMPROVEMENT! ??
Pasting complete code for two modified functions in includes/um-short-functions.php (didn’t touch the multisite thang) – not sure it will look good here though:
function um_get_cover_uri( $image, $attrs ) { $uri = false; $uri_common = false; $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); $ratio = str_replace(':1','',UM()->options()->get( 'profile_cover_ratio' ) ); $height = round( $attrs / $ratio ); if ( is_multisite() ) { //multisite fix for old customers $multisite_fix_dir = UM()->uploader()->get_upload_base_dir(); $multisite_fix_url = UM()->uploader()->get_upload_base_url(); $multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir ); $multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url ); if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo{$ext}?" . current_time( 'timestamp' ); } if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo-{$attrs}{$ext}?" . current_time( 'timestamp' ); }elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "cover_photo-{$attrs}x{$height}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/cover_photo-{$attrs}x{$height}{$ext}?". current_time( 'timestamp' ); } } //*** BEGIN: Append time stamp of file rather than current time /EL 2021-01-05 $base_dir = UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR; $base_url = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/"; $fname = "cover_photo-{$attrs}{$ext}"; if( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "cover_photo-{$attrs}x{$height}{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "cover_photo{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } } } if ( ! empty( $uri_common ) && empty( $uri ) ) { $uri = $uri_common; $fname=''; } // Append filemtime if fname is set, otherwise current time stamp $cache_time = ( $fname ? filemtime( $base_dir . $fname ) : current_time( 'timestamp' )); //*** END $uri .= "?{$cache_time}"; return $uri; } function um_get_avatar_uri( $image, $attrs ) { $uri = false; $uri_common = false; $find = false; $ext = '.' . pathinfo( $image, PATHINFO_EXTENSION ); if ( is_multisite() ) { //multisite fix for old customers $multisite_fix_dir = UM()->uploader()->get_upload_base_dir(); $multisite_fix_url = UM()->uploader()->get_upload_base_url(); $multisite_fix_dir = str_replace( DIRECTORY_SEPARATOR . 'sites' . DIRECTORY_SEPARATOR . get_current_blog_id() . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $multisite_fix_dir ); $multisite_fix_url = str_replace( '/sites/' . get_current_blog_id() . '/', '/', $multisite_fix_url ); if ( $attrs == 'original' && file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}"; } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}x{$attrs}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}x{$attrs}{$ext}"; } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$attrs}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$attrs}{$ext}"; } else { $sizes = UM()->options()->get( 'photo_thumb_sizes' ); if ( is_array( $sizes ) ) { $find = um_closest_num( $sizes, $attrs ); } if ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}x{$find}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}x{$find}{$ext}"; } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo-{$find}{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo-{$find}{$ext}"; } elseif ( file_exists( $multisite_fix_dir . um_user( 'ID' ) . DIRECTORY_SEPARATOR . "profile_photo{$ext}" ) ) { $uri_common = $multisite_fix_url . um_user( 'ID' ) . "/profile_photo{$ext}"; } } } //*** BEGIN: Append $base_dir = UM()->uploader()->get_upload_base_dir() . um_user( 'ID' ) . DIRECTORY_SEPARATOR; $base_url = UM()->uploader()->get_upload_base_url() . um_user( 'ID' ) . "/"; $fname = "profile_photo{$ext}"; if ( $attrs == 'original' && file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "profile_photo-{$attrs}x{$attrs}{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "profile_photo-{$attrs}{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $sizes = UM()->options()->get( 'photo_thumb_sizes' ); if ( is_array( $sizes ) ) { $find = um_closest_num( $sizes, $attrs ); } $fname = "profile_photo-{$find}x{$find}{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "profile_photo-{$find}{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } else { $fname = "profile_photo{$ext}"; if ( file_exists( $base_dir . $fname ) ) { $uri = $base_url . $fname; } } } } } } if ( ! empty( $uri_common ) && empty( $uri ) ) { $uri = $uri_common; $fname=''; } /** * UM hook * * @type filter * @title um_filter_avatar_cache_time * @description Change Profile field value if it's empty * @input_vars * [{"var":"$timestamp","type":"timestamp","desc":"Avatar cache time"}, * {"var":"$user_id","type":"int","desc":"User ID"}] * @change_log * ["Since: 2.0"] * @usage add_filter( 'um_filter_avatar_cache_time', 'function_name', 10, 2 ); * @example * <?php * add_filter( 'um_filter_avatar_cache_time', 'my_avatar_cache_time', 10, 2 ); * function my_avatar_cache_time( $timestamp, $user_id ) { * // your code here * return $timestamp; * } * ?> */ if(0) { $cache_time = apply_filters( 'um_filter_avatar_cache_time', current_time( 'timestamp' ), um_user( 'ID' ) ); if ( ! empty( $cache_time ) ) { $uri .= "?{$cache_time}"; } } else { // Append filemtime if fname is set, otherwise current time stamp $cache_time = ( $fname ? filemtime( $base_dir . $fname ) : current_time( 'timestamp' )); $uri .= "?{$cache_time}"; } //*** END return $uri; }
Hi @erikltz
Are you able to create a pull request on our GitHub repo so we can review, test it, and merge it in the next release?
https://github.com/ultimatemember/ultimatememberRegards,
“I’m sorry Da… Champ, I’m afraid I can’t do that” – ohh, well, I probably could but have many things in my project to attend to first so hope one of Your developers are actually interested in making a substantial optimization of Your plugin by using the code lines pasted above.
Regards,
Erik L.Hi @erikltz
Just letting you know that I’ve created an issue on our GitHub repo with your provided enhancement:
https://github.com/ultimatemember/ultimatemember/issues/726Regards,
Hey there!
This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.
Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??
Regards,
- The topic ‘Timestamps on images circumvent caching’ is closed to new replies.