html code to scale an image for a blog post?

Discussion in 'Web Design and Development' started by sir42, Nov 26, 2008.

  1. sir42 macrumors 6502

    Sep 16, 2003
    NY, NY
    I am trying to post an image to my blog, however I can't figure out how to scale the image to the size of my blog frame. The code that I'm using is:

    <a href="" target="_blank"><img src="" border="0" alt="Image 3"/></a>
    Is there code I can add to this that will automatically detect the width of my frame and scale the picture accordingly?

  2. angelwatt Moderator emeritus


    Aug 16, 2005
    You can scale an image to fit a div (or the like) using CSS. Check out this thread for some of the details. You'll essentially be setting the image attributes to 100% width and height and make sure the parent element has some width and height set, and of course using relative positioning. Again, see link for explanation in part.
  3. DaveTaylor macrumors 6502

    Nov 6, 2007
    Aberdeenshire, Scotland
    Another way of doing it is using the width and height syntax in the <img src tag:

    <img src="url" width="" height=""/>
  4. SrWebDeveloper macrumors 68000


    Dec 7, 2007
    Alexandria, VA, USA
    The behavior people usually want is to scale to fit only when the image is larger than the container. Until all browsers support CSS3 which has max-height and min-height selectors, below is a PHP based solution I've used for years with the following benefits:

    • Scales the image proportionately so the image does not distort
    • 100% server side processed - always looks the same on all browsers
    • No dependency on Javascript
    • No dependency on GD or any other graphics library - 100% native PHP, very fast too
    • Well supported CSS
    Basically the code below creates a div as a container set to the maximum width and height I wish via CSS. Then in one line I display an image where the HTML width and height attributes are returned based on a function I wrote that scales if the image width or height exceeds the maximum. This is a simple example only, obviously in production put the CSS in a stylesheet or in the head tag area.


    print "

        div.myimagebox {
            width: 500px; 
            height: 400px;
            margin: 12px;
        .images {border: 1px solid black; background-color: black;}

    <div class='myimagebox'>
        <img class='images' src='example.jpg' "
    .SetImageDimensions ("example.jpg",500,400)." border='0' alt='Test image...'>


    SetImageDimensions ($image_filename,$max_width,$max_height) {

    $image_filename=(strpos($image_filename,$_SERVER['DOCUMENT_ROOT'])===false) ? $_SERVER['DOCUMENT_ROOT']."/".$image_filename $image_filename ;
        if (
    file_exists($image_filename)) {
            if (
    $size[0]>$max_width || $size[1]>$max_height) {
    $scale min($max_width/$size[0], $max_height/$size[1]);
    $new_width intval(floor($scale*$size[0]));
    $new_height intval(floor($scale*$size[1]));
    "width='$new_width' height='$new_height'";
         else { return 
        else {return 

    Tested and works. Just change each instance of "example.jpg" to your actual image (.jpg, .gif, .tif, .png, etc.) with relative path. Adjust the CSS as you see fit, match the div width and height as the second and third arguments passed to the function.


Share This Page