]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/thumbnails/__init__.py
ef995029584c82a3d5aefdb9230d0b8cdf6508f8
[cheatdb.git] / app / blueprints / thumbnails / __init__.py
1 # Content DB
2 # Copyright (C) 2018  rubenwardy
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
18 from flask import abort, send_file, Blueprint, current_app
19
20 bp = Blueprint("thumbnails", __name__)
21
22 import os
23 from PIL import Image
24
25 ALLOWED_RESOLUTIONS=[(100,67), (270,180), (350,233)]
26
27 def mkdir(path):
28         assert path != "" and path is not None
29         try:
30                 if not os.path.isdir(path):
31                         os.mkdir(path)
32         except FileExistsError:
33                 pass
34
35
36 def resize_and_crop(img_path, modified_path, size):
37         try:
38                 img = Image.open(img_path)
39         except FileNotFoundError:
40                 abort(404)
41
42         # Get current and desired ratio for the images
43         img_ratio = img.size[0] / float(img.size[1])
44         ratio = size[0] / float(size[1])
45
46         # Is more portrait than target, scale and crop
47         if ratio > img_ratio:
48                 img = img.resize((int(size[0]), int(size[0] * img.size[1] / img.size[0])),
49                                 Image.BICUBIC)
50                 box = (0, (img.size[1] - size[1]) / 2, img.size[0], (img.size[1] + size[1]) / 2)
51                 img = img.crop(box)
52
53         # Is more landscape than target, scale and crop
54         elif ratio < img_ratio:
55                 img = img.resize((int(size[1] * img.size[0] / img.size[1]), int(size[1])),
56                                 Image.BICUBIC)
57                 box = ((img.size[0] - size[0]) / 2, 0, (img.size[0] + size[0]) / 2, img.size[1])
58                 img = img.crop(box)
59
60         # Is exactly the same ratio as target
61         else:
62                 img = img.resize(size, Image.BICUBIC)
63
64         img.save(modified_path)
65
66
67 @bp.route("/thumbnails/<int:level>/<img>")
68 def make_thumbnail(img, level):
69         if level > len(ALLOWED_RESOLUTIONS) or level <= 0:
70                 abort(403)
71
72         w, h = ALLOWED_RESOLUTIONS[level - 1]
73
74         upload_dir = current_app.config["UPLOAD_DIR"]
75         thumbnail_dir = current_app.config["THUMBNAIL_DIR"]
76         mkdir(thumbnail_dir)
77
78         output_dir = os.path.join(thumbnail_dir, str(level))
79         mkdir(output_dir)
80
81         cache_filepath  = os.path.join(output_dir, img)
82         source_filepath = os.path.join(upload_dir, img)
83
84         resize_and_crop(source_filepath, cache_filepath, (w, h))
85         return send_file(cache_filepath)