Results 1 to 5 of 5
  1. #1

    PHP, GD, Proportional Resizing

    I am trying to find some code that will check the height and width (in pixels) of an image and then, if it is bigger than MY specified width (say I set the maximum size and 640 pixels wide and 480 pixels tall) it will **proportionally** resize it down. Like using the 'Image > Resize > Image Size...' function in Photoshop. If the image was 700px by 500px, the script would see that both the x and y are larger than the limit, so it would make the image 640px by 457px. Anyone have any idea how to do this? I looked all over for 'php, gd, resize image' and a bunch of other stuff, but I just kept getting a bunch of thumbnail generators.

  2. #2
    Join Date
    Jan 2004
    York, UK
    Some old code of mine for making phone themes does that. You can grab the source form (go to the page first - I think I put hotlink protection on for that site and if so it will stop direct downloads of the zip files). themeauto.php and image.php are the code files to look for image resizing in.

    (ignore the mention of hosting on that page - I no longer offer hosting but haven't bothered to update that site)

  3. #3
    nothing hard to do - look here:
    if ( $img_width > 640 || $img_height > 480) {
      $prop = $img_width/$img_height;  
      if ( $prop> 1.33 ) {  // 1.33 got 640/480
         $newwidth = round( 640*($prop*0.75 ); // 0.75 = 1 /  (4/3) where 4/3=640/480
         $newheight = 480;
      } else {
         $newwidth = 640;
         $newheight = round( 480 * 1.33/$prop);
    after getting new sizes just resize image using resample.
    HostNodeList Web Host Directory, DEV.INTOEX.COM - products for online business

    Experienced web-developer | PHP | Smarty | Zend | Databases | Graphic design - looking for long-time relationship

  4. #4
    Join Date
    Jun 2006
    If you use php, maybe it could make a high page generation time, I tried some time ago and it took so long to load...
    You can use javascript:
    include this in the <head> part of your page
    <script type="text/javascript">
    function redimImg(foto, dimensioneMax){
    w = foto.width;
    h = foto.height;
    if(w <= dimensioneMax && h>dimensioneMax){
    foto.height = dimensioneMax;
    if(w > dimensioneMax && h<=dimensioneMax){
    foto.width = dimensioneMax;
    if(w > dimensioneMax && h>dimensioneMax){
    deltaX = w - dimensioneMax;
    deltaY = h - dimensioneMax;
    if (deltaX > deltaY){
    foto.width = dimensioneMax;
    else {
    foto.height = dimensioneMax;
    Then you can call your image like this:
    <img src="image.jpg" onload="redimImg(this, 100);">
    Where you can change 100 into the maximum image width and height you would like.

  5. #5
    Join Date
    May 2005
    Planet Earth
    The code you are looking for is well described on


    Example 2. Resampling an image proportionally


    G Disrupting traditional file hosting.
    █ Signup Early and enjoy Unlimited space/bandwidth for your files hosting, Forever!
    █ No Ads.
    █ No Countdowns.

Posting Permissions

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