Emacs Lisp: HTML image-link Command

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

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 (φfile-path)
  "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]
URL `http://ergoemacs.org/emacs/elisp_image_tag.html'
Version 2015-06-13"
  (let ((ξx nil)
        (ξy nil))
     ((string-match "\.svg$" φfile-path)
          ;; hackish. grab the first occurence of width height in file
          (insert-file-contents φfile-path)
          (goto-char (point-min))
          (when (search-forward-regexp "width=\"\\([0-9]+\\).*\"" nil 'NOERROR)
            (setq ξx (match-string 1 )))
          (goto-char (point-min))
          (if (search-forward-regexp "height=\"\\([0-9]+\\).*\"" nil 'NOERROR)
              (setq ξy (match-string 1 ))))
        (if (and (not (null ξx)) (not (null ξy)))
            (progn (vector (string-to-number ξx) (string-to-number ξy)))
          (progn [0 0]))))
      (let (ξxy )
          (clear-image-cache t)
          (setq ξxy (image-size
                      (if (file-name-absolute-p φfile-path)
                        (concat default-directory φfile-path)))
        (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 (φimg-file-path)
  "Returns a image file's width and height as a vector.
This function requires ImageMagick's “identify” shell command.
See also: `xah-get-image-dimensions'.
URL `http://ergoemacs.org/emacs/elisp_image_tag.html'
Version 2015-05-12"
  (let ((ξwidth-height
            "identify -format \"%w %h\" "
     (string-to-number (elt ξwidth-height 0))
     (string-to-number (elt ξwidth-height 1)))))

This function makes a shell call to the ImageMagick's identify command. 〔➤ Linux: ImageMagick Command Line Tutorial

Final Code

Here's the main code.

(defun xah-html-image-linkify ()
  "Replace image file path under cursor to HTML img inline link.
 <img src=\"emacs_logo.png\" alt=\"emacs logo\" width=\"123\" height=\"456\" />

URL `http://ergoemacs.org/emacs/elisp_image_tag.html'
Version 2015-06-13"
  (let* ((ξbounds (bounds-of-thing-at-point 'filename))
         (ξp1 (car ξbounds))
         (ξp2 (cdr ξbounds))
         (ξimgPath (buffer-substring-no-properties ξp1 ξp2 ))
           (file-name-directory (or (buffer-file-name) default-directory))))
           "_" " "
            "\\.[A-Za-z]\\{3,4\\}$" "" (file-name-nondirectory ξimgPath) t t) t t))
         (ξimgWH (xah-get-image-dimensions ξimgPath))
         (ξwidth (number-to-string (elt ξimgWH 0)))
         (ξ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. 〔➤ Emacs: How to Define Keys

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus