jeremy-compostella / org-msg

OrgMsg is a GNU/Emacs global minor mode mixing up Org mode and Message mode to compose and reply to emails in a Outlook HTML friendly style.
GNU General Public License v3.0
275 stars 58 forks source link

Automatic resize inline images when sending emails #55

Open pawsen opened 4 years ago

pawsen commented 4 years ago

Dear all,

I would like to automatic resize all inline- and attached images in messages I send. The listed code does this for attached images, but not for inline images (ie [[file:~/some_inline_img.jpg]]).

mu4e-resize-image-attachments is run by message-send-hook, looking for something like

<#part type="image/jpeg" filename="/home/user/some_attached_img.jpg" disposition=attachment>

and then replacing the filename with a resized version of the image created in /tmp.

What do I have to look for, for inline messages? org-msg also adds to the message-send-hook. How do I know in which order the different hooks are run in, ie. should I search for [[file:~/some_inline_img.jpg]], resize and replace with [[file:/tmp/some_inline_img.jpg]]?

(defvar mu4e-resize-image-types '("jpg" "png" "svg" "jpeg")
  "List of attached image types to resize.")

(defvar mu4e-resize-img t "if t, then automatic resize images")

(defun mu4e-resize-image-attachments ()
  (when mu4e-resize-img ; unless is the 'no-then' form of when
    (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 600 %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-toggle-resize-img()
  "Toggle automatic resizing of images to size 600px on largest
side, while keeping aspect ratio"
  (interactive)
  (set (make-local-variable 'mu4e-resize-img) (not (symbol-value 'mu4e-resize-img)))
  (message "mu4e-resize-img toggled (= %s)" (symbol-value 'mu4e-resize-img)) )

Best regards, Paw

jeremy-compostella commented 3 years ago

Hi @pawsen,

Sorry for the delay.

I would recommend that you advise the org-msg-org-to-xml on its results, walk through the XML tree, locate the img tags and do the transformation at this point.

Jeremy

pawsen commented 3 years ago

Hi Jeremy, Thank you for the answer. I will try it.