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 it's svg, and dimension cannot be determined, it returns [0 0].
If it's webp, calls `xah-get-image-dimensions-imk'.

URL `http://xahlee.info/emacs/emacs/elisp_image_tag.html'
Version 2017-01-11 2021-09-01"
  (let ($x $y)
     ((string-match "\.svg$" Filepath)
          ;; hackish. grab the first occurence of width height in file
          (insert-file-contents Filepath)
          (goto-char (point-min))
          (when (re-search-forward "width=\"\\([0-9]+\\).*\"" nil 1)
            (setq $x (match-string-no-properties 1 )))
          (goto-char (point-min))
          (if (re-search-forward "height=\"\\([0-9]+\\).*\"" nil 1)
              (setq $y (match-string-no-properties 1 ))))
        (if (and $x $y)
            (vector (string-to-number $x) (string-to-number $y))
          [0 0])))
     ((string-match "\.webp$" Filepath)
      (xah-get-image-dimensions-imk Filepath))
      (let ($xy )
          (clear-image-cache t)
          (setq $xy (image-size
                      (if (file-name-absolute-p Filepath)
                        (concat default-directory Filepath)))
        (vector (car $xy) (cdr $xy)))))))

If emacs isn't compiled with image support, then we can make a shell call to ImageMagick.

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

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

This function makes a shell call to the ImageMagick's identify command. [see Linux: ImageMagick Command Line Tutorial]

Image Path to HTML Image Link

Here's the main code.

(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 ( $p1 $p2 $imgPath $hrefValue $altText $imgWH $width $height)
      ;; get image file path begin end pos
      (let ($p0)
        (setq $p0 (point))
        ;; chars that are likely to be delimiters of full path, e.g. space, tabs, brakets.
        (skip-chars-backward "^  \"\t\n'|()[]{}<>〔〕“”〈〉《》【】〖〗〘〙«»‹›·。\\`")
        (setq $p1 (point))
        (goto-char $p0)
        (skip-chars-forward "^  \"\t\n'|()[]{}<>〔〕“”〈〉《》【】〖〗〘〙«»‹›·。\\'")
        (setq $p2 (point))
        (goto-char $p0)))
    (setq $imgPath
          (if (fboundp 'xahsite-web-path-to-filepath)
                (buffer-substring-no-properties $p1 $p2 )))
            (buffer-substring-no-properties $p1 $p2 )))
    (when (not (file-exists-p $imgPath))
      (user-error "file not exist at %s"  $imgPath))
    (setq $hrefValue
           (file-name-directory (or (buffer-file-name) default-directory))))
    (setq $altText
             "_" " "
              "\\.[A-Za-z]\\{3,4\\}$" "" (file-name-nondirectory $imgPath) t t) t t))
    (if current-prefix-arg
          (delete-region $p1 $p2)
            "<img src=\""
            "\"" " " "alt=\"" $altText "\"" " />")))
        (setq $imgWH (xah-get-image-dimensions $imgPath))
        (setq $width (number-to-string (elt $imgWH 0)))
        (setq $height (number-to-string (elt $imgWH 1)))

        (delete-region $p1 $p2)
         (if (or (equal $width "0") (equal $height "0"))
              "<img src=\""
              "\"" " " "alt=\"" $altText "\"" " />")
            "<img src=\""
            "\"" " " "alt=\"" $altText "\""
            " width=\"" $width "\""
            " height=\"" $height "\" />")))))

You can give this command a key such as F5. [see Emacs: How to Define Keys]