src/FileManipulator.php
public function performConversions(ConversionCollection $conversions, Media $media, $onlyIfMissing = false)
    {
        if ($conversions->isEmpty()) {
            return;
        }

        $imageGenerator = $this->determineImageGenerator($media);

        if (! $imageGenerator) {
            return;
        }

        $temporaryDirectory = TemporaryDirectory::create();

        $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary(
            $media,
            $temporaryDirectory->path(str_random(16).'.'.$media->extension)
        );

        $conversions
            ->reject(function (Conversion $conversion) use ($onlyIfMissing, $media) {
                $relativePath = $media->getPath($conversion->getName());

                $rootPath = config('filesystems.disks.'.$media->disk.'.root');

                if ($rootPath) {
                    $relativePath = str_replace($rootPath, '', $relativePath);
                }

                return $onlyIfMissing && Storage::disk($media->disk)->exists($relativePath);
            })
            ->each(function (Conversion $conversion) use ($media, $imageGenerator, $copiedOriginalFile) {
                event(new ConversionWillStart($media, $conversion, $copiedOriginalFile));

                $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion);

                $manipulationResult = $this->performManipulations($media, $conversion, $copiedOriginalFile);

                $newFileName = pathinfo($media->file_name, PATHINFO_FILENAME).
                    '-'.$conversion->getName().
                    '.'.$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION));

                $renamedFile = MediaLibraryFileHelper::renameInDirectory($manipulationResult, $newFileName);

                if ($conversion->shouldGenerateResponsiveImages()) {
                    app(ResponsiveImageGenerator::class)->generateResponsiveImagesForConversion(
                        $media,
                        $conversion,
                        $renamedFile
                    );
                }

                app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, 'conversions');

                $media->markAsConversionGenerated($conversion->getName(), true);

                event(new ConversionHasBeenCompleted($media, $conversion));
            });

        $temporaryDirectory->delete();
    }
public function performConversions(ConversionCollection $conversions, Media $media, $onlyIfMissing = false)
    {
        if ($conversions->isEmpty()) {
            return;
        }

        $imageGenerator = $this->determineImageGenerator($media);

        if (! $imageGenerator) {
            return;
        }

        $temporaryDirectory = TemporaryDirectory::create();

        $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary(
            $media,
            $temporaryDirectory->path(str_random(16).'.'.$media->extension)
        );

        $conversions
            ->reject(function (Conversion $conversion) use ($onlyIfMissing, $media) {
                $relativePath = $media->getPath($conversion->getName());

                $rootPath = config('filesystems.disks.'.$media->disk.'.root');

                if ($rootPath) {
                    $relativePath = str_replace($rootPath, '', $relativePath);
                }

                return $onlyIfMissing && Storage::disk($media->disk)->exists($relativePath);
            })
            ->each(function (Conversion $conversion) use ($media, $imageGenerator, $copiedOriginalFile) {
                event(new ConversionWillStart($media, $conversion, $copiedOriginalFile));

                $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion);

                $manipulationResult = $this->performManipulations($media, $conversion, $copiedOriginalFile);

                $newFileName = pathinfo($media->file_name, PATHINFO_FILENAME).
                    '-'.$conversion->getName().
                    '.'.$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION));

                $renamedFile = MediaLibraryFileHelper::renameInDirectory($manipulationResult, $newFileName);

                if ($conversion->shouldGenerateResponsiveImages()) {
                    app(ResponsiveImageGenerator::class)->generateResponsiveImagesForConversion(
                        $media,
                        $conversion,
                        $renamedFile
                    );
                }

                app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, 'conversions');

                $media->markAsConversionGenerated($conversion->getName(), true);

                event(new ConversionHasBeenCompleted($media, $conversion));
            });

        $temporaryDirectory->delete();
    }
public function performConversions ( ConversionCollection $ conversions , Media $ media , $ onlyIfMissing = false ) { if ( $ conversions -> isEmpty ( ) ) { return ; } $ imageGenerator = $ this -> determineImageGenerator ( $ media ) ; if ( ! $ imageGenerator ) { return ; } $ temporaryDirectory = TemporaryDirectory :: create ( ) ; $ copiedOriginalFile = app ( Filesystem :: class ) -> copyFromMediaLibrary ( $ media , $ temporaryDirectory -> path ( str_random ( 16 ) . '.' . $ media -> extension ) ) ; $ conversions -> reject ( function ( Conversion $ conversion ) use ( $ onlyIfMissing , $ media ) { $ relativePath = $ media -> getPath ( $ conversion -> getName ( ) ) ; $ rootPath = config ( 'filesystems.disks.' . $ media -> disk . '.root' ) ; if ( $ rootPath ) { $ relativePath = str_replace ( $ rootPath , '' , $ relativePath ) ; } return $ onlyIfMissing && Storage :: disk ( $ media -> disk ) -> exists ( $ relativePath ) ; } ) -> each ( function ( Conversion $ conversion ) use ( $ media , $ imageGenerator , $ copiedOriginalFile ) { event ( new ConversionWillStart ( $ media , $ conversion , $ copiedOriginalFile ) ) ; $ copiedOriginalFile = $ imageGenerator -> convert ( $ copiedOriginalFile , $ conversion ) ; $ manipulationResult = $ this -> performManipulations ( $ media , $ conversion , $ copiedOriginalFile ) ; $ newFileName = pathinfo ( $ media -> file_name , PATHINFO_FILENAME ) . '-' . $ conversion -> getName ( ) . '.' . $ conversion -> getResultExtension ( pathinfo ( $ copiedOriginalFile , PATHINFO_EXTENSION ) ) ; $ renamedFile = MediaLibraryFileHelper :: renameInDirectory ( $ manipulationResult , $ newFileName ) ; if ( $ conversion -> shouldGenerateResponsiveImages ( ) ) { app ( ResponsiveImageGenerator :: class ) -> generateResponsiveImagesForConversion ( $ media , $ conversion , $ renamedFile ) ; } app ( Filesystem :: class ) -> copyToMediaLibrary ( $ renamedFile , $ media , 'conversions' ) ; $ media -> markAsConversionGenerated ( $ conversion -> getName ( ) , true ) ; event ( new ConversionHasBeenCompleted ( $ media , $ conversion ) ) ; } ) ; $ temporaryDirectory -> delete ( ) ; }
Perform the given conversions for the given media.

@param \Spatie\MediaLibrary\Conversion\ConversionCollection $conversions
@param \Spatie\MediaLibrary\Models\Media $media
@param bool $onlyIfMissing
Perform the given conversions for the given media .