Emacs: HTML Image Path to Img Tag

By Xah Lee. Date: . Last updated: .

Emacs lisp command that changes a image path into a HTML image link.


You have this under cursor:


you want it to be this:

<img src="cat.jpg" alt="cat" width="600" height="400" />


Get Image Dimensions

First, here's the code for getting the image width and height.

(defun xah-get-image-dimensions (Filepath)
  "Returns a vector [width height] of a image's dimension.
The elements are integer datatype.
Support png jpg svg gif and any image type emacs supports.
if not supported image type, calls `xah-get-image-dimensions-imk'.

URL `http://xahlee.info/emacs/emacs/elisp_image_tag.html'
Version: 2017-01-11 2021-09-01 2023-09-13"
  (if (image-type-available-p (intern-soft (file-name-extension Filepath)))
      (let (ximg)
        (clear-image-cache Filepath)
        (setq ximg (image-size (create-image (file-truename Filepath)) t))
        (vector (car ximg) (cdr ximg)))
    (xah-get-image-dimensions-imk Filepath)))

(defvar xah-imagemagick-path
  (if (eq system-type 'windows-nt) "magick.exe" "magick")
  "Full file path or program name of ImageMagick program.
On mac/linux, this is usually β€œmagick”, on Windows it's usually β€œmagick.exe”.")

(defun xah-get-image-dimensions-imk (Filepath)
  "Returns a image file's width and height as a vector.
Each element is a integer.
This function requires the shell command: magick identify.
See also: `xah-get-image-dimensions'.

URL `http://xahlee.info/emacs/emacs/elisp_image_tag.html'
Version: 2015-05-12 2021-09-01 2023-01-26 2023-09-12"
  (let ((xwidthHeight
           (concat xah-imagemagick-path " identify -format \"%w %h\" " Filepath)))))
     (string-to-number (elt xwidthHeight 0))
     (string-to-number (elt xwidthHeight 1)))))

Image Path to HTML Image Link

latest code at Emacs: Xah HTML Mode πŸ“¦

(defun xah-html-image-linkify ()
  "Replace image file path under cursor to HTML img inline link.

 <img src=\"img/my_cats.jpg\" alt=\"my cats\" width=\"470\" height=\"456\" />

If `univers-argument' is called before, don't width and height attribute.

Returns the string used in the alt attribute.

URL `http://xahlee.info/emacs/emacs/elisp_image_tag.html'
Version 2018-06-14 2021-01-12"
  (let ( xp1 xp2 ximgPath xhrefValue xaltText ximgWH xwidth xheight)
      ;; get image file path begin end pos
      (let (xp0)
        (setq xp0 (point))
        ;; chars that are likely to be delimiters of full path, e.g. space, tabs, brackets.
        (skip-chars-backward "^ Β \"\t\n'|()[]{}<>γ€”γ€•β€œβ€γ€ˆγ€‰γ€Šγ€‹γ€γ€‘γ€–γ€—γ€˜γ€™Β«Β»β€Ήβ€ΊΒ·γ€‚\\`")
        (setq xp1 (point))
        (goto-char xp0)
        (skip-chars-forward "^ Β \"\t\n'|()[]{}<>γ€”γ€•β€œβ€γ€ˆγ€‰γ€Šγ€‹γ€γ€‘γ€–γ€—γ€˜γ€™Β«Β»β€Ήβ€ΊΒ·γ€‚\\'")
        (setq xp2 (point))
        (goto-char xp0)))
    (setq ximgPath
          (if (fboundp 'xahsite-web-path-to-filepath)
                (buffer-substring-no-properties xp1 xp2 )))
            (buffer-substring-no-properties xp1 xp2 )))
    (when (not (file-exists-p ximgPath))
      (user-error "file not exist at %s"  ximgPath))
    (setq xhrefValue
           (file-name-directory (or (buffer-file-name) default-directory))))
    (setq xaltText
             "_" " "
              "\\.[A-Za-z]\\{3,4\\}$" "" (file-name-nondirectory ximgPath) t t) t t))
    (if current-prefix-arg
          (delete-region xp1 xp2)
            "<img src=\""
            "\"" " " "alt=\"" xaltText "\"" " />")))
        (setq ximgWH (xah-get-image-dimensions ximgPath))
        (setq xwidth (number-to-string (elt ximgWH 0)))
        (setq xheight (number-to-string (elt ximgWH 1)))

        (delete-region xp1 xp2)
         (if (or (equal xwidth "0") (equal xheight "0"))
              "<img src=\""
              "\"" " " "alt=\"" xaltText "\"" " />")
            "<img src=\""
            "\"" " " "alt=\"" xaltText "\""
            " width=\"" xwidth "\""
            " height=\"" xheight "\" />")))))