Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Imagine\Image\Box;
- use Imagine\Image\ImageInterface;
- use Orchestra\Imagine\Facade;
- trait PictureTrait {
- /**
- * Creates the thumbnail
- * @param $path | folder picture is located in
- * @param $filename | name of the file
- * @param $extension | extension of the thumb
- * @return mixed
- */
- function create_thumbnail($path, $filename, $fileExtension, $thumbExtension)
- {
- $width = 320;
- $height = 320;
- $mode = ImageInterface::THUMBNAIL_OUTBOUND;
- $size = new Box($width, $height);
- $path = FileHelper::removeEndSlash($path);
- $filePath = "{$path}/{$filename}".(!empty($fileExtension) ? ".$fileExtension" : '');
- $thumbnail = Imagine::open($filePath)->thumbnail($size, $mode);
- $destination = "{$filename}.thumb.{$thumbExtension}";
- $thumbnail->save("{$path}/{$destination}");
- return str_replace(public_path(), '', $path."/".$destination);
- }
- /**
- * Creates a thumbnail from a picture
- * @param string $sourceColumn | the column in the database to get the picture from
- * @param string $destColumn | the column that the thumbnail belongs to
- */
- function createThumbnail($sourceColumn = 'picture', $destColumn = 'picture_thumb')
- {
- $upload_dir = Config::get('app.upload_directory');
- $thumb_path = $this->create_thumbnail(
- public_path().$upload_dir,
- basename($this->$sourceColumn),
- '',
- 'jpg'
- );
- $this->$destColumn = $thumb_path;
- $this->save();
- }
- /**
- * Checks if we have a thumbnail
- * @param string $column_name
- * @return bool
- */
- public function hasThumbnail($column_name = 'picture_thumb')
- {
- return $this->hasPicture($column_name);
- }
- /**
- * Returns path to thumbnail
- * @param string $column_name | thumbnail column
- * @param string $other_column | other column if the first has no thumbnail
- * @return string
- */
- public function getThumbnail($column_name = 'picture_thumb', $other_column = 'picture')
- {
- if($this->hasThumbnail($column_name)) {
- return $this->getPicture($column_name);
- }
- else {
- return $this->getPicture($other_column);
- }
- }
- /**
- * Gets a picture path
- * @param string $column_name
- * @param string $defaultValue
- * @return string
- */
- function getPicture($column_name = 'picture', $defaultValue = '')
- {
- if (isset($this->type) && isset($this->video_url) && $this->type == 'video') {
- if(Helper::isYouTubeURL($this->video_url)) {
- return "http://img.youtube.com/vi/" . Helper::getYouTubeVideoID($this->video_url) . "/maxresdefault.jpg";
- }
- else if(Helper::isVimeoURL($this->video_url)) {
- if(empty($this->picture)) {
- $thumbnailUrl = Helper::getVimeoThumbnail($this->video_url);
- $this->picture = $thumbnailUrl;
- $this->save();
- }
- return URL::asset($this->picture);
- }
- }
- if($this->hasPicture($column_name)) {
- return URL::asset($this->$column_name);
- }
- else {
- return $defaultValue;
- }
- }
- /**
- * Checks if this column has a picture
- * @param string $column_name
- * @return bool
- */
- function hasPicture($column_name = 'picture')
- {
- return !empty($this->$column_name);
- }
- /**
- * Uploads a picture
- * @param string $name | Input name
- * @param string $columnName | Column name
- * @param bool $returnAbsoluteUrl
- */
- function uploadPicture($name = 'picture', $columnName = 'picture', $returnAbsoluteUrl = false)
- {
- if(Input::hasFile($name))
- {
- $this->$columnName = FileHelper::upload(Input::file($name), $returnAbsoluteUrl);
- }
- }
- /**
- * Uploads a picture from a URL
- * @param $url
- * @param string $columnName
- */
- function uploadPictureFromURL($url, $columnName = 'picture')
- {
- $details = FileHelper::downloadImageFromURL($url);
- $this->$columnName = $details->src;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement