#
# FUNKTION: Erstellung bzw. Bearbeitung der Galerie-Bilder bzw. der Thumbnails
#
INC_NAME_PICTURE_EDITING='inc_gallery_picture_editing.txt'

# Variablen überprüfen
PICTURE_DIRECTORY_PATH=${PICTURE_DIRECTORY_PATH:='EMPTY'}
THUMBNAILS_DIRECTORY_PATH=${THUMBNAILS_DIRECTORY_PATH:='EMPTY'}
TEXT_FILE_DIRECTORY=${TEXT_FILE_DIRECTORY:='EMPTY'}
CREATE_GALLERY_USER_INFO_B=${CREATE_GALLERY_USER_INFO_B:='EMPTY'}
PICTURE_TARGET_WIDTH=${PICTURE_TARGET_WIDTH:=800}
PICTURE_TARGET_HEIGHT=${PICTURE_TARGET_HEIGHT:=600}
THUMBNAIL_TARGET_WIDTH=${THUMBNAIL_TARGET_WIDTH:=100}
THUMBNAIL_TARGET_HEIGHT=${THUMBNAIL_TARGET_HEIGHT:=75}
PICTURE_ORIGINAL_WIDTH_MAX=${PICTURE_WIDTH_MAX:=8000}
PICTURE_ORIGINAL_HEIGHT_MAX=${PICTURE_HEIGHT_MAX:=6000}
PICTURE_ORIGINAL_WIDTH_MIN=${PICTURE_WIDTH_MIN:=40}
PICTURE_ORIGINAL_HEIGHT_MIN=${PICTURE_HEIGHT_MIN:=30}
PICTURE_WIDTH_RESIZE_MIN=${PICTURE_WIDTH_RESIZE_MIN:=320}
PICTURE_HEIGHT_RESIZE_MIN=${PICTURE_HEIGHT_RESIZE_MIN:=240}
PICTURE_SHARPEN=${PICTURE_SHARPEN:='0.1x0.7'}
GALLERY_PICTURE_NAME_PART_A=${PICTURE_GALLERY_PICTURE_NAME_PART_A:='pic'}
# GALLERY_PICTURE_NAME_PART_B wird vom Shellskript gesetzt
GALLERY_PICTURE_NAME_PART_JPEG_C=${PICTURE_GALLERY_PICTURE_NAME_PART_JPEG_C:='.jpg'}
GALLERY_PICTURE_NAME_PART_PNG_C=${PICTURE_GALLERY_PICTURE_NAME_PART_PNG_C:='.png'}
GALLERY_THUMBNAIL_NAME_PART_A=${PICTURE_GALLERY_THUMBNAIL_NAME_PART_A:='pic'}
# GALLERY_THUMBNAIL_NAME_PART_B wird vom Shellskript gesetzt
GALLERY_THUMBNAIL_NAME_PART_JPEG_C=${PICTURE_GALLERY_THUMBNAIL_NAME_PART_JPEG_C:='_thumb.jpg'}
GALLERY_THUMBNAIL_NAME_PART_PNG_C=${PICTURE_GALLERY_THUMBNAIL_NAME_PART_PNG_C:='_thumb.png'}
if [ "${PICTURE_DIRECTORY_PATH}" = 'EMPTY' ] || [ "${THUMBNAILS_DIRECTORY_PATH}" = 'EMPTY' ] || [ "${TEXT_FILE_DIRECTORY}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-1 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ "${GALLERY_PICTURE_NAME_PART_A}" = 'EMPTY' ] || [ "${GALLERY_PICTURE_NAME_PART_JPEG_C}" = 'EMPTY' ] || [ "${GALLERY_PICTURE_NAME_PART_PNG_C}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-2 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ "${GALLERY_THUMBNAIL_NAME_PART_A}" = 'EMPTY' ] || [ "${GALLERY_THUMBNAIL_NAME_PART_JPEG_C}" = 'EMPTY' ] || [ "${GALLERY_THUMBNAIL_NAME_PART_PNG_C}" = 'EMPTY' ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-3 \nVARIABLE: NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ $(echo "${PICTURE_TARGET_WIDTH}" | grep -c "^[1-9][0-9]\{2,3\}$") -ne 1 ] || [ $(echo "${PICTURE_TARGET_HEIGHT}" | grep -c "^[1-9][0-9]\{2,3\}$") -ne 1 ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-4 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ ${PICTURE_TARGET_WIDTH} -gt ${PICTURE_ORIGINAL_WIDTH_MAX} ] || [ ${PICTURE_TARGET_HEIGHT} -gt ${PICTURE_ORIGINAL_HEIGHT_MAX} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-5 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ $(echo "${THUMBNAIL_TARGET_WIDTH}" | grep -c "^[1-9][0-9]\{1,2\}$") -ne 1 ] || [ $(echo "${THUMBNAIL_TARGET_HEIGHT}" | grep -c "^[1-9][0-9]\{1,2\}$") -ne 1 ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 1-6 \nFILE: CONFIGURATION-FILE IS DAMAGED \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi

# Verzeichnisse überprüfen
if [ ! -d ${PICTURE_DIRECTORY_PATH} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE_EDITING 2-1 \nDIRECTORY: »${PICTURE_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ -d ${THUMBNAILS_DIRECTORY_PATH} ]
then
# alte Thumnailbilder löschen
rm -Rf ${THUMBNAILS_DIRECTORY_PATH}/*
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 2-2 \nFILE: CAN NOT DELETE OLD THUMBNAIL-PICTURES \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
  fi
else
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 2-3 \nDIRECTORY: »${THUMBNAILS_DIRECTORY_PATH}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi
if [ ! -d ${TEXT_FILE_DIRECTORY} ]
then
echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 2-4 \nDIRECTORY: »${TEXT_FILE_DIRECTORY}« NOT FOUND \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
exit 1
fi

# Bilder loeschen, die zu klein oder zu groß sind
for p in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$")
do
  PICTURE_WIDTH_CHECK=$(identify -format %w ${p})
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 3-1 \nFILE: CAN NOT TO DETECT PICTURE-SIZE »${p}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  PICTURE_HEIGHT_CHECK=$(identify -format %h ${p})
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 3-2 \nFILE: CAN NOT TO DETECT PICTURE-SIZE »${p}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  if [ ${PICTURE_WIDTH_CHECK} -gt ${PICTURE_ORIGINAL_WIDTH_MAX} ] || [ ${PICTURE_HEIGHT_CHECK} -gt ${PICTURE_ORIGINAL_HEIGHT_MAX} ]
  then
  rm -f ${p}
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 3-3 \nFILE: NOT SUPPORTED PICTURE-SIZE - CAN NOT DELETE FILE »${p}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  continue
  fi
  if [ ${PICTURE_WIDTH_CHECK} -lt ${PICTURE_ORIGINAL_WIDTH_MIN} ] || [ ${PICTURE_HEIGHT_CHECK} -lt ${PICTURE_ORIGINAL_HEIGHT_MIN} ]
  then
  rm -f ${p}
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 3-4 \nFILE: NOT SUPPORTED PICTURE-SIZE - CAN NOT DELETE FILE »${p}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  continue
  fi
done

# Runde 1: Dateinamen der Bilder ändern - temporäre Bildnamen
# UNIX-Zeitstempel
TMP_PICTURE_NAME_PART_A=$(date +%s)
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 4-1 \nFILE: CAN NOT CREATE UNIX-TIMESTAMP \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
  fi
START=${START:+10000}
STEP=${STEP:+1}
N=${N:+$START}
for f in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | sort)
do
  # Bildernamen zusamenstellen
  if [ $(echo "${f}" | grep -ci "\(\.jpg\|\.jpeg\)$") -gt 0 ]
  then
    TMP_PICTURE_NAME="${TMP_PICTURE_NAME_PART_A}${N}${GALLERY_PICTURE_NAME_PART_JPEG_C}"
  else
    TMP_PICTURE_NAME="${TMP_PICTURE_NAME_PART_A}${N}${GALLERY_PICTURE_NAME_PART_PNG_C}"
  fi
  # Bilder temporär umbenennen
  mv -f "${f}" "${PICTURE_DIRECTORY_PATH}/${TMP_PICTURE_NAME}"
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 4-2 \nFILE: CAN NOT RENAME FILE »${f}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  N=$[$N+$STEP]
done

# Runde 2: Dateinamen der Bilder ändern (pic00001.jpg, pic00001.png, ...)
START=${START:+1}
STEP=${STEP:+1}
Q=${Q:+$START}
for g in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | sort)
do
  if [ $Q -gt 0 ] && [ $Q -lt 10 ]
  then
    GALLERY_PICTURE_NAME_PART_B="0000${Q}"
  elif [ $Q -lt 100 ]
  then
    GALLERY_PICTURE_NAME_PART_B=000${Q}
  elif [ $Q -lt 1000 ]
  then
    GALLERY_PICTURE_NAME_PART_B=00${Q}
  elif [ $Q -lt 10000 ]
  then
    GALLERY_PICTURE_NAME_PART_B=0${Q}
  elif [ $Q -lt 100000 ]
  then
    GALLERY_PICTURE_NAME_PART_B=${Q}
  else
    echo -e "\n  ${CREATE_GALLERY_USER_INFO_B}\n"
    exit 1
  fi
  # Bildernamen zusamenstellen
  if [ $(echo "${g}" | grep -ci "\(\.jpg\|\.jpeg\)$") -gt 0 ]
  then
    GALLERY_PICTURE_NAME="${GALLERY_PICTURE_NAME_PART_A}${GALLERY_PICTURE_NAME_PART_B}${GALLERY_PICTURE_NAME_PART_JPEG_C}"
  else
    GALLERY_PICTURE_NAME="${GALLERY_PICTURE_NAME_PART_A}${GALLERY_PICTURE_NAME_PART_B}${GALLERY_PICTURE_NAME_PART_PNG_C}"
  fi
  # Bilder umbenennen
  mv -f "${g}" "${PICTURE_DIRECTORY_PATH}/${GALLERY_PICTURE_NAME}"
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 4-3 \nFILE: CAN NOT RENAME FILE »${g}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  Q=$[$Q+$STEP]
done

# aktuelle Bildgröße an die Zielgröße (siehe: ./config/config_xx.txt) anpassen
# Bildbreite / Bildhoehe * 10 (* 10 -> IF-Abfrage benötigt GANZE Zahlen)
# Vorzugsgröße (Seitenverhältnis: 4 : 3) 640 x 480, 800 x 600 etc.; PICTURE_QUOTIENT = 13.33
# die Zahlen 15 und 12 entsprechen in etwa einer Abweichung von 10 Prozent vom Seitenverhältnis 4 : 3 -> STANDARD-Bearbeitung
PICTURE_STANDARD_QUOTIENT_A_MAX=15
PICTURE_STANDARD_QUOTIENT_A_MIN=12
# Vorzugsgröße (Seitenverhältnis: 16 : 9) 800 x 450 1200 x 675 etc.; PICTURE_QUOTIENT = 17.77
# die Zahlen 20 und 16 entsprechen in etwa einer Abweichung von 10 Prozent vom Seitenverhältnis 16 : 9 -> STANDARD-Bearbeitung
PICTURE_STANDARD_QUOTIENT_B_MAX=20
PICTURE_STANDARD_QUOTIENT_B_MIN=16
# bei einer starken Abweichung vom Seitenverhältnis 4 : 3 bzw. 16 : 9 (Breite x Höhe) -> FREESIZE-Bearbeitung

# aktuellen Standard-Quotitienten ermitteln
PICTURE_CURRENT_QUOTIENT_A=$(echo "scale=2; (${PICTURE_TARGET_WIDTH}/${PICTURE_TARGET_HEIGHT}*10)" | bc)
PICTURE_CURRENT_QUOTIENT_B=${PICTURE_CURRENT_QUOTIENT_A%%.*}
if [ ${PICTURE_CURRENT_QUOTIENT_B} -lt ${PICTURE_STANDARD_QUOTIENT_B_MIN} ]
then
PICTURE_STANDARD_QUOTIENT_MAX=${PICTURE_STANDARD_QUOTIENT_A_MAX}
PICTURE_STANDARD_QUOTIENT_MIN=${PICTURE_STANDARD_QUOTIENT_A_MIN}
else
PICTURE_STANDARD_QUOTIENT_MAX=${PICTURE_STANDARD_QUOTIENT_B_MAX}
PICTURE_STANDARD_QUOTIENT_MIN=${PICTURE_STANDARD_QUOTIENT_B_MIN}
fi
for h in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | sort)
do
  if [ -f ${h} ] && [ $(echo ${h} | grep -c "\(\.jpg\|\.jpeg\|\.png\)$") -gt 0 ]
  then
    PICTURE_WIDTH_A=$(identify -format %w ${h})
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-1 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
      exit 1
      fi
    PICTURE_HEIGHT_A=$(identify -format %h ${h})
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-2 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
      exit 1
      fi
    PICTURE_QUOTIENT_A=$(echo "scale=2; (${PICTURE_WIDTH_A}/${PICTURE_HEIGHT_A}*10)" | bc)
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-3 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
      exit 1
      fi
    PICTURE_QUOTIENT_B=${PICTURE_QUOTIENT_A%%.*}
    if [ ${PICTURE_WIDTH_A} -lt ${PICTURE_WIDTH_RESIZE_MIN} ] && [ ${PICTURE_HEIGHT_A} -lt ${PICTURE_HEIGHT_RESIZE_MIN} ]
    then
    PICTURE_QUOTIENT_B=${PICTURE_QUOTIENT_B:+0}
    # Bilder die ein Seitenverhältnis von 4:3 bzw. 16:9 haben, aber zu klein sind,
    # werden nicht mehr vergrößert -> FREESIZE-Bildbearbeitung
    fi
    if [ ${PICTURE_QUOTIENT_B} -ge ${PICTURE_STANDARD_QUOTIENT_MIN} ] && [ ${PICTURE_QUOTIENT_B} -le ${PICTURE_STANDARD_QUOTIENT_MAX} ]
    then
    # STANDARD-Bildbearbeitung
      if [ ${PICTURE_WIDTH_A} -ne ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_A} -ne ${PICTURE_TARGET_HEIGHT} ]
      then
	WIDTH_QUOTIENT_A=$(echo "scale=0; (${PICTURE_TARGET_WIDTH}*100/${PICTURE_WIDTH_A})+1" | bc)
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-4 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	HEIGHT_QUOTIENT_A=$(echo "scale=0; (${PICTURE_TARGET_HEIGHT}*100/${PICTURE_HEIGHT_A})+1" | bc)
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-5 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	if [ ${WIDTH_QUOTIENT_A} -ge ${HEIGHT_QUOTIENT_A} ]
	then
	  convert ${h} -resize ${WIDTH_QUOTIENT_A}% -quality 90 ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-6 \nFILE: CAN NOT RESIZE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	else
	  convert ${h} -resize ${HEIGHT_QUOTIENT_A}% -quality 90 ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-7 \nFILE: CAN NOT RESIZE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	fi
	PICTURE_WIDTH_B=$(identify -format %w ${h})
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-8 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	PICTURE_HEIGHT_B=$(identify -format %h ${h})
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-9 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	if [ ${PICTURE_WIDTH_B} -ge ${PICTURE_TARGET_WIDTH} ] && [ ${PICTURE_HEIGHT_B} -ge ${PICTURE_TARGET_HEIGHT} ]
	then
	  if [ ${PICTURE_WIDTH_B} -ne ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_B} -ne ${PICTURE_TARGET_HEIGHT} ]
	  then
	    CROP_POSITION_X=$(echo "scale=0; (${PICTURE_WIDTH_B}-${PICTURE_TARGET_WIDTH})/2" | bc)
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-10 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    CROP_POSITION_Y=$(echo "scale=0; (${PICTURE_HEIGHT_B}-${PICTURE_TARGET_HEIGHT})/2" | bc)
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-11 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    CROP_POSITION_X_CHECK=$[$CROP_POSITION_X*2+${PICTURE_TARGET_WIDTH}]
	      if [ ${CROP_POSITION_X_CHECK} -gt ${PICTURE_WIDTH_B} ]
	      then
	      CROP_POSITION_X=$[$CROP_POSITION_X-1]
	      fi
	    CROP_POSITION_Y_CHECK=$[$CROP_POSITION_Y*2+${PICTURE_TARGET_HEIGHT}]
	      if [ ${CROP_POSITION_Y_CHECK} -gt ${PICTURE_HEIGHT_B} ]
	      then
	      CROP_POSITION_Y=$[$CROP_POSITION_Y-1]
	      fi
	    convert ${h} -crop ${PICTURE_TARGET_WIDTH}x${PICTURE_TARGET_HEIGHT}+${CROP_POSITION_X}+${CROP_POSITION_Y} -quality 90 ${h}
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-12 \nFILE: CAN NOT RESIZE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	  fi
	else
	  rm -f ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-13 \nFILE: CAN NOT DELETE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  continue
	fi
	convert ${h} -sharpen ${PICTURE_SHARPEN} ${h}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 5-14 \nFILE: CAN NOT SHARPEN PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
      fi
    else
    # FREESIZE-Bildbearbeitung
      if [ ${PICTURE_WIDTH_A} -gt ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_A} -gt ${PICTURE_TARGET_HEIGHT} ]
      then
	WIDTH_QUOTIENT_A=$(echo "scale=2; (${PICTURE_TARGET_WIDTH}/${PICTURE_WIDTH_A}*100)" | bc)
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-1 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	WIDTH_QUOTIENT_B=${WIDTH_QUOTIENT_A%%.*}
	HEIGHT_QUOTIENT_A=$(echo "scale=2; (${PICTURE_TARGET_HEIGHT}/${PICTURE_HEIGHT_A}*100)" | bc)
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-2 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	HEIGHT_QUOTIENT_B=${HEIGHT_QUOTIENT_A%%.*}
	if [ ${WIDTH_QUOTIENT_B} -le ${HEIGHT_QUOTIENT_B} ]
	then
	  convert ${h} -resize ${WIDTH_QUOTIENT_B}% -quality 90 ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-3 \nFILE: CAN NOT RESIZE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	else
	  convert ${h} -resize ${HEIGHT_QUOTIENT_B}% -quality 90 ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-4 \nFILE: CAN NOT RESIZE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	fi
	convert ${h} -sharpen ${PICTURE_SHARPEN} ${h}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-5 \nFILE: CAN NOT SHARPEN PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	PICTURE_WIDTH_B=$(identify -format %w ${h})
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-6 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	PICTURE_HEIGHT_B=$(identify -format %h ${h})
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-7 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	if [ ${PICTURE_WIDTH_B} -ge ${PICTURE_TARGET_WIDTH} ] && [ ${PICTURE_HEIGHT_B} -ge ${PICTURE_TARGET_HEIGHT} ]
	then
	  if [ ${PICTURE_WIDTH_B} -ne ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_B} -ne ${PICTURE_TARGET_HEIGHT} ]
	  then
	    CROP_POSITION_X=$(echo "scale=0; (${PICTURE_WIDTH_B}-${PICTURE_TARGET_WIDTH})/2" | bc)
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-8 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    CROP_POSITION_Y=$(echo "scale=0; (${PICTURE_HEIGHT_B}-${PICTURE_TARGET_HEIGHT})/2" | bc)
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-9 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    CROP_POSITION_X_CHECK=$[$CROP_POSITION_X+${PICTURE_TARGET_WIDTH}]
	    if [ ${CROP_POSITION_X_CHECK} -gt ${PICTURE_WIDTH_B} ]
	    then
	    CROP_POSITION_X=$[$CROP_POSITION_X-1]
	    fi
	    CROP_POSITION_Y_CHECK=$[$CROP_POSITION_Y+${PICTURE_TARGET_HEIGHT}]
	    if [ ${CROP_POSITION_Y_CHECK} -gt ${PICTURE_HEIGHT_B} ]
	    then
	    CROP_POSITION_Y=$[$CROP_POSITION_Y-1]
	    fi
	    convert ${h} -crop ${PICTURE_TARGET_WIDTH}x${PICTURE_TARGET_HEIGHT}+${CROP_POSITION_X}+${CROP_POSITION_Y} -quality 90 ${h}
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-10 \nFILE: CAN NOT CROP PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    PICTURE_WIDTH_B=${PICTURE_WIDTH_B:+$(identify -format %w ${h})}
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-11 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	    PICTURE_WIDTH_B=${PICTURE_WIDTH_B:+$(identify -format %h ${h})}
	      if [ $? -ge 1 ]
	      then
	      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-12 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	      exit 1
	      fi
	  fi
	fi
	if [ ${PICTURE_WIDTH_B} -lt ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_B} -lt ${PICTURE_TARGET_HEIGHT} ]
	then
	  if [ ${PICTURE_WIDTH_B} -lt ${PICTURE_TARGET_WIDTH} ]
	  then
	    FRAME_WIDTH_MODULO=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_B})%2]
	      if [ ${FRAME_WIDTH_MODULO} -ne 0 ]
	      then
	      FRAME_WIDTH=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_B}+1)/2]
	      else
	      FRAME_WIDTH=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_B})/2]
	      fi
	  else
	  FRAME_WIDTH=1
	  fi
	  if [ ${PICTURE_HEIGHT_B} -lt ${PICTURE_TARGET_HEIGHT} ]
	  then
	    FRAME_HEIGHT_MODULO=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_B})%2]
	      if [ ${FRAME_HEIGHT_MODULO} -ne 0 ]
	      then
	      FRAME_HEIGHT=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_B}+1)/2]
	      else
	      FRAME_HEIGHT=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_B})/2]
	      fi
	  else
	  FRAME_HEIGHT=1
	  fi
	  convert ${h} -bordercolor white -border ${FRAME_WIDTH}x${FRAME_HEIGHT} ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-13 \nFILE: CAN NOT CREATE PICTURE (WHITE-BORDER) »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  PICTURE_WIDTH_C=$(identify -format %w ${h})
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-14 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  PICTURE_HEIGHT_C=$(identify -format %h ${h})
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-15 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  if [ ${PICTURE_WIDTH_C} -ge ${PICTURE_TARGET_WIDTH} ] && [ ${PICTURE_HEIGHT_C} -ge ${PICTURE_TARGET_HEIGHT} ]
	  then
	    if [ ${PICTURE_WIDTH_C} -ne ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_C} -ne ${PICTURE_TARGET_HEIGHT} ]
	    then
	      CROP_POSITION_X=$(echo "scale=0; (${PICTURE_WIDTH_C}-${PICTURE_TARGET_WIDTH})/2" | bc)
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-16 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	      CROP_POSITION_Y=$(echo "scale=0; (${PICTURE_HEIGHT_C}-${PICTURE_TARGET_HEIGHT})/2" | bc)
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-17 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	      CROP_POSITION_X_CHECK=$[$CROP_POSITION_X*2+${PICTURE_TARGET_WIDTH}]
		if [ ${CROP_POSITION_X_CHECK} -gt ${PICTURE_WIDTH_C} ]
		then
		CROP_POSITION_X=$[$CROP_POSITION_X-1]
		fi
	      CROP_POSITION_Y_CHECK=$[$CROP_POSITION_Y*2+${PICTURE_TARGET_HEIGHT}]
		if [ ${CROP_POSITION_Y_CHECK} -gt ${PICTURE_HEIGHT_C} ]
		then
		CROP_POSITION_Y=$[$CROP_POSITION_Y-1]
		fi
	      convert ${h} -crop ${PICTURE_TARGET_WIDTH}x${PICTURE_TARGET_HEIGHT}+${CROP_POSITION_X}+${CROP_POSITION_Y} -quality 90 ${h}
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-18 \nFILE: CAN NOT CROP PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	    fi
	  fi
	else
	  rm -f ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-19 \nFILE: CAN NOT DELETE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  continue
	fi
      elif [ ${PICTURE_WIDTH_A} -le ${PICTURE_TARGET_WIDTH} ] && [ ${PICTURE_HEIGHT_A} -le ${PICTURE_TARGET_HEIGHT} ]
      then
	if [ ${PICTURE_WIDTH_A} -lt ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_A} -lt ${PICTURE_TARGET_HEIGHT} ]
	then
	  if [ ${PICTURE_WIDTH_A} -lt ${PICTURE_TARGET_WIDTH} ]
	  then
	    FRAME_WIDTH_MODULO=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_A})%2]
	    if [ ${FRAME_WIDTH_MODULO} -ne 0 ]
	    then
	    FRAME_WIDTH=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_A}+1)/2]
	    else
	    FRAME_WIDTH=$[(${PICTURE_TARGET_WIDTH}-${PICTURE_WIDTH_A})/2]
	    fi
	  else
	  FRAME_WIDTH=1
	  fi
	  if [ ${PICTURE_HEIGHT_A} -lt ${PICTURE_TARGET_HEIGHT} ]
	  then
	    FRAME_HEIGHT_MODULO=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_A})%2]
	    if [ ${FRAME_HEIGHT_MODULO} -ne 0 ]
	    then
	    FRAME_HEIGHT=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_A}+1)/2]
	    else
	    FRAME_HEIGHT=$[(${PICTURE_TARGET_HEIGHT}-${PICTURE_HEIGHT_A})/2]
	    fi
	  else
	  FRAME_HEIGHT=1
	  fi
	  convert ${h} -bordercolor white -border ${FRAME_WIDTH}x${FRAME_HEIGHT} ${h}
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-20 \nFILE: CAN NOT CREATE PICTURE (WHITE-BORDER) »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  PICTURE_WIDTH_B=$(identify -format %w ${h})
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-21 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  PICTURE_HEIGHT_B=$(identify -format %h ${h})
	    if [ $? -ge 1 ]
	    then
	    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-22 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	    exit 1
	    fi
	  if [ ${PICTURE_WIDTH_B} -ge ${PICTURE_TARGET_WIDTH} ] && [ ${PICTURE_HEIGHT_B} -ge ${PICTURE_TARGET_HEIGHT} ]
	  then
	    if [ ${PICTURE_WIDTH_B} -ne ${PICTURE_TARGET_WIDTH} ] || [ ${PICTURE_HEIGHT_B} -ne ${PICTURE_TARGET_HEIGHT} ]
	    then
	      CROP_POSITION_X=$(echo "scale=0; (${PICTURE_WIDTH_B}-${PICTURE_TARGET_WIDTH})/2" | bc)
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-23 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	      CROP_POSITION_Y=$(echo "scale=0; (${PICTURE_HEIGHT_B}-${PICTURE_TARGET_HEIGHT})/2" | bc)
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-24 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	      CROP_POSITION_X_CHECK=$[$CROP_POSITION_X*2+${PICTURE_TARGET_WIDTH}]
		if [ ${CROP_POSITION_X_CHECK} -gt ${PICTURE_WIDTH_B} ]
		then
		CROP_POSITION_X=$[$CROP_POSITION_X-1]
		fi
	      CROP_POSITION_Y_CHECK=$[$CROP_POSITION_Y*2+${PICTURE_TARGET_HEIGHT}]
		if [ ${CROP_POSITION_Y_CHECK} -gt ${PICTURE_HEIGHT_B} ]
		then
		CROP_POSITION_Y=$[$CROP_POSITION_Y-1]
		fi
	      convert ${h} -crop ${PICTURE_TARGET_WIDTH}x${PICTURE_TARGET_HEIGHT}+${CROP_POSITION_X}+${CROP_POSITION_Y} -quality 90 ${h}
		if [ $? -ge 1 ]
		then
		echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-25 \nFILE: CAN NOT CROP PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
		exit 1
		fi
	    fi
	  fi
	fi
      else
	rm -f ${h}
	  if [ $? -ge 1 ]
	  then
	  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-26 \nFILE: CAN NOT DELETE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
	  exit 1
	  fi
	continue
      fi
    fi
  else
    rm -f ${h}
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 6-27 \nFILE: CAN NOT DELETE PICTURE »${h}« \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
      exit 1
      fi
    continue
  fi
done

# Erstellung der Thumbnails
PICTURE_NUMBERS=$(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | wc -l 2> /dev/null)
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-1 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
  fi
THUMBNAIL_NUMBERS_A=$(find ${THUMBNAILS_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\)$" | wc -l 2> /dev/null)
  if [ $? -ge 1 ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-2 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
  fi
if [ ${PICTURE_NUMBERS} -gt 0 ]
then
  if [ ${THUMBNAIL_NUMBERS_A} -gt 0 ]
  then
    rm -f ${THUMBNAILS_DIRECTORY_PATH}/*
      if [ $? -ge 1 ]
      then
      echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-3 \nFILE: CAN NOT DELETE OLD THUMBNAIL-PICTURES \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
      exit 1
      fi
  fi
  for i in $(find ${PICTURE_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\|\.png\)$" | sort)
  do
  PICTURE_NAME_CURRENT=${i##*/}
  convert $i -geometry ${THUMBNAIL_TARGET_WIDTH}x${THUMBNAIL_TARGET_HEIGHT} -quality 90 ${THUMBNAILS_DIRECTORY_PATH}/${PICTURE_NAME_CURRENT%.*}_thumb.jpg
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-4 \nFILE: CAN NOT CREATE THUMBNAIL-PICTURES \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  done
  THUMBNAIL_NUMBERS_B=$(find ${THUMBNAILS_DIRECTORY_PATH} -maxdepth 1 -type f -iregex "^.*\(\.jpg\|\.jpeg\)$" | wc -l 2> /dev/null)
    if [ $? -ge 1 ]
    then
    echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-5 \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
    exit 1
    fi
  if [ ${PICTURE_NUMBERS} -ne ${THUMBNAIL_NUMBERS_B} ]
  then
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-6 \nFILE: FILE-COUNT IN PICTURE- AND THUMBNAIL-DIRECTORY UNEQUAL \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
  fi
else
  echo -e "\nERROR-CODE: CREATE-GALLERY PICTURE-EDITING 7-7 \nFILE: CAN NOT FOUND PICTURE IN PICTURE-DIRECTORY \nSCRIPT-NAME: ${INC_NAME_PICTURE_EDITING}\n"
  exit 1
fi
