コード例 #1
0
 /**
  * Transform the source file.
  *
  * @param \CipeMotion\Medialibrary\Entities\File $file
  *
  * @return \CipeMotion\Medialibrary\Entities\Transformation
  */
 public function transform(File $file)
 {
     // Get a temp path to work with
     $destination = get_temp_path();
     // Get a Image instance from the file
     $image = Image::make($file->getLocalPath($destination));
     // Resize either with the fit strategy or just force the resize to the size
     if (array_get($this->config, 'fit', false)) {
         $image->fit(array_get($this->config, 'size.w', null), array_get($this->config, 'size.h', null), function (Constraint $constraint) {
             if (!array_get($this->config, 'upsize', true)) {
                 $constraint->upsize();
             }
         });
     } else {
         $image->resize(array_get($this->config, 'size.w', null), array_get($this->config, 'size.h', null), function (Constraint $constraint) {
             if (array_get($this->config, 'aspect', true)) {
                 $constraint->aspectRatio();
             }
             if (!array_get($this->config, 'upsize', true)) {
                 $constraint->upsize();
             }
         });
     }
     // Save the image to the temp path
     $image->save($destination);
     // Setup the transformation properties
     $transformation = new Transformation();
     $transformation->name = $this->name;
     $transformation->type = $file->type;
     $transformation->size = Filesystem::size($destination);
     $transformation->width = $image->width();
     $transformation->height = $image->height();
     $transformation->mime_type = $file->mime_type;
     $transformation->extension = $file->extension;
     $transformation->completed = true;
     // Get the disk and a stream from the cropped image location
     $disk = Storage::disk($file->disk);
     $stream = fopen($destination, 'r+');
     // Either overwrite the original uploaded file or write to the transformation path
     if (array_get($this->config, 'default', false)) {
         $disk->put("{$file->id}/upload.{$transformation->extension}", $stream);
     } else {
         $disk->put("{$file->id}/{$transformation->name}.{$transformation->extension}", $stream);
     }
     // Close the stream again
     if (is_resource($stream)) {
         fclose($stream);
     }
     // Return the transformation
     return $transformation;
 }
コード例 #2
0
 public function created(File $file)
 {
     $groupTransformations = $file->getGroupTransformations();
     if (count($groupTransformations)) {
         foreach ($groupTransformations as $name => $transformer) {
             $queue = array_get($transformer, 'queued');
             if ($queue === false) {
                 $job = new TransformFileUnqueuedJob($file, $name, array_get($transformer, 'transformer'), array_get($transformer, 'config', []));
             } else {
                 $job = new TransformFileQueuedJob($file, $name, array_get($transformer, 'transformer'), array_get($transformer, 'config', []));
                 if (is_string($queue)) {
                     $job->onQueue($job);
                 }
             }
             $this->dispatch($job);
         }
     }
 }
コード例 #3
0
 /**
  * Execute the job.
  */
 public function handle()
 {
     /** @var \CipeMotion\Medialibrary\Transformers\ITransformer $transformer */
     $transformer = app($this->transformer, [$this->name, $this->config]);
     if ($transformer instanceof ITransformer) {
         $transformation = $transformer->transform($this->file);
         if (array_get($this->config, 'default', false)) {
             $this->file->size = $transformation->raw_size;
             $this->file->width = $transformation->width;
             $this->file->height = $transformation->height;
             $this->file->mime_type = $transformation->mime_type;
             $this->file->extension = $transformation->extension;
             $this->file->save();
         } else {
             $this->file->transformations()->save($transformation);
         }
     } else {
         throw new \Exception('Unknown MediaLibrary transformer.');
     }
 }
