Image resizing with aspect ration maintained....

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Image resizing with aspect ration maintained....

    I have a section(185pixelsx 185pixels) in my web page to display an image
    that is stored in a directory. Using php, how do you resize so if:

    the image dimension is smaller(width and height is less than 185pixels),
    display it in the center of the section

    if the width OR height is larger than 185pixel, resize them with aspect
    ratio maintained so both width and height is <= 185pixels. Then display it
    in the center of the section.

    Thanks



    Ruby Tuesday Guest

  2. Similar Questions and Discussions

    1. Resizing a image!
      Hello, I just started with PHP. And I have made a code for uploading images. Now I wil resize the image also to a tumbnail, but when I search...
    2. resizing grouped text without losing point size & aspect ratio
      If I resize an individual area type object by dragging the bounding box handles, its text rewraps, but the point sizes and aspect ratios of its...
    3. Resizing Movies to fit res...but keeping aspect ratio.
      OK...I've looked for this soultion around but I havent found it. Basically I have a page with flash (www.madisyn.com , click on "knight" image,...
    4. Controlling aspect ration of browser window
      Does anybody know of a piece of code that will allow the visitor to resize their browser window but control the aspect ratio of the resize?
    5. Resizing high res image smaller results in blurred image
      Hi there, I have a high res logo in PSD format (around 1500px x 1500px) but when I resize it to around 300px x 300px the resulting image is not...
  3. #2

    Default Re: Image resizing with aspect ration maintained....

    Ruby Tuesday wrote:
    > I have a section(185pixelsx 185pixels) in my web page to display an image
    > that is stored in a directory. Using php, how do you resize so if:
    >
    > the image dimension is smaller(width and height is less than 185pixels),
    > display it in the center of the section
    >
    > if the width OR height is larger than 185pixel, resize them with aspect
    > ratio maintained so both width and height is <= 185pixels. Then display it
    > in the center of the section.
    Hi,

    I just wrote a function doing this yesterday.
    --> [url]http://www.hightech-board.de/thread.php?postid=18101#post18101[/url]

    (Sorry, the explanations are in german, but you'll understand the php
    code, I think)

    It resizes jpgs and pngs, if GD2 is installed and gifs, too, if there
    is gif reading support.

    Greetz
    Paul.

    P.S.: And here the code without Highlighting... ;-)

    ================================================== ========
    <?php
    function resizeImg($imgPath, $maxWidth = 100, $maxHeight = 100,
    $directOutput = false, $quality = 90, $verbose = false)
    {
    // get image size infos (0 width and 1 height,
    // 2 is (1 = GIF, 2 = JPG, 3 = PNG)
    $size = getimagesize($imgPath);

    // break and return false if failed to read image infos
    if(!$size){
    if($verbose && !$directOutput)echo "<br />Not able to read
    image infos.<br />";
    return false;
    }

    // relation: width/height
    $relation = $size[0]/$size[1];
    // maximal size (if parameter == false, no resizing will be made)
    $maxSize = array(
    $maxWidth?$maxWidth:$size[0],
    $maxHeight?$maxWidth:$size[1]
    );
    // declaring array for new size (initial value = original size)
    $newSize = $size;
    // width/height relation
    $relation = array($size[1]/$size[0], $size[0]/$size[1]);

    // get new dimension, if necessary
    $i = 0;
    while((($newSize[0] > $maxSize[0]) || ($newSize[1] >
    $maxSize[1])) && ($i < 2))
    {
    $newSize[$i] = intval(min($newSize[$i], $maxSize[$i]));
    $newSize[(($i+1)%2)] = intval($relation[$i]*$newSize[$i]);
    $i++;
    }
    // need to resize?
    if(!$i){
    if($verbose && !$directOutput)echo "<br />No need to resize.<br
    />";
    if(!$directOutput)return true;
    }

    // create image
    switch($size[2])
    {
    case 1:
    if(function_exists("imagecreatefromgif"))
    {
    $originalImage = imagecreatefromgif($imgPath);
    }else{
    if($verbose && !$directOutput)echo "<br />No GIF support
    in this php installation, sorry.<br />";
    return false;
    }
    break;
    case 2: $originalImage = imagecreatefromjpeg($imgPath); break;
    case 3: $originalImage = imagecreatefrompng($imgPath); break;
    default:
    if($verbose && !$directOutput)echo "<br />No valid image
    type.<br />";
    return false;
    }

    // create new image
    $resizedImage = imagecreatetruecolor($newSize[0], $newSize[1]);

    imagecopyresampled(
    $resizedImage, $originalImage,
    0, 0, 0, 0,
    $newSize[0], $newSize[1], $size[0], $size[1]);

    // output or save
    if($directOutput){
    imagejpeg($resizedImage);
    }else{
    imagejpeg($resizedImage,
    preg_replace("/\.([a-zA-Z]{3,4})$/",".jpg",$imgPath), $quality);
    }

    // return true if successfull
    return true;
    }
    ?>
    ================================================== ============
    Paul 'piz' Wellner Bou Guest

  4. #3

    Default Re: Image resizing with aspect ration maintained....

    ["Followup-To:" header set to comp.lang.php.]
    Ruby Tuesday wrote:
    > I have a section(185pixelsx 185pixels) in my web page to display an image
    > that is stored in a directory. Using php, how do you resize so if:
    >
    > the image dimension is smaller(width and height is less than 185pixels),
    > display it in the center of the section
    >
    > if the width OR height is larger than 185pixel, resize them with aspect
    > ratio maintained so both width and height is <= 185pixels. Then display it
    > in the center of the section.
    <?php
    define('AVAILABLE_WIDTH', 185);
    define('AVALIABLE_WIDTH', 185);

    // Get image size: [url]http://www.php.net/getimagesize[/url]
    // into $width and $height

    $reducex = AVAILABLE_WIDTH / $width;
    $reducey = AVAILABLE_HEIGHT / $height;

    $reduce = min($reducex, $reducey);
    if ($reduce < 1) {
    // resize: [url]http://www.php.net/imagecopyresized[/url]
    // or resample: [url]http://www.php.net/imagecopyresampled[/url]
    // with $reduce paying a important role in the process
    }

    // output the (possibly resized) image
    ?>


    Sorry for the pseudo and incomplete code -- but I think I can make it
    clearer with code than with words.
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Graca Guest

  5. #4

    Default Re: Image resizing with aspect ration maintained....

    this should help you:
    [url]http://www.gzentools.com/gzimg.php[/url]

    is a free little lib of functions that will easily do what you want.
    as far as cenetering top to bottom, is puts thumb on top, so when images are
    in a row, thier tops line up.
    the function is gzImagePad(). but you can modify it to center middle if you
    want.


    --
    Mike Bradley
    [url]http://www.gzentools.com[/url] -- free online php tools
    "Ruby Tuesday" <rubytuzdayz@yahoo.com> wrote in message
    news:c108kj$1cel2p$1@ID-205437.news.uni-berlin.de...
    > I have a section(185pixelsx 185pixels) in my web page to display an image
    > that is stored in a directory. Using php, how do you resize so if:
    >
    > the image dimension is smaller(width and height is less than 185pixels),
    > display it in the center of the section
    >
    > if the width OR height is larger than 185pixel, resize them with aspect
    > ratio maintained so both width and height is <= 185pixels. Then display it
    > in the center of the section.
    >
    > Thanks
    >
    >
    >

    CountScubula Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139