Emacs bug, Elisp Get Image Width Height. 2024-11

By Xah Lee. Date: .

Emacs Lisp Get Image Width Height Bug

if you use elisp to get image width height, the result is incorrect

this took me a day to write a script to check and fix some 30 thousand image tags i've created in past few years.

GNU Emacs 29.4 (build 2, x86_64-w64-mingw32) of 2024-07-05

(defun my-get-image-dimensions (ImgFilepath)
  "Returns (cons width height) of a image.
The elements are string type.
Support png jpg svg gif and any image type emacs supports.
If the image is not supported, return nil.

;; 2024-11-20 todo. found a major bug. the size generated is not correct. it varies with the current emacs frame. for now, call `xah-get-image-dimensions-imk' instead.

Created: 2024-11-21
Version: 2024-11-21"
  (let ((xiext (downcase (file-name-extension ImgFilepath))) xitype)
    (setq xitype
          (cond
           ((string-equal xiext "jpg") 'jpeg)
           ((string-equal xiext "jpeg") 'jpeg)
           ((string-equal xiext "jfif") 'jpeg)
           ((string-equal xiext "png") 'png)
           (t (intern xiext))))
    (if (image-type-available-p xitype)
        (progn
          (clear-image-cache ImgFilepath)
          (image-size (create-image (file-truename ImgFilepath)) t))
      nil)))

;; HHHH------------------------------
;; test

(my-get-image-dimensions "c:/Users/xah/web/xahlee_info/emacs/misc/i/img_test_205x117.png")
;; (226 . 129)

;; incorrect

;; correct size is
;; 205 117

(emacs-version)
;; "GNU Emacs 29.4 (build 2, x86_64-w64-mingw32) of 2024-07-05"

sample test image

img test 205x117