groovy
images
thumbnails
tools
Jahia 7.3
Jahia 8
How to (re)generate image thumbnails
Question
If you want to create a new thumbnail size for your images or if you want to regenerate all existing thumbnails for your images,
Answer
Thumbnails are generated on upload or on image update with such a rule. Here is an example of such a rule, coming from the default module https://github.com/Jahia/default/blob/master/src/main/resources/META-INF/rules.drl
rule "Image update"
salience 25
when
A file content has been modified
- the mimetype matches image/.*
- not in operation import
then
> long timer = System.currentTimeMillis();
Add the type jmix:image
Set the property j:width of the node with the width of the image
Set the property j:height of the node with the height of the image
Create an image "thumbnail" of size 150
Create an image "thumbnail2" of size 350
Dispose image
Log "Image " + node.getPath() + " updated in " + (System.currentTimeMillis() - timer) + " ms"
end
So if you want to create a new thumbnail size for your images or if you want to regenerate all existing thumbnails for your images, then you will need to replay this rule for all your images. Here is a useful script that you can play from the JCR console
QueryResult queryResult = null;
Query q = session.getWorkspace().getQueryManager().createQuery("SELECT * FROM [jnt:resource ] AS r WHERE name(r)='jcr:content' and ISDESCENDANTNODE('/sites') ", Query.JCR_SQL2);
queryResult = q.execute();
MultipleNodeIterator iterator = queryResult.getNodes();
while(iterator.hasNext()) {
JCRNodeWrapper node = (JCRNodeWrapper) iterator.nextNode();
if (node.getParent().isNodeType("jmix:image")) {
log.info(node.getPath());
try {
def data = node.getProperty("jcr:data").getBinary();
node.setProperty("jcr:data", data);
} catch (javax.jcr.PathNotFoundException e) {
log.info("Could not find jcr:data on " + node.getPath());
}
node.save();
}
}