Esempio n. 1
0
 function testTransitions()
 {
     exec('which gs 2>&1', $output, $return);
     $this->skipUnless($return === 0, 'gs command not available');
     $Medium = new DocumentMedium($this->TestData->getFile('application-pdf.pdf'));
     $Medium = $Medium->convert('image/png');
     if ($this->assertIsA($Medium, 'ImageMedium')) {
         $tmpFile = $Medium->store(TMP . uniqid('test_suite_'));
         $this->assertEqual(MimeType::guessType($tmpFile), 'image/png');
         unlink($tmpFile);
     }
     $Medium = new DocumentMedium($this->TestData->getFile('application-pdf.pdf'));
     $Medium = $Medium->convert('image/png');
     if ($this->assertIsA($Medium, 'ImageMedium')) {
         $result = $Medium->fit(10, 10);
         $this->assertTrue($result);
         $this->assertTrue($Medium->width() <= 10);
         $this->assertTrue($Medium->height() <= 10);
     }
 }
Esempio n. 2
0
 function testManipulation()
 {
     $Medium = new TestImagickImageMedium($this->TestData->getFile('image-jpg.jpg'));
     $Medium->fit(10, 10);
     $this->assertTrue($Medium->width() <= 10);
     $this->assertTrue($Medium->height() <= 10);
     $Medium = new TestImagickImageMedium($this->TestData->getFile('image-jpg.jpg'));
     $Medium = $Medium->convert('image/png');
     if ($this->assertIsA($Medium, 'ImageMedium')) {
         $result = $Medium->mimeType;
         $this->assertEqual($result, 'image/png');
     }
     $Medium = new TestImagickImageMedium($this->TestData->getFile('image-jpg.jpg'));
     $Medium = $Medium->convert('image/png');
     if ($this->assertIsA($Medium, 'ImageMedium')) {
         $tmpFile = TMP . uniqid('test_suite_');
         $tmpFile = $Medium->store($tmpFile);
         $this->assertEqual(MimeType::guessType($tmpFile), 'image/png');
         unlink($tmpFile);
     }
 }
Esempio n. 3
0
 /**
  * Gather/Return information about a resource
  *
  * @param mixed $resource Path to file in local FS, URL or file-upload array
  * @param string $what scheme, host, port, file, MIME type, size, permission,
  * 	dirname, basename, filename, extension or type
  * @return mixed
  */
 function info(&$Model, $resource, $what = null)
 {
     extract($this->settings[$Model->alias], EXTR_SKIP);
     $defaultResource = array('scheme' => null, 'host' => null, 'port' => null, 'file' => null, 'mimeType' => null, 'size' => null, 'pixels' => null, 'permisssion' => null, 'dirname' => null, 'basename' => null, 'filename' => null, 'extension' => null, 'type' => null);
     /* Currently HTTP is supported only */
     if (TransferValidation::url($resource, array('scheme' => 'http'))) {
         $resource = array_merge($defaultResource, pathinfo(parse_url($resource, PHP_URL_PATH)), array('scheme' => parse_url($resource, PHP_URL_SCHEME), 'host' => parse_url($resource, PHP_URL_HOST), 'port' => parse_url($resource, PHP_URL_PORT), 'file' => $resource, 'type' => 'http-url-remote'));
         if (!class_exists('HttpSocket')) {
             App::import('Core', 'HttpSocket');
         }
         $Socket =& new HttpSocket(array('timeout' => 5));
         $Socket->request(array('method' => 'HEAD', 'uri' => $resource['file']));
         if (empty($Socket->error) && $Socket->response['status']['code'] == 200) {
             $resource = array_merge($resource, array('size' => $Socket->response['header']['Content-Length'], 'mimeType' => $trustClient ? $Socket->response['header']['Content-Type'] : null, 'permission' => '0004'));
         }
     } elseif (MediaValidation::file($resource, false)) {
         $resource = array_merge($defaultResource, pathinfo($resource), array('file' => $resource, 'host' => 'localhost', 'mimeType' => MimeType::guessType($resource, array('paranoid' => !$trustClient))));
         if (TransferValidation::uploadedFile($resource['file'])) {
             $resource['type'] = 'uploaded-file-local';
         } else {
             $resource['type'] = 'file-local';
         }
         if (is_readable($resource['file'])) {
             /*
              * Because there is not better  way to determine if resource is an image
              * first, we suppress a warning that would be thrown here otherwise.
              */
             list($width, $height) = @getimagesize($resource['file']);
             $resource = array_merge($resource, array('size' => filesize($resource['file']), 'permission' => substr(sprintf('%o', fileperms($resource['file'])), -4), 'pixels' => $width * $height));
         }
     } elseif (TransferValidation::fileUpload($resource)) {
         $resource = array_merge($defaultResource, pathinfo($resource['name']), array('file' => $resource['name'], 'host' => env('REMOTE_ADDR'), 'size' => $resource['size'], 'mimeType' => $trustClient ? $resource['type'] : null, 'permission' => '0004', 'type' => 'file-upload-remote'));
     } else {
         return null;
     }
     if (!isset($resource['filename'])) {
         /* PHP < 5.2.0 */
         $length = isset($resource['extension']) ? strlen($resource['extension']) + 1 : 0;
         $resource['filename'] = substr($resource['basename'], 0, -$length);
     }
     if (is_null($what)) {
         return $resource;
     } elseif (array_key_exists($what, $resource)) {
         return $resource[$what];
     }
     return null;
 }
