added upload image

This commit is contained in:
2022-05-21 17:53:27 +02:00
parent 71b92a3424
commit 29e742284f
4 changed files with 105 additions and 1 deletions

View File

@@ -13,6 +13,9 @@ from ..forms import UserCreateForm, TopicForm, TagForm
from ..models.user import User
from ..models.blog_record import Topics, Tags
import os
from PIL import Image
import shutil
import magic
@view_config(route_name='home',
@@ -296,6 +299,7 @@ def tag_edit(request):
renderer='cao_blogr:templates/images.jinja2')
def images(request):
message = ''
folder_path = request.registry.settings['images_dir']
images_list = []
@@ -305,8 +309,46 @@ def images(request):
image.append(f.name)
images_list.append(image)
if 'form.submitted' in request.params:
if request.POST['uploadfile'] != b'':
# récupère le fichier download dans le dossier /tmp
input_file = request.POST['uploadfile'].file
input_name = request.POST['uploadfile'].filename
ext_allowed = ['image/jpeg', 'image/jpg', 'image/png']
mime = magic.from_buffer(input_file.read(), mime=True)
# types de fichiers autorisés ?
if mime not in ext_allowed:
request.session.flash("ERREUR: Le format du fichier n'est pas valide. Téléchargement impossible.", 'danger')
return HTTPFound(location=request.route_url('images'))
# Finally write the data to a temporary file
temp_file_path = os.path.join(folder_path, input_name)
# supprimer le fichier s'il existe déjà
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
# copie le fichier upload dans temp_file
input_file.seek(0)
with open(temp_file_path, 'wb') as output_file:
shutil.copyfileobj(input_file, output_file)
# controler la taille du fichier < 4 Mo
filesize = round(os.path.getsize(temp_file_path) / 1024)
if filesize > 4096:
os.remove(temp_file_path)
request.session.flash("ERREUR: La taille du fichier dépasse la limite autorisée. Téléchargement impossible.", 'danger')
return HTTPFound(location=request.route_url('images'))
# using the Python Image Library (PIL) to resize an image
resize_photos(temp_file_path)
request.session.flash('%s : Ce fichier est téléchargé avec succès.' % input_name, 'success')
return HTTPFound(location=request.route_url('images'))
return {
'page_title': "Images",
'message': message,
'images_list': images_list,
}
@@ -339,3 +381,27 @@ def image_edit(request):
'file_url': request.static_url('cao_blogr:static/img/') + filename,
}
def resize_photos(image_file):
# using the Python Image Library (PIL) to resize an image
img_org = Image.open(image_file)
# get the size of the original image
width_org, height_org = img_org.size
original_size = max(width_org, height_org)
MAX_SIZE = 800
# width or height > than MAX_SIZE ?
if original_size >= MAX_SIZE:
if (width_org > height_org):
resized_width = MAX_SIZE
resized_height = int(round((MAX_SIZE/float(width_org))*height_org))
else:
resized_height = MAX_SIZE
resized_width = int(round((MAX_SIZE/float(height_org))*width_org))
# best down-sizing filter
img_anti = img_org.resize((resized_width, resized_height), Image.ANTIALIAS)
# create a new file name for saving the result
img_anti.save(image_file)
return