Generating preview image thumbnails with liferay default configuration

Blog posts

Generating preview image thumbnails with liferay default configuration

Posted on 26/05/2016 by Charalampos Chrysikopoulos

This seems to be, even now, a subject without a specific documentation. The need of thumbnails is very common especially if you are showing content with images in a public url with a lot of traffic.

Liferay comes with PDFBox build in, that can create images for certain image types. Of course, you can use a lot of extra tools like ImageMagick, or Xuggler to create more sophisticated thumbnails, not only for images, but also for pdfs, documents etc. Have a look here.

In our case, the needs are small. We have jpg images, and we need to have two custom thumbnails for them:

  • 180 x 252
  • 280 x 187

Also, we have already a URL in some of our portlets, that has the form:

/portal/documents/178201/495116/image.jpg/ac34c05e-5ad5-4704-b006-7cdd182dc1a3?version=1.0&t=1464246512402

This url is generated by the class

String com.liferay.portlet.documentlibrary.util.DLUtil.getPreviewURL(FileEntry fileEntry, FileVersion fileVersion, ThemeDisplay themeDisplay, String queryString, boolean appendVersion, boolean absoluteURL)

After a lot of reading in the forum, and after a lot of trying, I came to this conclusion (without being very confident about it):

  • First thing is to define your thumbnails in the portal-ext.properties:
    dl.file.entry.thumbnail.custom1.max.width=180
    dl.file.entry.thumbnail.custom2.max.height=252
    dl.file.entry.thumbnail.custom2.max.width=280
    dl.file.entry.thumbnail.custom2.max.height=187
  • We add the parameter imageThumbnail in out url with the value 2 for the custom1 thumbnail and with the value 3 for the custom2 thumbnail. So our url now, looks like
    /portal/documents/178201/495116/image.jpg/ac34c05e-5ad5-4704-b006-7cdd182dc1a3?version=1.0&t=1464246512402&imageThumbnail=2

It is so simple.

Questions:

Ok, but in all articles I 've read, everybody is defining only 2 custom thumbnails. What if I want to defined 6 of them?
Hm, I don't know the answer. Everything I tried, didn't worked out. Defining another custom3 thumbnails in the properties, didn't work with adding the value 4 in the imageThumbnail parameter.

Havig a better look at the class com.liferay.portlet.documentlibrary.util.DLPreviewableProcessor, it seems that you can use only those two properties, and no more than this. So, the number of the optionally custom thumbnails are exact 2.

There are two properties dl.file.entry.thumbnail.max.width and dl.file.entry.thumbnail.max.height. What if I change them?
Changing them, would affect the thumbnails in the document & media portlet in the control panel. It would eventually break the layout.

How can I recreate the thumbnails of my images?
There is a button in the control panel / Configuration / Server Administration / Resources with the name "Reset preview and thumbnail files for Documents and Media portlet."
The description for that is:

Reset preview and thumbnail files for the Documents and Media portlet: You can send in a request to reset the preview and thumbnail files for each item in your portal’s Documents and Media libraries.

The code that is running in the background. deletes two directories in the file system:

public static void deleteFiles() {
long[] companyIds = PortalUtil.getCompanyIds();
 
for (long companyId : companyIds) {
try {
DLStoreUtil.deleteDirectory(
companyId, REPOSITORY_ID, PREVIEW_PATH);
}
catch (Exception e) {
}
 
try {
DLStoreUtil.deleteDirectory(
companyId, REPOSITORY_ID, THUMBNAIL_PATH);
}
catch (Exception e) {
}
}
}

If you want to generate them programmatically, you could use the following method:

ImageProcessorUtil.generateImages(null, imageFileEntry.getFileVersion());

If you need to check the existence of the thumbnails for a specific image you could call the following method:

ImageProcessorUtil.hasImages(imageFileEntry.getFileVersion())

Some sources from the forum

This entry was posted in Uncategorized and tagged liferay 6.2, thumbnails by Charalampos Chrysikopoulos

Add new comment

You must have Javascript enabled to use this form.