/**
  * Get the path to an original or derivative file for an image.
  *
  * @param File $file
  * @param string $derivativeType
  * @return string|null Null if not exists.
  * @see UniversalViewer_View_Helper_IiifInfo::_getImagePath()
  */
 protected function _getImagePath($file, $derivativeType = 'original')
 {
     // Check if the file is an image.
     if (strpos($file->mime_type, 'image/') === 0) {
         // Don't use the webpath to avoid the transfer through server.
         $filepath = FILES_DIR . DIRECTORY_SEPARATOR . $file->getStoragePath($derivativeType);
         if (file_exists($filepath)) {
             return $filepath;
         } else {
             $filepath = $file->getWebPath($derivativeType);
             return $filepath;
         }
     }
 }
 /**
  * Get path of a file type.
  *
  * Paths aren't available after a file is stored, but it can be guessed.
  *
  * @param string $type
  * @return string Partial path
  */
 private function _getFilePath($type)
 {
     $file = new File();
     $filename = 'filename.jpg';
     $file->filename = $filename;
     $path = $file->getStoragePath($type);
     return trim(substr($path, 0, strlen($path) - strlen($filename)), '/');
 }
 /**
  * Get an array of the width and height of the image file.
  *
  * @internal The process uses the saved constraints. It they are changed but
  * the derivative haven't been rebuilt, the return will be wrong (but
  * generally without consequences for BookReader).
  *
  * @param File $file
  * @param string $imageType
  * @return array Associative array of width and height of the image file.
  * If the file is not an image, the width and the height will be null.
  * @see UniversalViewer_View_Helper_IiifManifest::_getImageSize()
  */
 protected function _getImageSize($file, $imageType = 'original')
 {
     static $sizeConstraints = array();
     if (!isset($sizeConstraints[$imageType])) {
         $sizeConstraints[$imageType] = get_option($imageType . '_constraint');
     }
     $sizeConstraint = $sizeConstraints[$imageType];
     // Check if this is an image.
     if (empty($file) || strpos($file->mime_type, 'image/') !== 0) {
         $width = null;
         $height = null;
     } else {
         $metadata = json_decode($file->metadata, true);
         if (empty($metadata['video']['resolution_x']) || empty($metadata['video']['resolution_y'])) {
             $msg = __('The image #%d ("%s") is not stored correctly.', $file->id, $file->original_filename);
             _log($msg, Zend_Log::NOTICE);
             if (isset($metadata['video']['resolution_x']) || isset($metadata['video']['resolution_y'])) {
                 throw new Exception($msg);
             }
             // Get the resolution directly.
             // The storage adapter should be checked for external storage.
             $storageAdapter = $file->getStorage()->getAdapter();
             $filepath = get_class($storageAdapter) == 'Omeka_Storage_Adapter_Filesystem' ? FILES_DIR . DIRECTORY_SEPARATOR . $file->getStoragePath($imageType) : $file->getWebPath($imageType);
             list($width, $height, $type, $attr) = getimagesize($filepath);
             if (empty($width) || empty($height)) {
                 throw new Exception($msg);
             }
         } else {
             $sourceWidth = $metadata['video']['resolution_x'];
             $sourceHeight = $metadata['video']['resolution_y'];
             // Use the original size when possible.
             if ($imageType == 'original') {
                 $width = $sourceWidth;
                 $height = $sourceHeight;
             } else {
                 // Source is landscape.
                 if ($sourceWidth > $sourceHeight) {
                     $width = $sizeConstraint;
                     $height = round($sourceHeight * $sizeConstraint / $sourceWidth);
                 } elseif ($sourceWidth < $sourceHeight) {
                     $width = round($sourceWidth * $sizeConstraint / $sourceHeight);
                     $height = $sizeConstraint;
                 } else {
                     $width = $sizeConstraint;
                     $height = $sizeConstraint;
                 }
             }
         }
     }
     return array('width' => $width, 'height' => $height);
 }
 /**
  * Get, check and set type of storage.
  *
  * @return string Path to the storage of the selected type of file.
  */
 protected function _getStorage()
 {
     if (is_null($this->_storage)) {
         $type = $this->_getType();
         // This is used to get list of storage path. Is there a better way?
         // getPathByType() is not secure.
         $file = new File();
         try {
             $storagePath = $file->getStoragePath($type);
         } catch (RuntimeException $e) {
             $this->_storage = false;
             return false;
         }
         $this->_storage = $type == 'original' ? substr($storagePath, 0, strlen($storagePath) - 1) : substr($storagePath, 0, strlen($storagePath) - strlen(File::DERIVATIVE_EXT) - 2);
     }
     return $this->_storage;
 }