Just specifying the maxWidth: "auto" option in the popup seems like enough ...
layer.bindPopup(popupContent, { maxWidth: "auto" });
Demo: http://jsfiddle.net/3v7hd2vx/29/
EDIT
Unfortunately, if the image has not yet been loaded into the browser cache, a pop-up window will immediately open with the default size and adjust its size, but not its position, as soon as the image is fully loaded and displayed. As a result, the pop-up window shifts and its arrow is inappropriate compared to the marker to which it is attached.
A simple workaround is to listen for the "load" event and reopen the popup at this point:
popupContent = document.createElement("img"); popupContent.onload = function () { layer.openPopup(); }; popupContent.src = "path/to/image"; layer.bindPopup(popupContent, { maxWidth: "auto" });
Updated demo: http://jsfiddle.net/3v7hd2vx/32/
EDIT 2
Here's a more general solution: capture the <IMG> "load" event in all Leaflet popups and force them to update() each time.
document.querySelector(".leaflet-popup-pane").addEventListener("load", function (event) { var tagName = event.target.tagName, popup = map._popup;
Demo: https://jsfiddle.net/3v7hd2vx/277/
Link: Leaflet No. 5484 (comment)
ghybs source share