When displaying images added by users it is quite often necessary to resize them in order that they fit into a specific area of the page. Not doing this can cause problems with the images breaking the page layout. The trouble is that if you resize the image absolutely you tend to squash and distort it.
The following function can be used to calculate the width and height of an image to the correct aspect ratio, which will preserve the contents when printed out. This function makes use of the getimagesize() function, which is available as part of the GD library in PHP.