コード例 #4
0
ファイル: File.php プロジェクト: cipemotion/medialibrary
 /**
  * File upload helper.
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile $upload
  * @param array                                               $attributes
  * @param string|null                                         $disk
  *
  * @return bool|\CipeMotion\Medialibrary\Entities\File
  */
 public static function uploadFile(UploadedFile $upload, array $attributes = [], $disk = null)
 {
     // Start our journey with a fresh file Eloquent model
     $file = new File();
     // Retrieve the disk from the config unless it's given to us
     $disk = is_null($disk) ? config('medialibrary.disk') : $disk;
     /** @var \Illuminate\Database\Eloquent\Model $owner */
     $owner = call_user_func(config('medialibrary.relations.owner.resolver'));
     // Generate a file id and resolve the owner
     $file->id = Uuid::uuid4()->toString();
     $file->owner_id = $owner->getKey();
     // Check if we need to resolve a user to attach this file to
     if (!is_null(config('medialibrary.relations.user.model'))) {
         /** @var \Illuminate\Database\Eloquent\Model $user */
         $user = call_user_func(config('medialibrary.relations.user.resolver'));
         // Attach the user
         $file->user_id = $user->getKey();
     }
     // If a group is given, set it
     if (!empty(array_has($attributes, 'group'))) {
         $file->group = array_get($attributes, 'group');
     }
     // If a category is given, set it
     if (array_get($attributes, 'category', 0) > 0) {
         $file->category_id = array_get($attributes, 'category');
     }
     // If a filename is set use that, otherwise build a filename based on the original name
     if (!empty($name = array_get($attributes, 'name'))) {
         $file->name = $name;
     } else {
         $file->name = str_replace(".{$upload->getClientOriginalExtension()}", '', $upload->getClientOriginalName());
     }
     // If a caption was set include it
     if (!empty($caption = array_get($attributes, 'caption'))) {
         $file->caption = $caption;
     }
     // Extract the type from the mime of the file
     $type = self::getTypeForMime($upload->getMimeType());
     // If the file is a image we also need to find out how big the image is
     if ($type === FileTypes::TYPE_IMAGE) {
         $image = Image::make($upload);
         $file->width = $image->getWidth();
         $file->height = $image->getHeight();
     } else {
         $file->width = null;
         $file->height = null;
     }
     // Collect all the metadata we are going to save with the file entry in the database
     $file->type = $type;
     $file->disk = $disk;
     $file->filename = (string) Stringy::create($upload->getClientOriginalName())->toAscii()->trim()->toLowerCase()->slugify();
     $file->extension = strtolower($upload->getClientOriginalExtension());
     $file->mime_type = $upload->getMimeType();
     $file->size = $upload->getSize();
     $file->is_hidden = array_get($attributes, 'is_hidden', false);
     $file->completed = true;
     // Get a resource handle on the file so we can stream it to our disk
     $stream = fopen($upload->getRealPath(), 'r+');
     // Use Laravel' storage engine to store our file on a disk
     $success = Storage::disk($disk)->put("{$file->id}/upload.{$file->extension}", $stream);
     // Close the resource handle if we need to
     if (is_resource($stream)) {
         fclose($stream);
     }
     // Check if we succeeded
     if ($success) {
         $file->setLocalPath($upload->getRealPath());
         $file->save();
         return $file;
     }
     // Something went wrong and the file is not uploaded
     return false;
 }
コード例 #5
0
 /**
  * Transform the source file.
  *
  * @param \CipeMotion\Medialibrary\Entities\File $file
  *
  * @return \CipeMotion\Medialibrary\Entities\Transformation
  */
 public function transform(File $file)
 {
     $extension = array_get($this->config, 'extension', 'jpg');
     $cloudconvertSettings = ['inputformat' => $file->extension, 'outputformat' => $extension, 'input' => 'download', 'wait' => true, 'file' => $file->downloadUrl, 'converteroptions' => ['page_range' => '1-1']];
     if (!is_null(config('services.cloudconvert.timeout'))) {
         $cloudconvertSettings['timeout'] = config('services.cloudconvert.timeout');
     }
     // Run the conversion
     $convert = $this->api->convert($cloudconvertSettings)->wait();
     // Get a temp path
     $destination = get_temp_path();
     // Download the converted video file
     copy('https:' . $convert->output->url, $destination);
     // We got it all, cleanup!
     $convert->delete();
     // Get the disk and a stream from the cropped image location
     $disk = Storage::disk($file->disk);
     $stream = fopen($destination, 'r+');
     // Upload the preview
     $disk->put("{$file->id}/preview.{$extension}", $stream);
     // Cleanup our streams
     if (is_resource($stream)) {
         fclose($stream);
     }
     // Create a Image
     $image = Image::make($destination);
     // Build the transformation
     $preview = new Transformation();
     $preview->name = 'preview';
     $preview->size = Filesystem::size($destination);
     $preview->mime_type = $image->mime();
     $preview->type = File::getTypeForMime($preview->mime_type);
     $preview->width = $image->width();
     $preview->height = $image->height();
     $preview->extension = $extension;
     $preview->completed = true;
     // Store the preview
     $file->transformations()->save($preview);
     if (array_get($this->config, 'fit', false)) {
         $image->fit(array_get($this->config, 'size.w', null), array_get($this->config, 'size.h', null), function ($constraint) {
             if (!array_get($this->config, 'upsize', true)) {
                 $constraint->upsize();
             }
         }, 'top');
     } else {
         $image->resize(array_get($this->config, 'size.w', null), array_get($this->config, 'size.h', null), function ($constraint) {
             if (array_get($this->config, 'aspect', true)) {
                 $constraint->aspectRatio();
             }
             if (!array_get($this->config, 'upsize', true)) {
                 $constraint->upsize();
             }
         });
     }
     // Stora a cropped version
     $image->save($destination);
     // Build the transformation
     $transformation = new Transformation();
     $transformation->name = 'thumb';
     $transformation->type = $preview->type;
     $transformation->size = Filesystem::size($destination);
     $transformation->width = $image->width();
     $transformation->height = $image->height();
     $transformation->mime_type = $preview->mime_type;
     $transformation->extension = $preview->extension;
     $transformation->completed = true;
     // Get the disk and a stream from the cropped image location
     $stream = fopen($destination, 'r+');
     // Upload the preview
     $disk->put("{$file->id}/{$transformation->name}.{$transformation->extension}", $stream);
     // Cleanup our streams
     if (is_resource($stream)) {
         fclose($stream);
     }
     return $transformation;
 }
