Skip to content

Instantly share code, notes, and snippets.

@goeko
Last active November 26, 2024 22:23
Show Gist options
  • Save goeko/8610f855b98bd8164ba972084a199ff0 to your computer and use it in GitHub Desktop.
Save goeko/8610f855b98bd8164ba972084a199ff0 to your computer and use it in GitHub Desktop.
base64copy from file or -t=text and output text or -o|--output=html and -c|--compress=zip|imagemagic
#!/bin/bash
base64copy() {
local input=""
local output="text"
local mimetype="text/plain"
local base64data
local is_text_input=0
local string_only=0 # Flag für die reine String-Ausgabe
local compression_level=0 # Kompressionslevel, Standard ist 0 (keine Kompression)
while [[ "$#" -gt 0 ]]; do
case $1 in
-o|--output)
case "$2" in
text|html)
output="$2"
shift
;;
*)
echo "Fehler: Ungültiger Ausgabe-Typ. Verwenden Sie 'text' oder 'html'."
return 1
;;
esac
;;
-t|--text)
input="$2"
is_text_input=1
mimetype="text/plain"
shift
;;
-c|--compress)
compression_level="$2"
shift
;;
-s|--string)
string_only=1
;;
-h|--help)
cat << "EOF"
_ _ _ _
___ ___ _ __ _ _ _ __(_) __ _| |__ | |_ | |__ _ _
/ __/ _ \| '_ \| | | | '__| |/ _` | '_ \| __| | '_ \| | | |
| (_| (_) | |_) | |_| | | | | (_| | | | | |_ | |_) | |_| |
\___\___/| .__/ \__, |_| |_|\__, |_| |_|\__| |_.__/ \__, |
|_| |___/ __|___/ |___/
__ _ ___ | ____| | _____
/ _` |/ _ \| _| | |/ / _ \
| (_| | (_) | |___| < (_) |
_____\__, |\___/|_____|_|\_\___/____
|_____|___/ |_____|
base64copy ist ein Tool zur Kodierung von Dateien und Texten in das Base64-Format.
Es wird häufig verwendet, um Bilder oder andere Dateien direkt in HTML- oder CSS-Code einzubetten,
zum Beispiel in Newslettern, Webanwendungen und Single-Page-Anwendungen, wo externe Ressourcen
vermieden werden sollen. Dies hilft, die Anzahl der HTTP-Anfragen zu reduzieren, was die Ladegeschwindigkeit verbessern kann.
Verwendung: base64copy [Optionen] <Datei> oder --text 'string'
Optionen:
-o, --output=text|html Ausgabeformat: 'text' (Standard) oder 'html' für HTML img Tag
-t, --text="string" Direkte Text-Eingabe zum Kodieren
-c, --compress=[0-100] Kompressionslevel für ImageMagick oder ZIP für andere Dateien
-s, --string Kopiert nur den Base64-kodierten String in die Zwischenablage
-h, --help Hilfe anzeigen und beenden
Setup-Hinweise:
- macOS:
* Installieren Sie ImageMagick: 'brew install imagemagick'
* Installieren Sie pbcopy (bereits vorinstalliert)
* Installieren Sie ZIP: 'brew install zip' (falls nicht vorinstalliert)
- Linux (je nach Distribution):
* Ubuntu:
* Installieren Sie ImageMagick: 'sudo apt-get install imagemagick'
* Installieren Sie xclip für das Kopieren in die Zwischenablage: 'sudo apt-get install xclip'
* Installieren Sie ZIP: 'sudo apt-get install zip'
* CentOS/Red Hat:
* Installieren Sie ImageMagick: 'sudo yum install ImageMagick'
* Installieren Sie xclip für das Kopieren in die Zwischenablage: 'sudo yum install xclip'
* Installieren Sie ZIP: 'sudo yum install zip'
* Ersetzen Sie 'pbcopy' im Skript mit 'xclip -selection clipboard' für alle Linux-Distributionen
EOF
echo "copyright© 2024 _goEko_ GrandmasterFläsh"
return 0
;;
*)
if [[ $is_text_input -eq 0 ]]; then
input="$1"
fi
;;
esac
shift
done
if [[ $is_text_input -eq 0 && ! -f "$input" ]]; then
echo "Datei existiert nicht. Verwenden Sie --help für Informationen zur Nutzung."
return 1
fi
if [[ $is_text_input -eq 0 ]]; then
mimetype=$(file --mime-type -b "$input")
if [[ $mimetype =~ ^image/ && $compression_level -ne 0 ]]; then
# ImageMagick Kompression für Bildformate
base64data=$(convert "$input" -quality "$compression_level" - | base64)
elif [[ $compression_level -ne 0 ]]; then
# ZIP-Kompression für nicht Bildformate
tempfile=$(mktemp)
zip -q -j - "$input" > "$tempfile"
base64data=$(base64 < "$tempfile")
rm "$tempfile"
else
base64data=$(base64 < "$input")
fi
else
base64data=$(echo -n "$input" | base64)
fi
if [[ "$string_only" == 1 ]]; then
echo -n "$base64data" | pbcopy
echo "Base64-kodierter String wurde in die Zwischenablage kopiert!"
elif [[ "$output" == "html" ]]; then
echo "<img src='data:$mimetype;base64,$base64data'/>" | pbcopy
echo "Base64-Daten mit MIME-Typ in die Zwischenablage als HTML img Tag kopiert!"
else
echo "data:$mimetype;base64,$base64data" | pbcopy
echo "Base64-Daten mit MIME-Typ in die Zwischenablage als einfacher Text kopiert!"
fi
# Ausgabe der ersten 100 Zeichen des base64 Strings im Terminal
echo "${base64data:0:100}..."
}
# Nur erforderlich, wenn das Skript direkt ausgeführt wird
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
base64copy "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment