Skip to content

Instantly share code, notes, and snippets.

@sdball
Created September 9, 2010 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdball/572016 to your computer and use it in GitHub Desktop.
Save sdball/572016 to your computer and use it in GitHub Desktop.
A scaling function for CONTENTdm
<?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);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment