Пример #1
0
 function convert(&$Medium, $mimeType)
 {
     if (Medium::name(null, $mimeType) === 'Image') {
         $randomFrame = rand(1, $Medium->objects['ffmpeg_movie']->getFrameCount() - 1);
         $resource = $Medium->objects['ffmpeg_movie']->getFrame($randomFrame)->toGDImage();
         if (!is_resource($resource)) {
             return false;
         }
         $Image = Medium::factory(array('gd' => $resource), 'image/gd');
         return $Image->convert($mimeType);
     }
     return false;
 }
Пример #2
0
 function convert(&$Medium, $mimeType)
 {
     if (!isset($this->_formatMap[$mimeType])) {
         return false;
     }
     $Medium->mimeType = $mimeType;
     if ($Medium->name === 'Document') {
         // application/pdf -> image
         $this->store($Medium, $Medium->files['temporary']);
         /* Unset files to prevent too early deletion by $Medium */
         $temporary = $Medium->files['temporary'];
         unset($Medium->files);
         return Medium::factory(array('temporary' => $temporary), $mimeType);
     }
     return true;
 }
 function convert($Medium, $mimeType)
 {
     if (!isset($this->_formatMap[$mimeType])) {
         return false;
     }
     try {
         $Medium->objects['Imagick']->setFormat($this->_formatMap[$mimeType]);
     } catch (Exception $E) {
         return false;
     }
     $Medium->mimeType = $mimeType;
     if ($Medium->name === 'Document') {
         // application/pdf -> image
         return Medium::factory($Medium->objects['Imagick'], $mimeType);
     }
     return true;
 }
Пример #4
0
 /**
  * Generates markup to link to file
  *
  * @param string $path Absolute or partial path to a file
  * @param array $options
  * @return mixed
  */
 function link($path, $options = array())
 {
     $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);
     $Medium = Medium::factory($file, $mimeType);
     if (!empty($options['restrict']) && !in_array(strtolower($Medium->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);
     }
 }
Пример #5
0
 /**
  * Retrieve (cached) metadata of a file
  *
  * @param Model $Model
  * @param string $file Path to a file relative to `baseDirectory` or 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]);
     list($file, ) = $this->_file($Model, $file);
     $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])) {
         $Medium = Medium::factory($File->pwd());
         if ($Medium->name === 'Audio') {
             $data[2] = array('artist' => $Medium->artist(), 'album' => $Medium->album(), 'title' => $Medium->title(), 'track' => $Medium->track(), 'year' => $Medium->year(), 'length' => $Medium->duration(), 'quality' => $Medium->quality(), 'sampling_rate' => $Medium->samplingRate(), 'bit_rate' => $Medium->bitRate());
         } elseif ($Medium->name === 'Image') {
             $data[2] = array('width' => $Medium->width(), 'height' => $Medium->height(), 'ratio' => $Medium->ratio(), 'quality' => $Medium->quality(), 'megapixel' => $Medium->megapixel());
         } elseif ($Medium->name === 'Text') {
             $data[2] = array('characters' => $Medium->characters(), 'syllables' => $Medium->syllables(), 'sentences' => $Medium->sentences(), 'words' => $Medium->words(), 'flesch_score' => $Medium->fleschScore(), 'lexical_density' => $Medium->lexicalDensity());
         } elseif ($Medium->name === 'Video') {
             $data[2] = array('title' => $Medium->title(), 'year' => $Medium->year(), 'length' => $Medium->duration(), 'width' => $Medium->width(), 'height' => $Medium->height(), 'ratio' => $Medium->ratio(), 'quality' => $Medium->quality(), 'bit_rate' => $Medium->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);
 }
 function convert($Medium, $mimeType)
 {
     if (Medium::name(null, $mimeType) === 'Image') {
         $coverArt = $this->__coverArt($Medium);
         if (!$coverArt) {
             return false;
         }
         $resource = @imagecreatefromstring($coverArt);
         if (!is_resource($resource)) {
             return false;
         }
         $Image = Medium::factory(array('gd' => $resource), 'image/gd');
         return $Image->convert($mimeType);
     }
     return false;
 }
 function testMediumNameAndShort()
 {
     $file = $this->TestData->getFile('image-jpg.jpg');
     $result = Medium::factory($file);
     $this->assertEqual($result->name, 'Image');
     $this->assertEqual($result->short, 'img');
     $file = $this->TestData->getFile('image-png.png');
     $result = Medium::factory($file);
     $this->assertEqual($result->name, 'Image');
     $this->assertEqual($result->short, 'img');
     $file = $this->TestData->getFile('image-gif.gif');
     $result = Medium::factory($file);
     $this->assertEqual($result->name, 'Image');
     $this->assertEqual($result->short, 'img');
     $file = $this->TestData->getFile('text-plain.txt');
     $result = Medium::factory($file);
     $this->assertEqual($result->name, 'Text');
     $this->assertEqual($result->short, 'txt');
     $file = $this->TestData->getFile('application-pdf.pdf');
     $result = Medium::factory($file);
     $this->assertEqual($result->name, 'Document');
     $this->assertEqual($result->short, 'doc');
 }
Пример #8
0
 /**
  * Automatically processes a file and returns a Medium instance
  *
  * Possible values for $instructions:
  * 	array('name of method', 'name of other method')
  *  array('name of method' => array('arg1', 'arg2'))
  *
  * @param string $file Absolute path to a file
  * @param array $instructions See description above
  * @return object
  */
 static function make($file, $instructions = array())
 {
     $Medium = Medium::factory($file);
     foreach ($instructions as $key => $value) {
         if (is_int($key)) {
             $method = $value;
             $args = null;
         } else {
             $method = $key;
             if (is_array($value)) {
                 $args = $value;
             } else {
                 $args = array($value);
             }
         }
         if (!method_exists($Medium, $method)) {
             $message = "Medium::make - Invalid instruction ";
             $message .= "`" . get_class($Medium) . "::{$method}()`.";
             trigger_error($message, E_USER_WARNING);
             return false;
         }
         $result = call_user_func_array(array($Medium, $method), $args);
         if ($result === false) {
             $message = "Medium::make - Instruction ";
             $message .= "`" . get_class($Medium) . "::{$method}()` failed.";
             trigger_error($message, E_USER_WARNING);
             return false;
         } elseif (is_a($result, 'Medium')) {
             $Medium = $result;
         }
     }
     return $Medium;
 }