Generate the images with the correct size

The images generated by the original script have the same size as the document.
This pull request fixes that so every image has the same size as its layer.

It selects the current visible layer and trims the document before saving the layer.
At the end, the original document canvas size is restored.