I am working on a plugin that gets image URLs from an API, some being PNG, JPG, and others JPG without an extension. I've been trying to use a combination of
media_handle_sideload() to add the images, hoping I could then convert them to WebP and retrieve them via
wp_get_attachment_url(). This part is mostly based off of this code here, which doesn't work for the images with without extensions. (e.g. this URL https://s.yimg.com/ny/api/res/1.2/5kThzlhC8ohGderVoKLJrw--/YXBwaWQ9aGlnaGxhbmRlcjt3PTcwNQ--/https://media.zenfs.com/en/evening_standard_239/b4cd0950143bb1ed165aa095f73a3315)
I've tried this solution, but that didn't fix the issue. I am able to see that the image URLs without extensions are indeed JPGs, however. I saw this by checking against
$tmp rather than
image_type_to_extension( exif_imagetype( $tmp ) ). I was also able to eventually have all image URLs end with
Other than the problematic images without extensions, the other images do become stored in the media library. From here is it possible to use the same attachment URL to convert them to WebP and then serve them on the front end? I haven't been able to find anything concrete about utilizing
imagewp() in a plugin.
Is there something I am missing? Or is my approach to this completely wrong? Essentially I am trying to display WebP versions of the images after users make a request instead of the original format they are returned as.
Thanks in advance to anyone that can help with this.