Piwigo thumbnail generation script

Piwigo thumbnail generation script

I use Piwigo 1 photo gallery to host my own photos and I am happy with everything expect one thing, thumbnail generation! Every photo in my gallery has a set of thumbnails of different sizes

  • Square 120 x 120 pixels, crop
  • Thumbnail 144 x 144 pixels
  • XXS - tiny 240 x 240 pixels
  • XS - extra small 432 x 324 pixels
  • S - small 576 x 432 pixels
  • M - medium 792 x 594 pixels
  • L - large 1008 x 756 pixels
  • XL - extra large 1224 x 918 pixels
  • XXL - huge 1656 x 1242 pixels

I mostly use the first 6 thumbnail types and Piwigo creates these on demand, but I want to pre-generate all thumbnails for faster performance than wait the thumbnails to be generated when I need to view them for the first time. There is a function in Piwigo to do this, but it often times out for me and my ARM server takes 2 days to generate all thumbnails. I want to be able to generate the thumbnails on a faster computer and then just transfer the thumbnails to my server. So I created a small script to generate the needed thumbnails.

The script

The script uses ImageMagick to create the following sizes:

  • Square 120 x 120 pixels, crop
  • Thumbnail 144 x 144 pixels
  • XXS - tiny 240 x 240 pixels
  • S - small 576 x 432 pixels
  • M - medium 792 x 594 pixels

sourceDir points to the base directory where you store the full resolution photos and destDir points to Piwigo’s internal thumbnail cache directory. It then loops all jpegs found in the sourceDir, does some basic error checking and if the medium thumbnail does not exists in the destDir it will create a set of thumbnails. Therefore the script can be run daily or weekly on or off the server. For every time the script loops 100 photos it will print a status line and execution time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
#Author: Poul Serek

shopt -s globstar

echo "Starting Piwigo thumbnail generation"

#Remember a trailing '/'
sourceDir="/mnt/usbdisk/photos/Presentation/"
destDir="/var/www/html/gallery/_data/i/galleries/"

counter=0
fnNoExt=""
fnExt=""
fnPath=""

STARTTIME=$(date +%s)

for file in "$sourceDir"/**/*.{jpg,JPG,jpeg,JPEG}
do
        if [[ ! -f "$file" ]]
        then
                continue
        fi

        fnNoExt="${file%.*}"
        fnExt="${file##*.}"
        fnPath="${file%/*}"
        fnPath="${fnPath#$sourceDir}"
        fnNoExt="${fnNoExt#$sourceDir}"

        echo "${fnNoExt}"

        mkdir -p "${destDir}${fnPath}"

        #Error checking
        result=$(jpeginfo -c "$file")
        if [[ $result != *"[OK]"* ]]
        then
                echo $result
        fi

        #If the medium thumbnail exists we assume that the rest also exists and skip this image
        if [ ! -f "${destDir}${fnNoExt}-me.${fnExt}" ]; then
                #echo "MISSING! ${destDir}${fnNoExt}-me.${fnExt}"
                #Store correctly oriented base image (medium) in memory. All other thumbnails are created from this
                convert "${file}" -auto-orient -resize 792x594 -write mpr:baseline +delete \
                mpr:baseline -write "${destDir}${fnNoExt}-me.${fnExt}" +delete \
                mpr:baseline -resize 144x144 -write "${destDir}${fnNoExt}-th.${fnExt}" +delete \
                mpr:baseline -resize 240x240 -write "${destDir}${fnNoExt}-2s.${fnExt}" +delete \
                mpr:baseline -resize 576x432 -write "${destDir}${fnNoExt}-sm.${fnExt}" +delete \
                mpr:baseline -define jpeg:size=144x144 -thumbnail 120x120^ -gravity center -extent 120x120 "${destDir}${fnNoExt}-sq.${fnExt}"
        fi
        counter=$[$counter +1]
        if [ $(($counter%100)) -eq 0 ]; then
                ENDTIME=$(date +%s)
                echo "Processed: ${counter} - Executing for $((($ENDTIME - $STARTTIME)/60)) minutes"
        fi
done

ENDTIME=$(date +%s)
echo "It took $((($ENDTIME - $STARTTIME)/60)) minutes to complete this task..."

Example of execution output

The script should be run as the same user as Piwigo is run, for me that is www-data.

$ sudo su -s /bin/bash www-data -c '/home/user/piwi.sh'
Starting Piwigo thumbnail generation
Processed: 100 - Executing for 18 minutes
Processed: 200 - Executing for 36 minutes
It took 38 minutes to complete this task...

And you should now be able to see the thumbnails in Piwigo

Quirks

Some of my photos seems to be stretched when viewing them after clicking on the picture in the gallery

Piwigo stretched image caused by an incorrect width and height attribute on the IMG tag

The thumbnails themselves seem to work fine when opening the thumbnail in a new browser tab. I am sure I must be doing something wrong in the thumbnail generation, but for now I just hacked Piwigo by removing the height and width attributes from themes/default/template/picture_content.tpl:

  1. Open themes/default/template/picture_content.tpl
  2. Delete {$current.selected_derivative->get_size_htm()} from the line beginning with <img
  3. Restart the webserver
Fixed the image by hacking Piwigo and removing the width and height attributes

The stretched thumbnails should now look as expected. Please leave me a comment if you have some questions of improvements to the script!

  1. Piwigo is an open source photo gallery using PHP and MySQL 

Related Posts