Results 1 to 2 of 2

Thread: Constrain Proportions

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Constrain Proportions

    Hi all,

    Any ideas on how to constrain images echoed from DB via directory:

    Here is the source:

    PHP Code:
    <a href="<?php echo $_SERVER['BASE_NAME']; ?>img/galleries/drawings/<?php echo $row['Filename']; ?>" rel="lightbox" title="<?php echo $row['Caption']; ?>"><img src="img/galleries/drawings/thumbs/<?php echo $row['Filename']; ?>" alt="<?php echo $row['Caption']; ?>" title="<?php echo $row['Caption']; ?>" width="120" height="120" /></a>
    Problem is I need the box to remain in its dimensions, but have the image resize with out being distorted.

    Any ideas? thanks for any help and suggestions.
    Last edited by john0611; 08-17-2009 at 07:14 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You can resize the image to a percentage w*h. For example, let's say your image is 300 x 500 pxls. You loop it per percentage until the height is 120pxls or below [-70%]. You would subtract that same -70% from the width so that the final dimensions are 70 x 120 [at or below the image specifications, w/o distorting it.]

    This code should work for that:
    PHP Code:
    <?php

    $size 
    getimagesize($filename);
    $width $size[0];
    $height $size[1];

    function 
    loopResize($w$h) {
    $percent 10%; // percentage to resize per loop, can be chged for a more approximate calculation
    $percentW = ($width 100) * $percent;// calc perct.
    $percenH = ($height 100) * $percent// calculate percent

    $w = ($w $percentW); //subtract {x} percent
    $h = ($h $percentH); //subtract {x} percent
    if($w 120) {
    loopResize($w); // keep resizing until fit
    }

    if(
    $h 120) {
    loopResize($h); // keep resizing until fit
    }

    //end function

    if($w 120) {
    loopResize($w);
    }

    if(
    $h 120) {
    loopResize($h);
    }

    if((
    $h <= 120) && ($w <= 120)) {

    echo 
    '<a href="'.$_SERVER['BASE_NAME'].'img/galleries/drawings/'.$row['Filename'].'" rel="lightbox" title="'.$row['Caption'].'"><img src="img/galleries/drawings/thumbs/'.$row['Filename'].'" alt="'.$row['Caption'].'" title="'.$row['Caption'].'" width="120" height="120" /></a>'// img dimensions fit, return the output.

    }

    ?>
    HTH
    - Josh

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •