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
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
Comments
Post new comment