How to change Gallery image url in product page ? In function.php?

My code is like

add_filter('wp_get_attachment_url', function($  url){     if(strstr($  _SERVER['HTTP_ACCEPT'], 'image/webp')!=false){         return $  url.".webp";     }else{         return $  url;     } }); 

But only the last image in Gallery changed to xxx.webp . I want to know how to change other image ….I want all of them changed to xxx.webp

I dont want use a plugin (Or if i have to …),Please HELP ,Thanks .