function convert($Media, $mimeType) { if (Media::name(null, $mimeType) === 'Image') { $randomFrame = rand(1, $Media->objects['ffmpeg_movie']->getFrameCount() - 1); $resource = $Media->objects['ffmpeg_movie']->getFrame($randomFrame)->toGDImage(); if (!is_resource($resource)) { return false; } $Image = Media::factory(array('gd' => $resource), 'image/gd'); return $Image->convert($mimeType); } return false; }
function convert($Media, $mimeType) { if (!isset($this->_formatMap[$mimeType])) { return false; } $Media->mimeType = $mimeType; if ($Media->name === 'Document') { // application/pdf -> image $this->store($Media, $Media->files['temporary']); /* Unset files to prevent too early deletion by $Media */ $temporary = $Media->files['temporary']; unset($Media->files); return Media::factory(array('temporary' => $temporary), $mimeType); } return true; }
function convert($Media, $mimeType) { if (!isset($this->_formatMap[$mimeType])) { return false; } try { $Media->objects['Imagick']->setFormat($this->_formatMap[$mimeType]); } catch (Exception $E) { return false; } $Media->mimeType = $mimeType; if ($Media->name === 'Document') { // application/pdf -> image return Media::factory($Media->objects['Imagick'], $mimeType); } return true; }
function convert($Media, $mimeType) { if (!isset($this->_formatMap[$mimeType])) { return false; } try { $Media->objects['Imagick']->setFormat($this->_formatMap[$mimeType]); } catch (Exception $E) { return false; } if ($Media->name !== Media::name(null, $mimeType)) { // document -> image return Media::factory($Media->objects['Imagick']->clone(), $mimeType); } else { $Media->mimeType = $mimeType; } return true; }
/** * 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); }
function convert($Media, $mimeType) { if (Media::name(null, $mimeType) === 'Image') { $coverArt = $this->__coverArt($Media); if (!$coverArt) { return false; } $resource = @imagecreatefromstring($coverArt); if (!is_resource($resource)) { return false; } $Image = Media::factory(array('gd' => $resource), 'image/gd'); return $Image->convert($mimeType); } return false; }
/** * Automatically processes a file and returns a Media 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()) { $Media = Media::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($Media, $method)) { $message = "Media::make - Invalid instruction "; $message .= "`" . get_class($Media) . "::{$method}()`."; trigger_error($message, E_USER_WARNING); return false; } $result = call_user_func_array(array($Media, $method), $args); if ($result === false) { $message = "Media::make - Instruction "; $message .= "`" . get_class($Media) . "::{$method}()` failed."; trigger_error($message, E_USER_WARNING); return false; } elseif (is_a($result, 'Media')) { $Media = $result; } } return $Media; }
function testMediaNameAndShort() { $file = $this->TestData->getFile('image-jpg.jpg'); $result = Media::factory($file); $this->assertEqual($result->name, 'Image'); $this->assertEqual($result->short, 'img'); $file = $this->TestData->getFile('image-png.png'); $result = Media::factory($file); $this->assertEqual($result->name, 'Image'); $this->assertEqual($result->short, 'img'); $file = $this->TestData->getFile('image-gif.gif'); $result = Media::factory($file); $this->assertEqual($result->name, 'Image'); $this->assertEqual($result->short, 'img'); $file = $this->TestData->getFile('text-plain.txt'); $result = Media::factory($file); $this->assertEqual($result->name, 'Text'); $this->assertEqual($result->short, 'txt'); $file = $this->TestData->getFile('application-pdf.pdf'); $result = Media::factory($file); $this->assertEqual($result->name, 'Document'); $this->assertEqual($result->short, 'doc'); }
/** * serve method * * If the setting useTokens is true, check that the request token matches the url to * prevent a user just typing a url in the addressbar and DOS ing the server. If the request * is not authentic, put a link to the fallback in the webroot so that subsequent requests * do not hit php. * * If the request is for the original file - link or copy it to the webroot * If the request is for a version, after checking the original exists - * Generate it, and put it in the webroot * * can process any of the following types of request, given that uploads/img/example.jpg exists * /img/example.jpg * link, or copy, the original to the request url (webroot/img/example.jpg) * /img/example,small.jpg * see bootstrap/media.php - process the request using the small config * /img/example,99x10.jpg * see bootstrap/media.php - process the request using the 99x10 config * /img/example,100x100.jpg * generate a 100x100 'fit' image * /img/example,fitCrop,50,50.jpg * use fitCrop, passing array(50, 50) as the params * /img/example,zoomCrop,50,50,center.jpg * use zoomCrop, passing array(50, 50, center) as the params * /img/example,someother,1,2,3,foo,bar,zum.jpg * use someother, passing array(1, 2, 3, foo, bar, zum) as the params * etc. * * The request does not have to be for an image, any type of media can be handled the same way * if an adapter method exists to generate the desired version. That said, currently there are * only appropriate methods in the vendors for images * * @return void * @access public */ function serve() { $url = $this->params->url; if ($this->settings['useTokens']) { if (!isset($this->params['url']['token'])) { return $this->_serve(); } $compare = $this->params['url']['token']; App::import('Core', 'Security'); $hash = Security::hash($url, null, true); if (!Configure::read() && $hash !== $compare) { return $this->_serve(); } } $url = str_replace('%2C', ',', $url); if (!strpos($url, ',')) { if ($this->_linkOriginal($url, $this->settings['store'])) { return $this->_serve(); } header("HTTP/1.0 404 Not Found"); return $this->_serve(); } $params = array(); preg_match('@,(.*)\\.@', $url, $matches); $version = $matches[1]; if (isset($this->settings['versions'][$version])) { $params = $this->settings['versions'][$version]; } if (!$params) { if (preg_match('@^(\\d*)x(\\d*)$@', $version, $dims)) { $params = array('fit', $dims[0], $dims[1]); } elseif (strpos($version, ',')) { $params = explode(',', $version); } else { $isImage = false; if ($isImage && !empty($this->settings['versions']['default'])) { $params = $this->settings['versions']['default']; } else { return $this->_serve(); } } } $original = $this->_linkOriginal($url); if (!$original) { header("HTTP/1.0 404 Not Found"); if ($this->settings['autoFallback']) { if (file_exists(WWW_ROOT . 'img' . DS . $version . '.gif')) { $this->_link(WWW_ROOT . 'img' . DS . $version . '.gif'); } else { $dims = array_slice($params, 1, 2); $version = implode($dims, 'x'); if (file_exists(WWW_ROOT . 'img' . DS . $version . '.gif')) { $this->_link(WWW_ROOT . 'img' . DS . $version . '.gif'); } } } $this->_serve(); } App::uses('Media', 'Media.Libs/Media'); $Media = Media::factory($original); $function = array_shift($params); call_user_func_array(array($Media, $function), $params); if ($this->settings['store']) { $Media->store(WWW_ROOT . $url, false, false); } $this->_serve(); }
/** * 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); } }