最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Is it possible to convert various image types from remote URLs to WebP and then serve them immediately?

programmeradmin1浏览0评论

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 download_url() and 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 .2/5kThzlhC8ohGderVoKLJrw--/YXBwaWQ9aGlnaGxhbmRlcjt3PTcwNQ--/)

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 $imageurl within image_type_to_extension( exif_imagetype( $tmp ) ). I was also able to eventually have all image URLs end with .tmp.

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.

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 download_url() and 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/ny/api/res/1.2/5kThzlhC8ohGderVoKLJrw--/YXBwaWQ9aGlnaGxhbmRlcjt3PTcwNQ--/https://media.zenfs/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 $imageurl within image_type_to_extension( exif_imagetype( $tmp ) ). I was also able to eventually have all image URLs end with .tmp.

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.

Share Improve this question edited Jan 8, 2021 at 14:50 whatifthis asked Jan 8, 2021 at 4:52 whatifthiswhatifthis 54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

To serve a webP image instead of the original format using the original image URL, you could use a .htaccess redirect rules to redirect any request to the original image to the webP version.

WebP Converter for Media Does a good job at this with files uploaded to WP Media Library.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论