Esempio n. 4
0
 /**
  * Retrieve (cached) metadata of a file
  *
  * @param Model $Model
  * @param string $file An absolute path to a file
  * @param integer $level level of amount of info to add, `0` disable, `1` for basic, `2` for detailed info
  * @return mixed Array with results or false if file is not readable
  */
 function metadata(&$Model, $file, $level = 1)
 {
     if ($level < 1) {
         return array();
     }
     extract($this->settings[$Model->alias]);
     $File = new File($file);
     if (!$File->readable()) {
         return false;
     }
     $checksum = $File->md5(true);
     if (isset($this->__cached[$Model->alias][$checksum])) {
         $data = $this->__cached[$Model->alias][$checksum];
     }
     if ($level > 0 && !isset($data[1])) {
         $data[1] = array('size' => $File->size(), 'mime_type' => MimeType::guessType($File->pwd()), 'checksum' => $checksum);
     }
     if ($level > 1 && !isset($data[2])) {
         $Media = Media::factory($File->pwd());
         if ($Media->name === 'Audio') {
             $data[2] = array('artist' => $Media->artist(), 'album' => $Media->album(), 'title' => $Media->title(), 'track' => $Media->track(), 'year' => $Media->year(), 'length' => $Media->duration(), 'quality' => $Media->quality(), 'sampling_rate' => $Media->samplingRate(), 'bit_rate' => $Media->bitRate());
         } elseif ($Media->name === 'Image') {
             $data[2] = array('width' => $Media->width(), 'height' => $Media->height(), 'ratio' => $Media->ratio(), 'quality' => $Media->quality(), 'megapixel' => $Media->megapixel());
         } elseif ($Media->name === 'Text') {
             $data[2] = array('characters' => $Media->characters(), 'syllables' => $Media->syllables(), 'sentences' => $Media->sentences(), 'words' => $Media->words(), 'flesch_score' => $Media->fleschScore(), 'lexical_density' => $Media->lexicalDensity());
         } elseif ($Media->name === 'Video') {
             $data[2] = array('title' => $Media->title(), 'year' => $Media->year(), 'length' => $Media->duration(), 'width' => $Media->width(), 'height' => $Media->height(), 'ratio' => $Media->ratio(), 'quality' => $Media->quality(), 'bit_rate' => $Media->bitRate());
         } else {
             $data[2] = array();
         }
     }
     for ($i = $level, $result = array(); $i > 0; $i--) {
         $result = array_merge($result, $data[$i]);
     }
     $this->__cached[$Model->alias][$checksum] = $data;
     return Set::filter($result);
 }
Esempio n. 5
0
 /**
  * Get MIME type for a path
  *
  * @param string|array $path Absolute or partial path to a file
  * @return string|boolean
  */
 function mimeType($path)
 {
     if ($file = $this->file($path)) {
         return MimeType::guessType($file);
     }
     return false;
 }
