+
+ // Returns the nearest power of 2 reduction factor that results in a larger image
+ function nearestPow2Reduce($desiredDimension, $sourceDimension) {
+ $ratio = floatval($sourceDimension) / floatval($desiredDimension);
+ return $this->nearestPow2ForScale($ratio);
+ }
+
+ // Returns nearest power of 2 reduction factor that results in a larger image
+ function nearestPow2ForScale($scale) {
+ $scale = intval($scale);
+ if ($scale <= 1) {
+ return 0;
+ }
+ $binStr = decbin($scale); // convert to binary string. e.g. 5 -> '101'
+ return strlen($binStr) - 1;
+ }
+
+ /*
+ * Parses a page request like "page5_r2.jpg" or "cover_t.jpg" to corresponding
+ * page type, size, reduce, and format
+ */
+ function parsePageRequest($pageRequest, $bookPrefix) {
+
+ // Will hold parsed results
+ $pageInfo = array();
+
+ // Normalize
+ $pageRequest = strtolower($pageRequest);
+
+ // Pull off extension
+ if (preg_match('#(.*)\.([^.]+)$#', $pageRequest, $matches) === 1) {
+ $pageRequest = $matches[1];
+ $extension = $matches[2];
+ if ($extension == 'jpeg') {
+ $extension = 'jpg';
+ }
+ } else {
+ $extension = 'jpg';
+ }
+ $pageInfo['extension'] = $extension;
+
+ // Split parts out
+ $parts = explode('_', $pageRequest);
+
+ // Remove book prefix if it was included (historical)
+ if ($parts[0] == $bookPrefix) {
+ array_shift($parts);
+ }
+
+ if (count($parts) === 0) {
+ $this->BRfatal('No page type specified');
+ }
+ $page = array_shift($parts);
+
+ $pageTypes = array(
+ 'page' => 'str',
+ 'n' => 'num',
+ 'cover' => 'single',
+ 'preview' => 'single',
+ 'title' => 'single',
+ 'leaf' => 'num'
+ );
+
+ // Look for known page types
+ foreach ( $pageTypes as $pageName => $kind ) {
+ if ( preg_match('#^(' . $pageName . ')(.*)#', $page, $matches) === 1 ) {
+ $pageInfo['type'] = $matches[1];
+ switch ($kind) {
+ case 'str':
+ $pageInfo['value'] = $matches[2];
+ break;
+ case 'num':
+ $pageInfo['value'] = intval($matches[2]);
+ break;
+ case 'single':
+ break;
+ }
+ }
+ }
+
+ if ( !array_key_exists('type', $pageInfo) ) {
+ $this->BRfatal('Unrecognized page type');
+ }
+
+ // Look for other known parts
+ foreach ($parts as $part) {
+ if ( array_key_exists($part, self::$imageSizes) ) {
+ $pageInfo['size'] = $part;
+ continue;
+ }
+
+ // Key must be alpha, value must start with digit and contain digits, alpha, ',' or '.'
+ // Should prevent injection of strange values into the redirect to datanode
+ if ( preg_match('#^([a-z]+)(\d[a-z0-9,.]*)#', $part, $matches) === 0) {
+ // Not recognized
+ continue;
+ }
+
+ $key = $matches[1];
+ $value = $matches[2];
+
+ if ( array_key_exists($key, self::$imageUrlKeys) ) {
+ $pageInfo[self::$imageUrlKeys[$key]] = $value;
+ continue;
+ }
+
+ // If we hit here, was unrecognized (no action)
+ }
+
+ return $pageInfo;
+ }
+
+ function getRegionDimensions($sourceDimensions, $regionDimensions) {
+ // Return region dimensions as { 'x' => xOffset, 'y' => yOffset, 'w' => width, 'h' => height }
+ // in terms of full resolution image.
+ // Note: this will clip the returned dimensions to fit within the source image
+
+ $sourceX = 0;
+ if (array_key_exists('x', $regionDimensions)) {
+ $sourceX = intAmount($regionDimensions['x'], $sourceDimensions['width']);
+ }
+ $sourceX = $this->clamp(0, $sourceDimensions['width'] - 2, $sourceX); // Allow at least one pixel
+
+ $sourceY = 0;
+ if (array_key_exists('y', $regionDimensions)) {
+ $sourceY = intAmount($regionDimensions['y'], $sourceDimensions['height']);
+ }
+ $sourceY = $this->clamp(0, $sourceDimensions['height'] - 2, $sourceY); // Allow at least one pixel
+
+ $sourceWidth = $sourceDimensions['width'] - $sourceX;
+ if (array_key_exists('w', $regionDimensions)) {
+ $sourceWidth = intAmount($regionDimensions['w'], $sourceDimensions['width']);
+ }
+ $sourceWidth = $this->clamp(1, max(1, $sourceDimensions['width'] - $sourceX), $sourceWidth);
+
+ $sourceHeight = $sourceDimensions['height'] - $sourceY;
+ if (array_key_exists('h', $regionDimensions)) {
+ $sourceHeight = intAmount($regionDimensions['h'], $sourceDimensions['height']);
+ }
+ $sourceHeight = $this->clamp(1, max(1, $sourceDimensions['height'] - $sourceY), $sourceHeight);
+
+ return array('x' => $sourceX, 'y' => $sourceY, 'w' => $sourceWidth, 'h' => $sourceHeight);
+ }
+
+ function getRegionDimensionsAsFloat($sourceDimensions, $regionDimensions) {
+ // Return region dimensions as { 'x' => xOffset, 'y' => yOffset, 'w' => width, 'h' => height }
+ // in terms of full resolution image.
+ // Note: this will clip the returned dimensions to fit within the source image
+
+ $sourceX = 0;
+ if (array_key_exists('x', $regionDimensions)) {
+ $sourceX = floatAmount($regionDimensions['x'], $sourceDimensions['width']);
+ }
+ $sourceX = $this->clamp(0.0, 1.0, $sourceX);
+
+ $sourceY = 0;
+ if (array_key_exists('y', $regionDimensions)) {
+ $sourceY = floatAmount($regionDimensions['y'], $sourceDimensions['height']);
+ }
+ $sourceY = $this->clamp(0.0, 1.0, $sourceY); // Allow at least one pixel
+
+ $sourceWidth = $sourceDimensions['width'] - $sourceX;
+ if (array_key_exists('w', $regionDimensions)) {
+ $sourceWidth = floatAmount($regionDimensions['w'], $sourceDimensions['width']);
+ }
+ $sourceWidth = $this->clamp(0.0, 1.0, $sourceWidth);
+
+ $sourceHeight = $sourceDimensions['height'] - $sourceY;
+ if (array_key_exists('h', $regionDimensions)) {
+ $sourceHeight = floatAmount($regionDimensions['h'], $sourceDimensions['height']);
+ }
+ $sourceHeight = $this->clamp(0.0, 1.0, $sourceHeight);
+
+ return array('x' => $sourceX, 'y' => $sourceY, 'w' => $sourceWidth, 'h' => $sourceHeight);
+ }
+
+ function intAmount($stringValue, $maximum) {
+ // Returns integer amount for string like "5" (5 units) or "0.5" (50%)
+ if (strpos($stringValue, '.') === false) {
+ // No decimal, assume int
+ return intval($stringValue);
+ }
+
+ return floatval($stringValue) * $maximum + 0.5;
+ }
+
+ function floatAmount($stringValue, $maximum) {
+ // Returns float amount (0.0 to 1.0) for string like "0.4" (40%) or "4" (40% if max is 10)
+ if (strpos($stringValue, ".") === false) {
+ // No decimal, assume int value out of maximum
+ return floatval($stringValue) / $maximum;
+ }
+
+ // Given float - just pass through
+ return floatval($stringValue);
+ }
+
+ function clamp($minValue, $maxValue, $observedValue) {
+ if ($observedValue < $minValue) {
+ return $minValue;
+ }
+
+ if ($observedValue > $maxValue) {
+ return $maxValue;
+ }
+
+ return $observedValue;
+ }
+
+ // Clean up temporary files and resources
+ function cleanup() {
+ foreach($this->tempFiles as $tempFile) {
+ unlink($tempFile);
+ }
+ $this->tempFiles = array();
+ }
+