コード例 #6
0
 /**
  * Generate a thumb and retrieve info about the video file.
  *
  * @param \CipeMotion\Medialibrary\Entities\File $file
  *
  * @return array|\CipeMotion\Medialibrary\Entities\Transformation|\CloudConvert\Process
  */
 private function generateThumbAndRetrieveFileInfo(File $file)
 {
     // Extract config options
     $previewWidth = array_get($this->config, 'preview.size.w', 1280);
     // Build up the base settings
     $cloudconvertSettings = ['mode' => 'info', 'wait' => true, 'input' => 'download', 'file' => $file->downloadUrl, 'converteroptions' => ['thumbnail_format' => 'jpg', 'thumbnail_size' => "{$previewWidth}x"]];
     // Respect the cloudconvert timeout
     if (!empty(config('services.cloudconvert.timeout'))) {
         $cloudconvertSettings['timeout'] = config('services.cloudconvert.timeout');
     }
     // Execute our info request and wait for the output
     $info = $this->api->convert($cloudconvertSettings)->wait();
     // Generate a temp path
     $destination = get_temp_path();
     // Copy the thumb to our temp path
     copy('https:' . $info->output->url, $destination);
     // Get the disk and a stream from the cropped image location
     $disk = Storage::disk($file->disk);
     $stream = fopen($destination, 'r+');
     // Store the preview image
     $disk->put("{$file->id}/preview.jpg", $stream);
     // Cleanup our streams
     if (is_resource($stream)) {
         fclose($stream);
     }
     // Create a image instance so we can detect the width and height
     $image = Image::make($destination);
     // Build the preview
     $preview = new Transformation();
     $preview->name = 'preview';
     $preview->size = Filesystem::size($destination);
     $preview->mime_type = $image->mime();
     $preview->type = File::getTypeForMime($preview->mime_type);
     $preview->width = $image->width();
     $preview->height = $image->height();
     $preview->extension = 'jpg';
     $preview->completed = true;
     // Resize the preview for a thumb
     if (array_get($this->config, 'thumb.fit', false)) {
         $image->fit(array_get($this->config, 'thumb.size.w', null), array_get($this->config, 'thumb.size.h', null), function ($constraint) {
             if (!array_get($this->config, 'thumb.upsize', true)) {
                 $constraint->upsize();
             }
         }, 'top');
     } else {
         $image->resize(array_get($this->config, 'thumb.size.w', null), array_get($this->config, 'thumb.size.h', null), function ($constraint) {
             if (array_get($this->config, 'thumb.aspect', true)) {
                 $constraint->aspectRatio();
             }
             if (!array_get($this->config, 'thumb.upsize', true)) {
                 $constraint->upsize();
             }
         });
     }
     // Store the thumb version
     $image->save($destination);
     // Build the thumb transformation
     $thumb = new Transformation();
     $thumb->name = 'thumb';
     $thumb->size = Filesystem::size($destination);
     $thumb->mime_type = $image->mime();
     $thumb->type = File::getTypeForMime($thumb->mime_type);
     $thumb->width = $image->width();
     $thumb->height = $image->height();
     $thumb->extension = 'jpg';
     $thumb->completed = true;
     // Get the disk and a stream from the cropped image location
     $stream = fopen($destination, 'r+');
     // Upload the preview
     $disk->put("{$file->id}/{$thumb->name}.{$thumb->extension}", $stream);
     // Cleanup our streams
     if (is_resource($stream)) {
         fclose($stream);
     }
     return [$preview, $thumb, $info];
 }
コード例 #7
0
 /**
  * Attach model observers.
  */
 protected function attachObservers()
 {
     File::observe(new FileObserver());
 }