Esempio n. 6
0
 function testGuessTypeParanoid()
 {
     $this->skipUnless(extension_loaded('fileinfo'), '%s. Fileinfo extension not loaded.');
     MimeType::config('magic', array('engine' => 'fileinfo'));
     MimeType::config('glob', array('engine' => 'core', 'file' => dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'vendors' . DS . 'mime_glob.db'));
     $file = $this->TestData->getFile(array('image-png.png' => TMP . 'image-png.jpg'));
     $this->assertEqual(MimeType::guessType($file, array('paranoid' => true)), 'image/png');
     $this->assertEqual(MimeType::guessType($file, array('paranoid' => false)), 'image/jpeg');
 }
Esempio n. 7
0
 function testTransitions()
 {
     $Media = new DocumentMedia($this->TestData->getFile('application-pdf.pdf'));
     $Media->Adapters->detach(array_diff($Media->adapters, array('ImagickShell')));
     $Media = $Media->convert('image/png');
     $Media->Adapters->detach(array_diff($Media->adapters, array('ImagickShell')));
     $this->assertIsA($Media, 'ImageMedia');
     $tmpFile = $Media->store(TMP . uniqid('test_suite_'));
     $this->assertEqual(MimeType::guessType($tmpFile), 'image/png');
     unlink($tmpFile);
     $Media = new DocumentMedia($this->TestData->getFile('application-pdf.pdf'));
     $Media->Adapters->detach(array_diff($Media->adapters, array('ImagickShell')));
     $Media = $Media->convert('image/png');
     $Media->Adapters->detach(array_diff($Media->adapters, array('ImagickShell')));
     $result = $Media->fit(10, 10);
     $this->assertTrue($result);
     $this->assertTrue($Media->width() <= 10);
     $this->assertTrue($Media->height() <= 10);
 }
Esempio n. 8
0
 /**
  * Determines media name for a file or MIME type
  *
  * In the case of there are no arguments passed to this method
  * the values of $_mimeTypesToNames are returned
  *
  * @param string $file
  * @param string $mimeType
  * @return mixed
  */
 static function name($file = null, $mimeType = null)
 {
     if ($file === null && $mimeType === null) {
         return array_values(self::$_mimeTypesToNames);
     }
     if ($mimeType === null) {
         $mimeType = MimeType::guessType($file);
     }
     $mimeType = MimeType::simplify($mimeType);
     foreach (self::$_mimeTypesToNames as $mapMimeType => $name) {
         if (strpos($mimeType, $mapMimeType) !== false) {
             return $name;
         }
     }
     return 'Generic';
 }
Esempio n. 9
0
 /**
  * Generates markup to link to file
  *
  * @param string $path Absolute or partial path to a file
  * @param array $options
  * @return mixed
  * @deprecated
  */
 function link($path, $options = array())
 {
     $message = "MediaHelper::__construct - ";
     $message .= "All functionality related to assets has been deprecated.";
     trigger_error($message, E_USER_NOTICE);
     $default = array('inline' => true, 'restrict' => array());
     $defaultRss = array('title' => 'RSS Feed');
     if (is_bool($options)) {
         $options = array('inline' => $options);
     }
     $options = array_merge($default, $options);
     if (is_array($path) && !array_key_exists('controller', $path)) {
         $out = null;
         foreach ($path as $i) {
             $out .= $this->link($i, $options);
         }
         if (empty($out)) {
             return null;
         }
         return $out;
     }
     $inline = $options['inline'];
     unset($options['inline']);
     if (!($url = $this->url($path))) {
         return null;
     }
     if (strpos('://', $path) !== false) {
         $file = parse_url($url, PHP_URL_PATH);
     } else {
         $file = $this->file($path);
     }
     $mimeType = MimeType::guessType($file);
     $Media = Media::factory($file, $mimeType);
     if (!empty($options['restrict']) && !in_array(strtolower($Media->name), (array) $options['restrict'])) {
         return null;
     }
     unset($options['restrict']);
     switch ($mimeType) {
         case 'text/css':
             $out = sprintf($this->tags['csslink'], $url, $this->_parseAttributes($options, null, '', ' '));
             return $this->output($out, $inline);
         case 'application/javascript':
         case 'application/x-javascript':
             $out = sprintf($this->tags['javascriptlink'], $url);
             return $this->output($out, $inline);
         case 'application/rss+xml':
             $options = array_merge($defaultRss, $options);
             $out = sprintf($this->tags['rsslink'], $url, $options['title']);
             return $this->output($out, $inline);
         default:
             return $this->Html->link(basename($file), $url);
     }
 }