public
Last active

A scaling function for CONTENTdm

  • Download Gist
calculate_contentdm_scaling.php
PHP
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
<?php
 
/**
* Return CONTENTdm scaling of $width and $height to $maxwidth and $maxheight.
*
* @param string $width starting width
* @param string $height starting height
* @param string $maxwidth width limit
* @param string $maxheight height limit
* @return array dmscale, dmwidth, dmheight
* @author Stephen Ball
*/
function calculate_contentdm_scaling($width, $height, $maxwidth, $maxheight)
{
$original_width = $width;
$original_height = $height;
// scale the width and height if necessary to get under maxwidth
if ($width > $maxwidth) {
$width_diff = $width / $maxwidth;
$width = $width / $width_diff;
$height = $height / $width_diff;
$target_dimension = $maxwidth;
$original_dimension = $original_width;
}
// scale the height and width if necessary to get under maxheight
if ($height > $maxheight) {
$height_diff = $height / $maxheight;
$height = $height / $height_diff;
$width = $width / $height_diff;
$target_dimension = $maxheight;
$original_dimension = $original_height;
}
// construct the scaling in "dm" values
$dmHeight = round($height, 0);
$dmWidth = round($width, 0);
$dmScale = ($target_dimension * 100) / $original_dimension;
return array($dmScale, $dmWidth, $dmHeight);
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.