Advertisement
benshepherd

PictureTrait.php

Jul 25th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.33 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. use Imagine\Image\Box;
  5. use Imagine\Image\ImageInterface;
  6. use Orchestra\Imagine\Facade;
  7.  
  8.  
  9. trait PictureTrait {
  10.  
  11.     /**
  12.      * Creates the thumbnail
  13.      * @param $path | folder picture is located in
  14.      * @param $filename | name of the file
  15.      * @param $extension | extension of the thumb
  16.      * @return mixed
  17.      */
  18.     function create_thumbnail($path, $filename, $fileExtension, $thumbExtension)
  19.     {
  20.         $width  = 320;
  21.         $height = 320;
  22.         $mode   = ImageInterface::THUMBNAIL_OUTBOUND;
  23.         $size   = new Box($width, $height);
  24.  
  25.         $path = FileHelper::removeEndSlash($path);
  26.         $filePath = "{$path}/{$filename}".(!empty($fileExtension) ? ".$fileExtension" : '');
  27.         $thumbnail   = Imagine::open($filePath)->thumbnail($size, $mode);
  28.         $destination = "{$filename}.thumb.{$thumbExtension}";
  29.  
  30.         $thumbnail->save("{$path}/{$destination}");
  31.  
  32.         return str_replace(public_path(), '', $path."/".$destination);
  33.     }
  34.  
  35.     /**
  36.      * Creates a thumbnail from a picture
  37.      * @param string $sourceColumn | the column in the database to get the picture from
  38.      * @param string $destColumn | the column that the thumbnail belongs to
  39.      */
  40.     function createThumbnail($sourceColumn = 'picture', $destColumn = 'picture_thumb')
  41.     {
  42.         $upload_dir = Config::get('app.upload_directory');
  43.         $thumb_path = $this->create_thumbnail(
  44.             public_path().$upload_dir,
  45.             basename($this->$sourceColumn),
  46.             '',
  47.             'jpg'
  48.         );
  49.  
  50.         $this->$destColumn = $thumb_path;
  51.         $this->save();
  52.     }
  53.  
  54.     /**
  55.      * Checks if we have a thumbnail
  56.      * @param string $column_name
  57.      * @return bool
  58.      */
  59.     public function hasThumbnail($column_name = 'picture_thumb')
  60.     {
  61.         return $this->hasPicture($column_name);
  62.     }
  63.  
  64.     /**
  65.      * Returns path to thumbnail
  66.      * @param string $column_name | thumbnail column
  67.      * @param string $other_column | other column if the first has no thumbnail
  68.      * @return string
  69.      */
  70.     public function getThumbnail($column_name = 'picture_thumb', $other_column = 'picture')
  71.     {
  72.         if($this->hasThumbnail($column_name)) {
  73.             return $this->getPicture($column_name);
  74.         }
  75.         else {
  76.             return $this->getPicture($other_column);
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Gets a picture path
  82.      * @param string $column_name
  83.      * @param string $defaultValue
  84.      * @return string
  85.      */
  86.     function getPicture($column_name = 'picture', $defaultValue = '')
  87.     {
  88.         if (isset($this->type) && isset($this->video_url) && $this->type == 'video') {
  89.  
  90.             if(Helper::isYouTubeURL($this->video_url)) {
  91.                 return "http://img.youtube.com/vi/" . Helper::getYouTubeVideoID($this->video_url) . "/maxresdefault.jpg";
  92.             }
  93.             else if(Helper::isVimeoURL($this->video_url)) {
  94.  
  95.                 if(empty($this->picture)) {
  96.                     $thumbnailUrl = Helper::getVimeoThumbnail($this->video_url);
  97.                     $this->picture = $thumbnailUrl;
  98.                     $this->save();
  99.                 }
  100.  
  101.                 return URL::asset($this->picture);
  102.             }
  103.         }
  104.  
  105.         if($this->hasPicture($column_name)) {
  106.             return URL::asset($this->$column_name);
  107.         }
  108.         else {
  109.             return $defaultValue;
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Checks if this column has a picture
  115.      * @param string $column_name
  116.      * @return bool
  117.      */
  118.     function hasPicture($column_name = 'picture')
  119.     {
  120.         return !empty($this->$column_name);
  121.     }
  122.  
  123.     /**
  124.      * Uploads a picture
  125.      * @param string $name | Input name
  126.      * @param string $columnName | Column name
  127.      * @param bool $returnAbsoluteUrl
  128.      */
  129.     function uploadPicture($name = 'picture', $columnName = 'picture', $returnAbsoluteUrl = false)
  130.     {
  131.         if(Input::hasFile($name))
  132.         {
  133.             $this->$columnName = FileHelper::upload(Input::file($name), $returnAbsoluteUrl);
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * Uploads a picture from a URL
  139.      * @param $url
  140.      * @param string $columnName
  141.      */
  142.     function uploadPictureFromURL($url, $columnName = 'picture')
  143.     {
  144.         $details = FileHelper::downloadImageFromURL($url);
  145.         $this->$columnName = $details->src;
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement