If you are inputting all these images yourself, you could simply do a batch file convert in Photoshop where it converts all images to 800px width. Then remove the images that are tall and batch resize them to 800px height. Then you won't need any coding at all.
If you want them all to be square, then simply batch convert all the 800px tall images to 800px wide with white background, and batch convert the wide images to 800px tall with white background.
When all is done, you can batch convert them all to 150px for thumbnails.
If you want a border around each image, then you should do it with CSS so you can adjust it globally.