0

I'm migrating my site from Drupal 6 to Drupal 7, and while most of it just worked after migration, the change from the Image module to the core image handling left a large number of broken images on the site.

Images that were inserted in the node body were not handled at all by the migration, and the links are now in an invalid format. Fortunately, fixing those links is pretty straightforward, I just need to change

http://mysite.com/sites/default/files/images/[image-name].[style].jpg

to

http://mysite.com/sites/default/files/styles/[style]/public/images/[image-name].jpg

This looks like it should be relatively straightforward to do, but I didn't find an easy way to do this replacement. I found the Search and Replace Scanner module, but that doesn't seem to allow using parts of the original match in the substitution.

How can I fix these image links all at once without having to do it manually?

1

Assuming MySQL, you can fix that up with a search and replace SQL query.

Perhaps easiest from the MySQL command line, though could also use db_query.

The pattern is:

UPDATE tablename SET fieldname = REPLACE(fieldname, 'search', 'replace');

Something like:

UPDATE field_data_body SET body_value = REPLACE(body_value, 'sites/default/files/images/', 'sites/default/files/styles/[style]/public/images/');

UPDATE field_revision_body SET body_value = REPLACE(body_value, 'sites/default/files/images/', 'sites/default/files/styles/[style]/public/images/');

Backup the database first.

You may need to do it one image style at a time, or else make all the images uses a new standard style.

More info: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

  • This seems to have the same limitation the Search and Replace Scanner module has. I need to get the image style out of the original filename, put that one into the new path and remove the image style part out of the filename. – Mad Scientist Jun 22 '13 at 9:26
  • Yeah, you may need to do it one image style at a time, or else make all the images uses a new standard style. – David Thomas Jun 22 '13 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.