Plot resizes with old layout after downloading image with edited layout

Hi. I have a Plot with a rangeslider but when I download an image I want to remove the rangeslider and remove all the legends that are not enabled on the plot to reduce clutter. When removing the disabled legends the plot works perfect where it doesn’t show them on the image and it doesn’t change their state in the browser window either.

When disabling the rangeslider it works fine in the image where it doesn’t show up but after downloading the image the plot reloads in the browser and it enables all the legends that were disabled before a.k.a it makes changes to the plot which were not made by the user. I traced it down to the fact that Plotly resizes the plot because it changed in layout.

How can I prevent this reload or how can I make it reload with the same layout as before downloading the image?

Here is my code for download image button:

click: function (gd) {
	gd.layout.xaxis.rangeslider = false
	let data = gd.data.map(function (datapoint) {
		if (datapoint.visible === "legendonly") {
			datapoint.visible = false;
		}
		return datapoint
	})
	gd.data = data;
	Plotly.downloadImage(gd, {
		filename: '#####_' + moment().format("YYYYMMDD_HHmmss"),
		format: 'png',
	})
}

I managed to change the code so that it doesn’t redraw the plot like this:

click: function (gd) {
	gd.layout.xaxis.rangeslider = { visible: false };
	gd.layout.legend = {
		orientation: "h",
		y: -0.1,
		itemsizing: 'constant',
	};
	gd.layout.xaxis.rangeselector = { visible: false };
	let data = gd.data.map(function (datapoint) {
		if (datapoint.visible === "legendonly") {
			datapoint.visible = false;
		}
		return datapoint
	})
	gd.data = data;
	Plotly.downloadImage(gd, {
		filename: 'miran_' + moment().format("YYYYMMDD_HHmmss"),
		format: 'png',
	})
}

But now when I try to change the size of the image downloaded, it again does the same redraw and makes changes to the legends. Here are both codes that I tried using after the format: ‘png’:

width: 1000,
height: (gd._fullLayout.height / gd._fullLayout.width) * 1000,
width: gd._fullLayout.width,
height: gd._fullLayout.height,