views:

376

answers:

3

How to set maximum height or width for:

$img_attributes= ' height=100 width=100 '. 'alt="'.$product['product_name'].'"';

+1  A: 

Well, there are the max-height and max-width CSS properties, aren't they? THey work in all major browsers except IE 6 and in IE 7 you need to take note of this.

Pekka
Hi Pekka, I haven't tested it yet, but can this work? $img_attributes= ' max-height=100 max-width=100 '. 'alt="'.$product['product_name'].'"';
Chris
Nope, you need to add a `style`: `style='max-height: 100px; max-width: 100px' `
Pekka
A: 

You should check this answer for general information : http://stackoverflow.com/questions/115462/proportional-image-resize.

If you want to have an image resized without using server side I suggest you to user Javascript. Here is a tutorial.

In short you have a JavaScript function that will return the new Width and Height:

function scaleSize(maxW, maxH, currW, currH){
  var ratio = currH / currW;
  if(currW >= maxW){
        currW = maxW;
        currH = currW * ratio;
  } else >if(currH >= maxH){
        currH = maxH;
        currW = currH / ratio;
  }
  return [currW, currH];
}

With this function you can set the image width and height:

img.width = newW;
img.height = newH;

But, the best way would be to do it at server side. This will prevent to have a weird effect on client side.

Daok
A: 

The following style will cause all images using the "MaxSized" css class to have a max height of 100px and a max width of 100px. If an image is smaller, it will not be stretched.

<style>
IMG.MaxSized
{
max-width: 100px;
max-height: 100px;
}
</style>

As mentioned by Pekka, you'll have to have a XHTML 1.0 Strict DTD in order for this to work in modern versions of IE, but I personally believe this is the appropriate approach.

Brian Hasden
problem is if the image is bigger both in width and height, but not square.
Adriano Varoli Piazza
Will you get a sort of loop then?
Chris
That doesn't matter. If you want to limit the height or the width, you can with the max-height and max-width styles. If the image is larger vertically, it will be limited to the maximum height specified and scaled appropriate. Same goes for the width. The scale doesn't change.
Brian Hasden
I'm not sure what you mean by loop? I'm talking about CSS, not code. The CSS limits the displayed size of the image. It's determined on the client side, not server side and doesn't really have an execution path for it to loop.
Brian Hasden