/**
  * 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;
 }
 /**
  * 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];
 }