Skip to content

How to optimize the size and generation of the preview pictures

Move to Nextcloud - optimize the generated preview pictures

optimize the generated preview pictures

Nextcloud can show previews of pictures located in the private cloud. Therefore it generates preview-pictures in different sizes to serve previews for "all" devices using Nextcloud. The most of them are not needed as the devices only requesting one cropped picture, not all. As example, if a mobile device requesting 256x256 preview picture, these sizes are not needed: 128x128 for example, and so on. But sadly those were also created and filling up the disk space.

size optimization

To find out, which sizes are needed, grep the GET requests from the apache log:

sudo tail -f /var/log/apache2/access.log | grep GET
# device is requesting a 256x256 picture
1111:11:1111:1111:111:1111:1111:1111 - user [23/Dec/2021:10:07:00 +0100] "GET /index.php/apps/files/api/v1/thumbnail/256/256/user/FILE.jpg HTTP/2.0" 200 22041 "-" "Mozilla/5.0 (Android) Nextcloud-android/3.18.1"

# device is requesting a 1080x2394 picture
1111:11:1111:1111:111:1111:1111:1111 - user [23/Dec/2021:10:07:42 +0100] "GET /index.php/apps/files/user/FILE.jpg&x=1080&y=2394&a=1&mode=cover&forceIcon=0 HTTP/2.0" 200 898737 "-" "Mozilla/5.0 (Android) Nextcloud-android/3.18.1"

Once the needed sizes are known (x=width, y=height), they can be set as parameter into the config.php via the occ command line tool of Nextcloud. This tool is executed as web server user. In this case www-data. Please adapt the values to your needs.

sudo -u www-data bash
cd /path/to/nextcloud/files # example /var/www/html/nextcloud

# size of previewed images
php ./occ config:system:set preview_max_x --value 2394
php ./occ config:system:set preview_max_y --value 2394

# the sizes found out above for the generation
php ./occ config:app:set --value="32 64 250 256"  previewgenerator squareSizes 
php ./occ config:app:set --value="250 256 1024 1080 1920" previewgenerator widthSizes
php ./occ config:app:set --value="250 256 1024 1080 1920 2394" previewgenerator heightSizes

# the quality of the previews
php ./occ config:system:set jpeg_quality --value 60
php ./occ config:app:set preview jpeg_quality --value="60"

If the right sizes are set, it is recommend to run a generation of all the pictures. This is also executed as www-data user.

sudo -u www-data bash
cd /path/to/nextcloud/files # example /var/www/html/nextcloud
php ./occ preview:generate-all -vvv

# Can need some depeding on how many pictures are located int he cloud

Once this is done, a pre-generation can be set as cronjob so that previews for newly uploaded images are generated automatically.

sudo -u www-data crontab -e

# insert the following line into the crontab and save it
*/10 * * * * /usr/bin/php -f /var/www/nextcloud/occ preview:pre-generate
Preview Providers

It is also possible to configure for which file types previews should be generated. Therefor the following can be added and modified to the config.php.

'enabledPreviewProviders' => 
array (
0 => 'OC\Preview\PNG',
1 => 'OC\Preview\JPEG',
2 => 'OC\Preview\GIF',
3 => 'OC\Preview\BMP',
4 => 'OC\Preview\XBitmap',
5 => 'OC\Preview\Movie',
6 => 'OC\Preview\PDF',
7 => 'OC\Preview\MP3',
8 => 'OC\Preview\TXT',
9 => 'OC\Preview\MarkDown',
cleanup of the preview files

Before finding out about above parameters for the right preview picture generation, there are many Nextcloud systems which already created needed and also not needed preview pictures. And those filling up the disk space. To cleanup those not needed previews try the following.

  • run the preview-generation from above for all pictures with the new parameters
  • delete all old preview picture from the file system
  • delete all empty folders in the preview folder
sudo -u www-data bash
# navigate to the apdate folder (which contains the preview folder) and create a backup of the preview folder
cd /var/www/html/nextcloud/data/appdata*
cp -r preview{,.old}

# assuming the genration was done in two days, delete all files older than 2 days
cd preview
find . -type f -mtime +2 -delete

# delete all empty folder
find . -type d -empty delete

Run the preview:generate-all from above again. This should be done fast, since every preview picture already exists. If everything went fine, delete the backup of the preview folder.

sudo -u www-data bash
cd /var/www/html/nextcloud/data/appdata*
rm -rf preview.old