Remove support for bmp and gifs, and compression script ignores folders without images

This commit is contained in:
Keplyx 2018-04-23 12:37:45 +02:00
parent 99b7a237a3
commit 0c4c90f0ed
2 changed files with 24 additions and 13 deletions

View file

@ -70,7 +70,7 @@ function getPhotos($dir)
$path = realpath($dir.DIRECTORY_SEPARATOR.$value); $path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if (!is_dir($path)) { if (!is_dir($path)) {
$ext = pathinfo($path, PATHINFO_EXTENSION); $ext = pathinfo($path, PATHINFO_EXTENSION);
if ($ext == "bmp" || $ext == "jpg" || $ext == "jpeg" || $ext == "png" || $ext == "gif") { if ($ext == "jpg" || $ext == "jpeg" || $ext == "png") {
$imageSrc = $dir.DIRECTORY_SEPARATOR.$value; $imageSrc = $dir.DIRECTORY_SEPARATOR.$value;
$imageId = "photo-".$displayedItems; $imageId = "photo-".$displayedItems;
include("includes/photos/photo_template.php"); include("includes/photos/photo_template.php");

View file

@ -15,9 +15,9 @@ def get_images(path):
""" """
file_list = [] file_list = []
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
if is_directory_valid(root, len(files)): if is_directory_valid(root, files):
for fn in files: for fn in files:
if fn.endswith("png") or fn.endswith("jpg") or fn.endswith("bmp"): if is_file_valid_image(fn):
file_list.append(os.path.join(root, fn)) file_list.append(os.path.join(root, fn))
return file_list return file_list
@ -59,7 +59,8 @@ def compress_images(images):
sys.exit("Fatal : Directory '" + os.path.dirname(filename) + "' does not exist and cannot be created") sys.exit("Fatal : Directory '" + os.path.dirname(filename) + "' does not exist and cannot be created")
img.save(filename, "JPEG") img.save(filename, "JPEG")
print("Done. Thumbnails saved in 'photos_thumb' directory") print("done")
print("Thumbnails saved in 'photos_thumb' directory")
def get_new_path(img): def get_new_path(img):
@ -73,18 +74,18 @@ def get_new_path(img):
def zip_dir(path): def zip_dir(path):
""" """
Compress files in the specified directory and sub-directories Compress images in the specified directory and sub-directories
Create one zip per folder Create one zip per folder
:param path: directory to get files in :param path: directory to get files in
:return: :return:
""" """
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
if is_directory_valid(root, len(files)): if is_directory_valid(root, files):
print("Compressing '" + root + "'", end="") print("Compressing '" + root + "'", end="")
with ZipFile(os.path.join(root, get_current_dir(root)) + ".zip", "w", ZIP_DEFLATED) as zip_file: with ZipFile(os.path.join(root, get_current_dir(root)) + ".zip", "w", ZIP_DEFLATED) as zip_file:
for fn in files: for fn in files:
if not fn.endswith("zip"): if not fn.endswith("zip") and is_file_valid_image(fn):
print(".", end="") print(".", end="")
absolute_file_name = os.path.join(root, fn) absolute_file_name = os.path.join(root, fn)
zippped_file_name = absolute_file_name[len(root)+len(os.sep):] # XXX: relative path zippped_file_name = absolute_file_name[len(root)+len(os.sep):] # XXX: relative path
@ -93,22 +94,32 @@ def zip_dir(path):
print("COMPRESSION FINISHED") print("COMPRESSION FINISHED")
def is_directory_valid(path, files_number): def is_directory_valid(path, files):
""" """
Check if the given path is not hidden or empty Check if the given path is not hidden or without images
:param files_number: number of files in the folder :param files: files in the folder
:param path: Path to check :param path: Path to check
:return: True if path contains a hidden folder or is empty, False otherwise :return: True if path contains a hidden folder or is empty, False otherwise
""" """
directories = path.split(os.sep) directories = path.split(os.sep)
valid = files_number > 0 valid = len(files) > 0
for dn in directories: for dn in directories: # check if directory or one of its parent are not hidden
if dn.startswith(".") or not valid: if dn.startswith(".") or not valid:
valid = False valid = False
break break
if valid:
valid_files = []
for fn in files: # check if directory contains valid images
if is_file_valid_image(fn):
valid_files.append(fn)
valid = len(valid_files) > 0
return valid return valid
def is_file_valid_image(file):
return file.endswith("png") or file.endswith("jpg") or file.endswith("jpeg")
def get_current_dir(path): def get_current_dir(path):
""" """
Get the name of the current directory Get the name of the current directory
@ -126,7 +137,7 @@ def get_confirmation(path):
""" """
print("The following folders will be compressed (hidden and empty folders are ignored):") print("The following folders will be compressed (hidden and empty folders are ignored):")
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
if is_directory_valid(root, len(files)): if is_directory_valid(root, files):
print(root) print(root)
confirmation = input("Are you sure you want to proceed? [Y/n]") confirmation = input("Are you sure you want to proceed? [Y/n]")
return confirmation == "Y" or confirmation == "y" return confirmation == "Y" or confirmation == "y"