How to position images within a canvas while avoiding overlapping
If there is a place for the new image to fit, there must be a place where the left edge of the new image is aligned with either the left edge of the canvas or of some previous image. The same true for the top edge of the new image.  So, if you've already placed 2 images, you only have to try 3 possibilities for the left edge and 3 possibilities for the top edge, or a total of 9 candidate positions in total.  Given a candidate position, you should be easily able to test whether a new image can be placed there without overlapping any prior image.