User login

Scale a square's dimensions with PHP

Or determine and apply any ratio.

Agaric needed this for a Drupal module, but this is general PHP that should work for anything.

php scaling function length height
calculate a ratio

Below, Agaric's function for scaling two dimensions maintaining their aspect ratio given a limit for one or both dimensions (or a percentage, but in retrospect it is very silly to wrap multiplication in a function).

/**
* Scales two dimensions based on one or two limits or a percentage value.
*
* THIS FUNCTION TAKES VARIABLES BY REFERENCE
* It does not return anything you'll find useful.
* Instead, it modifies directly the variables you pass it.
*
* This function could be extended to also scale up to the smaller of the $*max variables.
*/
function coamod_scale_dimensions(&$first, &$second, $firstmax, $secondmax = NULL) {
  if ($secondmax == 'percentage' || $secondmax == 'percent' || $secondmax == '%') {  // treat $firstmax as a percentage scale, not a limit
    $ratio = $firstmax/100;
  }
  else {
    if ($first > $firstmax) {
      $reduce = $first - $firstmax;
      $ratio = 1 - $reduce/$first;
    }
    if ($secondmax && is_numeric($secondmax)) {  // redundant?  Anyhow, if we're getting a second limit, calculate its ration
      $reduce = $second - $secondmax;
      $secondratio = 1 - $reduce/$second;
      if ($secondratio < $ratio)  $ratio = $secondratio;
    }
  }
  $first = $first*$ratio;
  $second = $second*$ratio;
  return $ratio;
}

This would work best knowing the browser window size.

php get browser window dimensions
drupal get browser window size

http://www.faqts.com/knowledge_base/view.phtml/aid/131

Or determine and apply any ratio.

Agaric needed this for a Drupal module, but this is general PHP that should work for anything.

php scaling function length height
calculate a ratio

Below, Agaric's function for scaling two dimensions maintaining their aspect ratio given a limit for one or both dimensions (or a percentage, but in retrospect it is very silly to wrap multiplication in a function).

/**
* Scales two dimensions based on one or two limits or a percentage value.
*
* THIS FUNCTION TAKES VARIABLES BY REFERENCE
* It does not return anything you'll find useful.
* Instead, it modifies directly the variables you pass it.
*
* This function could be extended to also scale up to the smaller of the $*max variables.
*/
function coamod_scale_dimensions(&$first, &$second, $firstmax, $secondmax = NULL) {
  if ($secondmax == 'percentage' || $secondmax == 'percent' || $secondmax == '%') {  // treat $firstmax as a percentage scale, not a limit
    $ratio = $firstmax/100;
  }
  else {
    if ($first > $firstmax) {
      $reduce = $first - $firstmax;
      $ratio = 1 - $reduce/$first;
    }
    if ($secondmax && is_numeric($secondmax)) {  // redundant?  Anyhow, if we're getting a second limit, calculate its ration
      $reduce = $second - $secondmax;
      $secondratio = 1 - $reduce/$second;
      if ($secondratio < $ratio)  $ratio = $secondratio;
    }
  }
  $first = $first*$ratio;
  $second = $second*$ratio;
  return $ratio;
}

This would work best knowing the browser window size.

php get browser window dimensions
drupal get browser window size

http://www.faqts.com/knowledge_base/view.phtml/aid/131

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.