Solution
I used the source code in lisp/image-mode.el to write this up. Calling this function in any image buffer will resize it to fit to width or height depending on the image and window height/width ratios.
You do need these 2 things for this function to work:
- Your Emacs needs to have been compiled with ImageMagick.
- By default
libjpeg loader is used to handle JPEG images. The hack shown in next part of this answer is used to force emacs to use imagemagick loader.
(defun modi/image-transform-fit-to-window() "Resize the image to fit the width or height based on the image and window ratios. Imagemagick is required to run this function." (interactive) (let* ( (img-size (image-display-size (image-get-display-property) t)) (img-width (car img-size)) (img-height (cdr img-size)) (img-h/w-ratio (/ (float img-height) (float img-width))) (win-width (- (nth 2 (window-inside-pixel-edges)) (nth 0 (window-inside-pixel-edges)))) (win-height (- (nth 3 (window-inside-pixel-edges)) (nth 1 (window-inside-pixel-edges)))) (win-h/w-ratio (/ (float win-height) (float win-width)))) ;; Fit image by width if the h/w ratio of window is > h/w ratio of the image (if (> win-h/w-ratio img-h/w-ratio) (image-transform-fit-to-width) ;; Else fit by height (image-transform-fit-to-height))))
Hack to force Emacs to use ImageMagick for loading images
After following through the emacs bug reports #18797, #10746 and #10112, the following solution worked for forcing Imagemagick to load images; put it somewhere in your init.el:
I have forced Emacs to use ImageMagick only for the image files familiar to me: PNG, TIFF, JPEG, SVG; the rest are kept as default. I have chosen to allow gif files to be opened by the default loader as emacs 24.4 has improved support for viewing multi-frame images. The whole regexp is taken from lisp/image.el.
(setq image-type-header-regexps `( ("\\`/[\t\n\r ]*\\*.*XPM.\\*/" . xpm) ("\\`P[1-6]\\\(?:\ \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[[:space:]]\\)+\ \\(?:\\(?:#[^\r\n]*[\r\n]\\)?[0-9]\\)+\ \\)\\{2\\}" . pbm) ("\\`GIF8[79]a" . gif) ;; ("\\`\x89PNG\r\n\x1a\n" . png) ;; Uncomment this (and comment the below line) to enable inline PNG images in org-mode ("\\`\x89PNG\r\n\x1a\n" . imagemagick) ; PNG ("\\`[\t\n\r ]*#define \\([a-z0-9_]+\\)_width [0-9]+\n\ #define \\1_height [0-9]+\n\\(\ #define \\1_x_hot [0-9]+\n\ #define \\1_y_hot [0-9]+\n\\)?\ static \\(unsigned \\)?char \\1_bits" . xbm) ;; ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . tiff) ("\\`\\(?:MM\0\\*\\|II\\*\0\\)" . imagemagick) ; TIFF ("\\`[\t\n\r ]*%!PS" . postscript) ;; ("\\`\xff\xd8" . jpeg) ;; Uncomment this (and comment the below line) to enable inline JPEG images in org-mode ("\\`\xff\xd8" . imagemagick) ; JPEG ("\\`\377\330" . imagemagick) ; JPEG (,(let* ((incomment-re "\\(?:[^-]\\|-[^-]\\)") (comment-re (concat "\\(?:!--" incomment-re "*-->[ \t\r\n]*<\\)"))) (concat "\\(?:<\\?xml[ \t\r\n]+[^>]*>\\)?[ \t\r\n]*<" comment-re "*" "\\(?:!DOCTYPE[ \t\r\n]+[^>]*>[ \t\r\n]*<[ \t\r\n]*" comment-re "*\\)?" "[Ss][Vv][Gg]")) ;; . svg) ;; Uncomment this (and comment the below line) to enable inline SVG images in org-mode . imagemagick) ; SVG ))
Drawback
- Image types chosen to be loaded by ImageMagick will not show up as inline images in Org mode.
References