Resizing images only works for images types supported by the imagemagick-library.
The function imagemagick-types
returns a list of these types.
EDIT: Input M-: (setq val (imagemagick-types))
RET and C-h v val
RET to check what the function returns.
If the value of val
is nil
imagemagick is not enabled and you are out of luck. If the value is a list look whether your image type is member of that list.
For enabling all types supported by imagemagick set imagemagick-enabled-types
to t
.
You can do that with M-x customize-variable
RET imagemagick-enabled-types
RET. Just use the option Support all ImageMagick types from the Value menu.
If this prerequisites are fulfilled you can use the following lisp code to adapt
the image width to the window width in markdown-buffers.
(setq imagemagick-enabled-types t)
(defun image-p (obj)
"Return non-nil if OBJ is an image"
(eq (car-safe obj) 'image))
(defun iimage-scale-to-fit-width ()
"Scale over-sized images in the active buffer to the width of the currently selected window.
(imagemagick must be enabled)"
(interactive)
(let ((max-width (window-width (selected-window) t)))
(alter-text-property (point-min) (point-max)
'display
(lambda (prop)
(when (image-p prop)
(plist-put (cdr prop) :type 'imagemagick)
(plist-put (cdr prop) :max-width max-width)
prop)))))
(defun iimage-scale-on-window-configuration-change ()
"Hook function for major mode that display inline images:
Adapt image size via `iimage-scale-to-fit-width' when the window size changes."
(add-hook 'window-configuration-change-hook #'iimage-scale-to-fit-width t t))
(add-hook 'markdown-mode-hook #'iimage-scale-on-window-configuration-change)