Is it possible to regenerate all the image formats?

System Information
  • Strapi Version: 4.13.7
  • Operating System: ubuntu 18.04
  • Database: mysql 8
  • Node Version: 18.18
  • NPM Version:
  • Yarn Version: 1.22.19

I changed the dimensions of generated formats when uploading media and know I need to re-generate those images. Is there a way to do it to all the files using Strapi?

Yeah this is a good question. We all need this feature.

I created a first version of a code that does that using a stream to consult the database on demand and regenerate the formats. I will publish it as a plugin, but first I need to make some improvements, for example a button in admin dashboard and some strategy to keep track of the process.

1 Like

Thank you for your service, Sir!

If we talk about why I need something like this:
i change my bucket minio s3 to aws s3 and existing url in database is wrong :slight_smile: . I write a query to change url but formats column is a json. I am not a expert on SQL to change json fields but i am able to think xD regenerate formats :D.

I think regenerating formats in your case is too much processing (as regenerating is a heavy processing and takes a long time) for your need, that is only a database change. I think the best approach for you would be really only update at the database.

1 Like

yeah. I wrote a python code.

import psycopg2
import json

conn = psycopg2.connect(host="localhost", database="example", user="postgres", password="XXXX", port="5432")


def replace_bucket_url(file_id):
    cur = conn.cursor()
    cur.execute("SELECT formats FROM files WHERE id = %s", [file_id])
    file = cur.fetchone()

    formats_dict: dict = file[0]

    if formats_dict is None:
        return

    if formats_dict.get("large"):
        formats_dict["large"]["url"] = formats_dict["large"]["url"].replace(
            "https://bucket.domain.xyz/bucket",
            "https://uploads.domain.com",
        )

    if formats_dict.get("medium"):
        formats_dict["medium"]["url"] = formats_dict["medium"]["url"].replace(
            "https://bucket.domain.xyz/bucket",
            "https://uploads.domain.com",
        )

    if formats_dict.get("small"):
        formats_dict["small"]["url"] = formats_dict["small"]["url"].replace(
            "https://bucket.domain.xyz/bucket",
            "https://uploads.domain.com",
        )

    if formats_dict.get("thumbnail"):
        formats_dict["thumbnail"]["url"] = formats_dict["thumbnail"]["url"].replace(
            "https://bucket.domain.xyz/bucket",
            "https://uploads.domain.com",
        )

    formats_json = json.dumps(formats_dict)

    cur.execute("UPDATE files SET formats = %s WHERE id = %s", [formats_json, file_id])
    cur.execute("COMMIT")


def main():
    cur = conn.cursor()

    # Get the data from the database
    files_response = cur.execute("SELECT id,formats FROM files")
    files_response = cur.fetchall()

    for file in files_response:
        replace_bucket_url(file_id=file[0])


if __name__ == "__main__":
    main()

But I think Strapi need more then this. Every image’s responsive formats should be regenerated by admin panel

1 Like

@metapaiva, can you you share your solution?