Skip to content

Instantly share code, notes, and snippets.

@PhilSager
Last active October 12, 2015 06:07
Show Gist options
  • Save PhilSager/3982053 to your computer and use it in GitHub Desktop.
Save PhilSager/3982053 to your computer and use it in GitHub Desktop.
Script to get scaled image URL from CONTENTdm reference URL
<?php
/**
* Script to get scaled image URL from CONTENTdm reference URL
* Phil Sager <[email protected]>.
*
*/
$CDM_WEBSERVICES_HOME = "https://somedomain.contentdm.oclc.org"; // web services
$CONTENTDM_HOME = "http://somedomain.somewhere.org"; // public-facing CONTENTdm domain
$img_size = 400; // default size if none specified
$max_length = 4; // total number of characters allowed for the pixel size value, e.g. 4 digits, which will accommodate up to 9999
// function to send curls to CONTENTdm web service
function do_curl($curl_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!strpos($curl_url, 'xml')) {
$cdm_data_json = curl_exec($ch);
} else {
$cdm_data_xml = curl_exec($ch);
$xml = simplexml_load_string($cdm_data_xml);
$cdm_data_json = json_encode($xml);
}
curl_close($ch);
return json_decode($cdm_data_json, true);
}
if ( isset($_POST['link']) ) {
// make sure the posted link is a valid CONTENTdm reference URL
$pattern = "@".$CONTENTDM_HOME."\/cdm\/ref\/collection\/[A-Za-z0-9\/]{0,30}@";
if (!preg_match($pattern, $_POST['link'])) { exit; }
// get image size but only up to max length
if ( isset($_POST['imagesize']) && is_numeric($_POST['imagesize']) ) {
$img_size = substr($_POST['imagesize'],0,$max_length);
}
// get collection alias and image id
$CDM_link_arr = explode(",", preg_replace('/^.*collection\/(.*?)\/id\/(.*)$/','$1,$2',$_POST['link']));
// curl to get dimensions and scale to present image in crop window
$curl_url = $CDM_WEBSERVICES_HOME."/dmwebservices/index.php?q=dmGetImageInfo/".$CDM_link_arr[0]."/".$CDM_link_arr[1]."/xml";
$image_info = do_curl($curl_url);
// get width and scale info
$imgwidth = $image_info['width'];
$imgheight = $image_info['height'];
$longest_side = $imgwidth > $imgheight ? $imgwidth : $imgheight;
$trimmed_scale = "20";
//$scale = round(($img_size/$longest_side), 2);
$scale = $img_size/$longest_side;
$targ_w = $imgwidth*$scale;
$targ_h = $imgheight*$scale;
$formatted_scale = sprintf("%01.2f", $scale);
$trimmed_scale = substr($formatted_scale, 2);
if ($imgwidth < $img_size) { $trimmed_scale = 100; }
$scaled_link = $CONTENTDM_HOME.'/utils/ajaxhelper/?CISOROOT='.$CDM_link_arr[0].'&CISOPTR='.$CDM_link_arr[1].'&action=2&DMSCALE='.$trimmed_scale.'&DMWIDTH='.$targ_w.'&DMHEIGHT='.$targ_h;
}
?>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/1999/rec-html401-19991224/loose.dtd">
<html>
<head>
<title>Get sized CDM image from reference URL</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript">
function SelectAll(id) {
document.getElementById(id).focus();
document.getElementById(id).select();
}
</script>
<style type="text/css">
#scaleimage {
border: 1px solid gray;
padding: 10px;
display:-moz-inline-stack;
display:inline-block;
zoom:1;
*display:inline;
}
li {
text-align: left;
}
</style>
</head>
<body>
<div style="margin: 0 auto;text-align: center;">
<form id="scaleimage" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<p><b>Get a scaled image link from a CONTENTdm reference URL</b></p>
<ul>
<li>CDM Reference URL: <input type="text" name="link" size="30" value="<?php echo isset($_POST['link']) ? $_POST['link'] : '' ?>" /></li>
<li>Fit image into <input type="text" name="imagesize" size="4" value="<?php echo isset($_POST['imagesize']) ? $_POST['imagesize'] : '' ?>" /> pixel box. <input type="submit"></li>
</ul>
<div style="padding: 10px">
<?php
if (isset($_POST['link'])) {
echo 'Scaled image URL: <input id="cdmref" type="text" size="50" Value="'.$scaled_link.'" onClick="SelectAll(\'cdmref\');"">';
echo '<br/><br/>';
echo '<img src='.$scaled_link.'>';
}
?>
</div>
</form>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment