From c6a10d47b26de75f85506bde7b60f995a2002c5a Mon Sep 17 00:00:00 2001 From: Frederik Vanrenterghem Date: Mon, 6 May 2024 23:00:50 +0800 Subject: [PATCH] Automatically downscale large image attachments in mu4e. Resize attached images to not exceed 1200x1200. --- emacs/.emacs.d/init.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/emacs/.emacs.d/init.el b/emacs/.emacs.d/init.el index 51f86f1..69a744a 100644 --- a/emacs/.emacs.d/init.el +++ b/emacs/.emacs.d/init.el @@ -147,6 +147,40 @@ (or (string-join (seq-take (split-string (mu4e-fetch-field msg "X-Spam-Status") " ") 2) " ") ""))))) (add-to-list 'mu4e-view-fields :spam-status) b + +;; Resize image attachments when sending email +(defvar mu4e-resize-image-types '("jpg" "png" "svg" "jpeg") + "List of attached image types to resize.") + +(defvar mu4e-inhibit-resize nil) + +(defun mu4e-resize-image-attachments () + (unless mu4e-inhibit-resize + (let (cmds + (image-types + (mapconcat #'identity mu4e-resize-image-types "\\|"))) + (save-excursion + (message-goto-body-1) + (while (re-search-forward + (format "<#part.+\\(filename=\"\\)\\(.+\\(\\.%s\\)\\)\"" + image-types) + nil t) + (let* ((infile (match-string-no-properties 2)) + (outfile (concat (temporary-file-directory) + (file-name-nondirectory infile)))) + (push (format "convert %s -resize 1200x1200\\> %s" + (shell-quote-argument infile) + (shell-quote-argument outfile)) + cmds) + (replace-match outfile t t nil 2))) + (mapcar #'shell-command cmds))))) + +(add-hook 'message-send-hook 'mu4e-resize-image-attachments) + +(defun mu4e-inhibit-resize() + (interactive) + (set (make-local-variable 'mu4e-inhibit-resize) t)) + ;; Load configuration for website ;(load "mustache-html") -- 2.39.5