I would like to create a simple script to programmatically rename existing, already uploaded product images (without having to re-upload them or use the admin backend).
To process this in manageable chunks, I'm thinking something along the lines of:
- specify a category ID, get product collection
foreach
product, get all product imagesforeach
image, rename file on server, update database table(s), following naming convention:
$new_img_name = 'company-name-' . strtolower(preg_replace("/\W+/m", "-", $_product->getName())) . '-' . $product_image_number_incremental;
Steps 1 and 2 above I can do, but I'm not having any luck changing the actual file name on the server and in the database—I can only seem to find programmatic ways to change things like image label.
Thoughts?
—
Details:
- Magento CE 1.9.1.1
- approx. 900 products
- approx. 10 images per product
Reasoning:
- I'd like our product images to follow a more SEO-friendly naming convention that includes our company name, the product name, and style information—preferably without all the manual labor of downloading, renaming, and re-uploading all product images (there are a lot…)
Best Answer
Please make sure the following things before running the script
.