org-msg
                                
                                 org-msg copied to clipboard
                                
                                    org-msg copied to clipboard
                            
                            
                            
                        Automatic resize inline images when sending emails
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
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
Hi Jeremy, Thank you for the answer. I will try it.