Search and replace REGEX in post content for image filename

I’ve had an issue with importing images from Blogger to WordPress, and somehow it used all medium sized dimensions for my inline post content images, which are way too small.

I’ve tried to resize my medium size dimensions in my WordPress media settings, and then ran the Regenerate Thumbnails plugin, but for some reason the images in my blogger imported posts are still too small (using the old medium image size dimensions).

Read More

I’ve been trying to think of a way to fix this, and I think the easiest method would be to just search and replace in post content wherever there is an image tag that has dimensions on the end, like:

http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded-163x300.png

And replace that with:

http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded.png

By means of some sort of MySQL search and replace using REGEX. I want this to work no matter the image filename, and no matter the dimensions on the end.

So:

http://example.com/wp-content/uploads/2014/05/an-image-filename-123x456.png

would become:

http://example.com/wp-content/uploads/2014/05/an-image-filename.png

And also work regardless of extension (png/gif/jpg).

In an ideal situation, I wouldn’t remove the dimension entirely, but just upsize the dimensions to my large media file size (which is max width: 600, max height: รขยˆยž) maintaining the same aspect ratio as the old dimensions. But I feel this sort of query is much more difficult. And I believe my user uploaded all web ready sized images to begin with.

Has anyone done this before or know of a fix for my problem? Or know of another simpler method without re-importing 2000+ posts from blogger?

Thanks in advance!

EDIT:

Or if someone knows why Regen. Thumbnails wouldn’t work. My images are attached to the posts, but here is what my image tag looks like that’s getting spit out:

<img alt="Comfy Cozy Couture" class="aligncenter size-full wp-image-3654" src="http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded-163x300.png" height="1100" width="600" />

It’s odd because it has the correct dimensions in the width and height attrs but the image source has the old hardcoded dimensions in the filename.

Also previously I tried to do this all with JS but i wanted a more permanent solution. Here’s what I was doing with JS:

$('.entry-content img').prop('src', function (i, v) {
  return v.replace(/-d+xd+.(jpg|png|gif)$/, '.$1');
});

Related posts

Leave a Reply

1 comment

  1. I was able to perform a similar search and replace using the WordPress Search Regrex plugin (https://wordpress.org/plugins/search-regex/) and the following search pattern:

    .-d+xd*.
    

    If you try this, put .-d+xd*. in your search pattern and make sure you select “Regex” in the checkbox. I replaced the pattern with nothing (so essentially removing the “-###x###” portion of the string).

    The plugin will allow you to preview the results before submitting. Search will show you what your pattern is doing. Replace will preview the replace. Replace and save does exactly what it sounds like ๐Ÿ™‚