Created
September 9, 2010 15:30
-
-
Save sdball/572016 to your computer and use it in GitHub Desktop.
A scaling function for CONTENTdm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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