BookStackApp / BookStack

A platform to create documentation/wiki content built with PHP & Laravel
https://www.bookstackapp.com/
MIT License
15.35k stars 1.92k forks source link

Add automatic compression to uploaded images #2342

Open anutator opened 4 years ago

anutator commented 4 years ago

Describe the feature you'd like Add option to reduce all loaded png images from 24-bit to 256 or even 64 colors. https://about.gitlab.com/blog/2020/01/30/simple-trick-for-smaller-screenshots/ --- article Program to use - pngquant Example: pngquant 64 --skip-if-larger --strip --ext=.png --force *

Add option to convert new .png images during upload, but I don't know if it's possible.

Describe the benefits this feature would bring to BookStack users Screenshots by default are saved as 24-bit png files. With this feature you can reduce BookStack's folder size and open site pages quickly.

tiredofit commented 4 years ago

This would be a great addition to the core software not only for PNG but for JPGs as well.

I am doing this manually via a script inside of my docker image, but would be great to have as the images were uploaded.

tipuraneo commented 1 year ago

Hi out there,

I wrote the following bash script to optimize uploaded images:

# !/bin/bash
# set -x
#
# Recompresses uploaded images of the current month and strips meta tags.
# Images are only processed if there are potential savings. Script can be run
# multiple times without recompressing already compressed images - processed
# images are skipped.Needs pngqunat and jpegoptimum to be installed. Run script
# atleast once a week better daily.
# Logs at /var/log/[script-name].log
# Metadata of png files in drawio are not stripped because they are containing
# vector data which are neccessary to edit these drawings
#
# Version 2023-09-06

FOLDER_BASE=/var/www/bookstack/public/uploads/images

LOG=/var/log/`echo $(basename "$0") | rev | cut -d. -f2- | rev`.log
#FOLDERS="cover_book cover_bookshelf drawio gallery system user"
echo "$(date '+%Y-%m-%d %H:%M:%S') === START ===" >> $LOG
FOLDERS="`ls -q $FOLDER_BASE`"
for FOLDER in $FOLDERS; do
  FOLDER_LONG=$FOLDER_BASE/$FOLDER/`date +"%Y-%m"`/
# FOLDER_LONG=$FOLDER_BASE/$i/2023-07/
  if [ -d "$FOLDER_LONG" ]; then
    DU_BEFORE=`du -s --block-size=1 $FOLDER_LONG | cut -f1`
    DU_BEFORE_K=$(($DU_BEFORE/1024))
    FILES=`ls  -lq $FOLDER_LONG | grep -iE ".*\.(jpg|jpeg|png)$" | wc -l`
    if [ $FILES -gt 0 ]; then
      if [ "$FOLDER" = "drawio" ]; then
        find $FOLDER_LONG -name "*.png" -exec pngquant --force --skip-if-larger --speed=1 --ext .png --ordered --quality=75-90 {} \;
        find $FOLDER_LONG -name "*.PNG" -exec pngquant --force --skip-if-larger --speed=1 --ext .PNG --ordered --quality=75-90 {} \;
      else
        find $FOLDER_LONG -name "*.png" -exec pngquant --strip --force --skip-if-larger --speed=1 --ext .png --ordered --quality=75-90 {} \;
        find $FOLDER_LONG -name "*.PNG" -exec pngquant --strip --force --skip-if-larger --speed=1 --ext .PNG --ordered --quality=75-90 {} \;
      fi
      find $FOLDER_LONG -iname "*.png" -exec chown -f --reference=$FOLDER_LONG {} \;
      find $FOLDER_LONG -iregex ".*\.jpe?g" -exec jpegoptim -q -p --all-progressive -m75 --strip-com --strip-exif --strip-iptc --strip-icc --strip-xmp {} \;
      DU_AFTER=`du -s --block-size=1 $FOLDER_LONG | cut -f1`
      DU_AFTER_K=$((DU_AFTER/1024))
      if [ $DU_AFTER -lt $DU_BEFORE ]; then
        SAVED_K=$((DU_BEFORE_K-$DU_AFTER_K))
        echo "$(date '+%Y-%m-%d %H:%M:%S') $FOLDER_LONG $FILES images, folder shrinked from ${DU_BEFORE_K}k to ${DU_AFTER_K}k, ${SAVED_K}k saved" >> $LOG
      else
        echo "$(date '+%Y-%m-%d %H:%M:%S') $FOLDER_LONG nothhing to do, $FILES images, ${DU_AFTER_K}k" >> $LOG
     fi
    else
      echo "$(date '+%Y-%m-%d %H:%M:%S') $FOLDER_LONG no images found" >> $LOG
    fi
  else
    echo "$(date '+%Y-%m-%d %H:%M:%S') $FOLDER_LONG does not exists" >> $LOG
  fi
done
echo "$(date '+%Y-%m-%d %H:%M:%S') $FOLDER_BASE/ `find $FOLDER_BASE/. -type f | wc -l` files, `du -sh $FOLDER_BASE | cut -f1`" >> $LOG
echo "$(date '+%Y-%m-%d %H:%M:%S') === STOP ===" >> $LOG

I'll maintain the script here: https://kb.el.uni-leipzig.de/books/bookstack/page/optimierung-von-bilddateien