我可以调整图像大小以适应桌子的宽度吗?

I'm using a BB code function to allow members to post images and such in comments and topics. My problem is if an image is too big to fit the table, it expands everything past the width. Are there any workarounds for this? Either in the table or the td? A predefined width and height for the image won't work because I'll never know how big each image is. Is there a way to make a max width/height for the image or something along those lines?

Here's a bit of code you can run to force a maximum width if the images are being stored on your server:

$maxWidth = 450;
$info = getimagesize("../path/to/image.jpg");
if( $info[0] > $maxWidth )
{
    print '<img src="../path/to/image.jpg" width="' . $maxWidth . '" />';
}
else
{
    print '<img src="../path/to/image.jpg" />';
}

The height will scale to the width, so nothing gets too stretched out.

Edit:

  1. If you want to actually resize the images on your server, I have a PHP library that might be of help to you, found on my github, here.
  2. If you want to do it all clientside, without storing the image locally, you should be able to create a new Javascript Image(), set the src to the image and then determine the width and height of the image through Javascript's built-in methods. You could then dynamically alter the width/height of the <img> element to be no larger than your pre-determined width/height.

Set the TD a max height or width and set the img to 100% width/height