View Single Post
Old 04-18-2022, 08:40 AM  
just a punk
So fuckin' bored
 
just a punk's Avatar
 
Industry Role:
Join Date: Jun 2003
Posts: 32,388

Quote:
Originally Posted by femdomdestiny View Post
Hi. Did anyone already have a need to convert all existing photos on the website from jpg to WEBP or AVIF format?

The site is on WP and there are a few thousand blogs posts which means a lot of images. I know there are plugins like Webp converter for media, but what worries me is that it will do redirects instead of really replacing images in posts.

On top of that, there are many nextgen galleries too.

thanks
If you have a CyberSEO Pro license (since you are a GFY member, I assume you have it ) then you already have all the tools you need. Here we go:

1) If you want to automatically convert images in new posts, just put the following code into the Expert -> Custom PHP code box in your feed settings:


Code:
if (!cseo_post_exists($post)) {
    preg_match_all('/<img.+?src=["\'](.+?)["\'].*?>/is', $post['post_content'] . $post['post_excerpt'], $matches);
    if (isset($matches[1])) {
        foreach ($matches[1] as $link) {
            cseo_store_image($link, $post['post_title'], -1, -1, -1, IMAGETYPE_WEBP);
        }
    }
} else {
    $post = false;
}
Now the plugin will automatically convert post images into the WEBP format.

2) If you want to do the same trick with images in already existing posts, click Post Modification Tools, put the following code into the box and execute it:

Code:
preg_match_all('/<img.+?src=["\'](.+?)["\'].*?>/is', $post->post_content . $post->post_excerpt, $matches);
if (isset($matches[1])) {
    foreach ($matches[1] as $link) {
        $new_link = cseo_save_image($link, $post->post_title, -1, -1, -1, IMAGETYPE_WEBP);
        if ($new_link != $link) {
            $post->post_content = str_replace($link, $new_link, $post->post_content);
            $post->post_excerpt = str_replace($link, $new_link, $post->post_excerpt);
            cseo_delete_media_by_url(array($link));
        }
    }
}
Vualá! All your WordPress post images now converted into WEBM

If you want to convert your images into a different format, simply replace IMAGETYPE_WEBP with the appropriate value, e.g.: IMAGETYPE_JPEG, IMAGETYPE_PNG etc. Find more examples for Modification Tools here: https://www.cyberseo.net/modification-tools/#examples

P.S. Make sure you are using the most recent version of CyberSEO Pro, which is 9.016 ATM.
__________________
Obey the Cowgod
just a punk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote