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