thibaud-rohmer / PhotoShow

A free web gallery in PHP with drag-n-drop support
http://www.photoshow-gallery.com
502 stars 151 forks source link

Allow non quadratic thumbnails: width, height, optional borders (code attached) #359

Open bufemc opened 6 years ago

bufemc commented 6 years ago

I would like to to generate non quadratic thumbnails, too. This could be done by specifying:

thumbnail width: (default: 200) thumbnail height: (default: 200) thumbnail crop: off/on (default: on) thumbnail add borders (to fit exactly thumbnail size): off/on (default: off)

The default values just represent the guessed current behavior, but I would e.g. like to change to this: width: 260 height: 195 crop: off add borders: on

People like the author might prefer cropped pictures (as it is), but I prefer to see the full picture in the thumbnail already, even if there is need to add borders then to achieve same dimension for all images.

I also show some code how I did it so far, but it's in Python 3.6 and might serve just as template: (on the other hand the thumbnail method used here is quick like hell, in tests it was fastest so far)

import os
from PIL import Image, ImageOps

THUMBS_PATH = 'f:/out/'
MAX_SIZE = (260,195) # 7x7
ADD_BORDERS = True

def create_thumbnail(in_path):
    im = Image.open(in_path)
    im.thumbnail(MAX_SIZE, Image.ANTIALIAS)
    out_path = THUMBS_PATH + os.path.basename(in_path)
    # Extend image with border if width or height smaller than MAX_SIZE
    if ADD_BORDERS:
        dw = MAX_SIZE[0] - im.size[0]
        dh = MAX_SIZE[1] - im.size[1]
        dw_half = int(dw / 2)
        dh_half = int(dh / 2)
        ltrb_border = (dw_half, dh_half, dw - dw_half, dh - dh_half)
        im_border = ImageOps.expand(im, border=ltrb_border, fill='black')
        im_border.save(out_path, 'JPEG')
    else:
        im.save(out_path, 'JPEG')

Additionally, but less important, I would like that you can also disable generating "web images" (the "thumbs" with _small suffix), instead just show the original picture (reason: hdd health).