MG 2.3 Do you want to replace existing images?

Steff

Active member
Licensed customer
Just putting it out there as I see that there is no official way of replacing images once they have been uploaded.
In my case I have a lot of images that were uploaded a decade ago and now I have better, larger images. I want to keep the image count and comments, so I have a script to do just that.
Is it ok to post that script here in case it is any use for others?
 
This would be a great add-on to release.

I've seen this requested a number of times over the last ~12 years:
etc.
 
The script is a safe replacement tool for an existing XFMG media item’s underlying image file. It is designed so you can swap in a better scan, cleaned version, or de-watermarked image without creating a new media item, which means the same media ID, same URL, and existing comments stay intact.

Replace image script in practice it works like this:

You give it an existing media_id and the path to the new image file. (the url is fine)

A dry run first checks that it can correctly find the existing media linkage and shows the old/new file maing before any change is made. It prints the media ID, the data_id, and the old/new file paths, and you’re told to stop if it errors.

The real run then replaces the file and, with --purge-xfmg-cache, clears the derived XFMG image cache so XenForo regenerates the image variants instead of continuing to show the previous one.

It also backs up the old file and updates the database metadata such as hash, size, and dimensions so XenForo/XFMG still has consistent attachment information after the swap.

After that, you run the two thumbnail rebuild commands so the front end reflects the replacement properly.

So the short version is:
it replaces the file behind an existing media item, preserves the public-facing identity of that item, refreshes cache/metadata, and then relies on XenForo rebuild jobs to regenerate thumbnails.
I'll try and containerize it in to a plugin and release it once it has been tested a little bit more.
 
Back
Top Bottom