3

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).

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+x\d+\.(jpg|png|gif)$/, '.$1');
});
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Corey
  • 2,453
  • 4
  • 35
  • 63
  • Search: `(https?:\/\/\S*?)-\d+x\d+([.](?:png|gif|jpe?g))` and replace with: `\1\2`. Sorry I don't have the motivation to do a full answer, hope this helps :) – Sam Jun 06 '14 at 15:22
  • @Sam Thanks, I've just never done a REGEX replace for MySQL (or even know if it's possible). Still researching it myself. – Corey Jun 06 '14 at 15:26
  • Doesn't look supported in MySQL, but there is [this workaround](http://stackoverflow.com/questions/986826/how-to-do-a-regular-expression-replace-in-mysql). Might be worth throwing together a quick PHP script that loops through your 2k+ posts, parses the links and posts updates. Then you would also be able to use [`preg_replace_callback()`](http://www.php.net/manual/en/function.preg-replace-callback.php) to take care of your "max-dimensions" need. – Sam Jun 06 '14 at 15:31
  • @Sam thanks again. I've added a little more detail about what my image tag looks like that's getting spit out, and why Regen. Thumbnails wouldn't work. Odd that the attrs are correct. – Corey Jun 06 '14 at 15:48

1 Answers1

3

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+x\d*.

If you try this, put .-\d+x\d*. 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 :-)

Evan Volgas
  • 2,900
  • 3
  • 19
  • 30
  • I think that REGEX is not exact enough, it was matching a few times in other filenames for ex: `a-4ximage-filename-163x300.png`. I tried to use this one `-\d+x\d+\.` and replace with a '.', but I kept getting error "Invalid regular expression: No ending delimiter '-' found" – Corey Jun 06 '14 at 16:18
  • 1
    This works better: `-\d+x\d+\.-`. But doesn't include the first dash. – Corey Jun 06 '14 at 16:20
  • 1
    I think I've got it now: `.-\d+x\d+\..`. Going to try search and replace now. – Corey Jun 06 '14 at 16:23
  • 1
    Here's what I did, I did two Search & Replaces: one with this REGEX: `.-300+x\d+\..` replaced with `.` and another search with this REGEX: `.-\d+x300+\..` replaced with `.`. I did it this way because I knew that the image tags with the wrong dimensions were either exactly 300px tall or 300px wide. This way I know I didn't replace unnecessary ones. This fixed my issues. Thanks @evanv! – Corey Jun 06 '14 at 16:31
  • No prob bob. Glad it helped. – Evan Volgas Jun 06 '14 at 16:31