예제 #1
0
 /**
  * Gets an image by id sized and rotated based on the passed params. If the file
  * doesn't exist, it is created using the imager class and stored in cache dir. Every 
  * call after that will load the file directly.
  *
  * When sizing an image with the same width and height, it will be scaled down to the closest
  * width/height and then cropped.
  *
  * To size an image to a specific width, set the width value needed and the height value to 0. 
  * This ensures the specified width is met, and the height is adjusted proportionally. The same
  * can be done by setting the height needed and the width to 0.
  *
  * @param $id int The id of image to get
  * @param $rotation int The degrees to rotate the image, can be null
  * @param $widthOrPercent int When by itself, represents percent to increase/decrease size.
  *      When used with the height param, represents the image width.
  * @param $height int The height to size the image.
  *
  * @return string The relative path from ROOT to the file, if it exists. Boolean false otherwise.
  */
 public function render($id, $rotation = null, $widthOrPercent = null, $height = null)
 {
     try {
         if ($image = Media::m('file')->find($id)) {
             $ext = pathinfo($image->name, PATHINFO_EXTENSION);
             $cachedFilename = str_replace('.' . $ext, sprintf('_%d.%s', $id . $rotation . $widthOrPercent . $height, $ext), $image->name);
             $cachedFullPath = ROOT . Media::getFilesPath() . Media::getCachePath() . $cachedFilename;
             if (!file_exists($cachedFullPath)) {
                 $origFullPath = ROOT . Media::getFilesPath() . $image->path . $image->name;
                 Media_Imager::open($origFullPath);
                 if (!is_null($widthOrPercent) && is_null($height)) {
                     Media_Imager::percent($widthOrPercent);
                 } elseif (!is_null($widthOrPercent) && !is_null($height)) {
                     $adaptive = $widthOrPercent == 0 || $height == 0 ? false : true;
                     Media_Imager::resize($widthOrPercent, $height, $adaptive);
                 }
                 if (!is_null($rotation) && $rotation > 0) {
                     Media_Imager::rotate($rotation);
                 }
                 Media_Imager::save($cachedFullPath);
             }
             return Media::getFilesPath() . Media::getCachePath() . $cachedFilename;
         }
     } catch (Exception $e) {
         Log::error('media', $e->getMessage());
     }
     return false;
 }
예제 #2
0
 /**
  * TODO Comments.
  */
 public static function render($id, $rotation = null, $widthOrPercent = null, $height = null)
 {
     $image = Media::m('file')->find($id);
     $path = Media::image()->render($id, $rotation, $widthOrPercent, $height);
     header('Content-Type: ' . $image->mime_type);
     readfile(ROOT . $path);
     die;
 }
예제 #3
0
 /**
  * Returns the relative path from ROOT to the file.
  */
 public function getPath($id)
 {
     if ($file = Media::m('file')->find($id)) {
         $path = Media::getFilesPath() . $file->path . $file->name;
         if (file_exists($path)) {
             return $path;
         }
     }
     return false;
 }
예제 #4
0
 /**
  * TODO Comments.
  */
 public function embed($id, $width = 560, $height = 345)
 {
     if ($video = Media::m('url')->find($id)) {
         $data = unserialize($video->data);
         if (stristr($video->url, 'youtube')) {
             return sprintf('<iframe width="%d" height="%d" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $width, $height, $data['id']);
         }
         if (stristr($video->url, 'vimeo')) {
             return sprintf('<iframe src="http://player.vimeo.com/video/%s?title=0&amp;byline=0&amp;portrait=0" width="%d" height="%d" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>', $data['id'], $width, $height);
         }
     }
 }
예제 #5
0
 /**
  * Deletes the file associated with the given id from the system.
  *
  * @param $id int The id of the file to delete.
  * @return boolean
  */
 public static function delete($id)
 {
     if ($file = Media::m('file')->find($id)) {
         $filePath = ROOT . self::getFilesPath() . $file->path . $file->name;
         if (file_exists($filePath)) {
             @unlink($filePath);
         }
         self::clearCache($file);
         if (Media::m('file')->delete($id)) {
             return true;
         }
     }
     return false;
 }
예제 #6
0
파일: file.php 프로젝트: simudream/caffeine
 /**
  * Forces a download of the file based on the given id. If the file doesn't
  * exist, 404 is shown.
  */
 public static function download($id)
 {
     $file = Media::m('file')->find($id);
     if ($file) {
         $filePath = Media::getFilesPath() . $file->path . $file->name;
         header('Content-Type: application/octet-stream');
         header('Content-Disposition: attachment; filename=' . $file->name);
         header('Content-Transfer-Encoding: binary');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Pragma: public');
         header('Content-Length: ' . $file->size);
         ob_clean();
         flush();
         readfile($file_path);
         exit;
     }
     return ERROR_404;
 }
예제 #7
0
 /**
  * Saves a file from a url.
  *
  * @param string $url The full url for the file to save.
  * @param string $type The file type to store in the db (file, image, video)
  *
  * @return Returns the media id if created successfully, boolean false otherwise.
  */
 public static function saveFromUrl($url, $type)
 {
     if (!($nameAndPath = self::_getNewNameAndPath(basename($url)))) {
         return false;
     }
     list($newFilename, $fullPath) = $nameAndPath;
     if ($urlContent = file_get_contents($url)) {
         if (file_put_contents($fullPath, $urlContent)) {
             $id = Media::m('file')->insert(array('name' => $newFilename, 'path' => Media::getMediaPath(), 'size' => filesize($fullPath), 'mime_type' => self::_determineMimeType($fullPath), 'file_type' => $type));
             return $id;
         } else {
             self::$_error = 'Unable to save ' . $type . ' from URL.';
         }
     } else {
         self::$_error = 'Unable to read file contents from URL.';
         Dev::debug('media', 'ERROR: Cant save from url: ' . $url);
     }
     return false;
 }