src/File/FileUtil.php
public function getPreviewFromPdf(FilesModel $file, int $page = 0): FilesModel
    {
        $strippedName = str_replace('.'.$file->extension, '', $file->name);
        $previewFileName = 'preview-'.$strippedName.'.'.static::FILE_UTIL_CONVERT_FILE_TYPE;
        $folder = str_replace($file->name, '', $file->path);
        $target = $folder.\DIRECTORY_SEPARATOR.$previewFileName;

        // ghostscript
        /** @var Transcoder $transcoder */
        $transcoder = $this->framework->getAdapter(Transcoder::class)->create();
        $transcoder->toImage($file->path, $target);

        // get all created images
        $folderFiles = scandir($folder);
        $pdfPreviewFiles = [];
        $needle = '/preview-'.$strippedName.'*\.'.static::FILE_UTIL_CONVERT_FILE_TYPE.'/';

        foreach ($folderFiles as $file) {
            if (!preg_match($needle, $file)) {
                continue;
            }

            $pdfPreviewFiles[] = $file;
        }

        $preview = null;

        foreach ($pdfPreviewFiles as $key => $value) {
            if ($page != $key && file_exists($value)) {
                unlink($value);

                continue;
            }

            $preview = $value;
        }

        if (null === ($previewFile = $this->framework->getAdapter(FilesModel::class)->findByPath($preview))) {
            $previewFile = $this->framework->getAdapter(Dbafs::class)->addResource($folder.\DIRECTORY_SEPARATOR.$preview);
        }

        return $previewFile;
    }
public function getPreviewFromPdf(FilesModel $file, int $page = 0): FilesModel
    {
        $strippedName = str_replace('.'.$file->extension, '', $file->name);
        $previewFileName = 'preview-'.$strippedName.'.'.static::FILE_UTIL_CONVERT_FILE_TYPE;
        $folder = str_replace($file->name, '', $file->path);
        $target = $folder.\DIRECTORY_SEPARATOR.$previewFileName;

        // ghostscript
        /** @var Transcoder $transcoder */
        $transcoder = $this->framework->getAdapter(Transcoder::class)->create();
        $transcoder->toImage($file->path, $target);

        // get all created images
        $folderFiles = scandir($folder);
        $pdfPreviewFiles = [];
        $needle = '/preview-'.$strippedName.'*\.'.static::FILE_UTIL_CONVERT_FILE_TYPE.'/';

        foreach ($folderFiles as $file) {
            if (!preg_match($needle, $file)) {
                continue;
            }

            $pdfPreviewFiles[] = $file;
        }

        $preview = null;

        foreach ($pdfPreviewFiles as $key => $value) {
            if ($page != $key && file_exists($value)) {
                unlink($value);

                continue;
            }

            $preview = $value;
        }

        if (null === ($previewFile = $this->framework->getAdapter(FilesModel::class)->findByPath($preview))) {
            $previewFile = $this->framework->getAdapter(Dbafs::class)->addResource($folder.\DIRECTORY_SEPARATOR.$preview);
        }

        return $previewFile;
    }
public function getPreviewFromPdf ( FilesModel $ file , int $ page = 0 ) : FilesModel { $ strippedName = str_replace ( '.' . $ file -> extension , '' , $ file -> name ) ; $ previewFileName = 'preview-' . $ strippedName . '.' . static :: FILE_UTIL_CONVERT_FILE_TYPE ; $ folder = str_replace ( $ file -> name , '' , $ file -> path ) ; $ target = $ folder . \ DIRECTORY_SEPARATOR . $ previewFileName ; // ghostscript /** @var Transcoder $transcoder */ $ transcoder = $ this -> framework -> getAdapter ( Transcoder :: class ) -> create ( ) ; $ transcoder -> toImage ( $ file -> path , $ target ) ; // get all created images $ folderFiles = scandir ( $ folder ) ; $ pdfPreviewFiles = [ ] ; $ needle = '/preview-' . $ strippedName . '*\.' . static :: FILE_UTIL_CONVERT_FILE_TYPE . '/' ; foreach ( $ folderFiles as $ file ) { if ( ! preg_match ( $ needle , $ file ) ) { continue ; } $ pdfPreviewFiles [ ] = $ file ; } $ preview = null ; foreach ( $ pdfPreviewFiles as $ key => $ value ) { if ( $ page != $ key && file_exists ( $ value ) ) { unlink ( $ value ) ; continue ; } $ preview = $ value ; } if ( null === ( $ previewFile = $ this -> framework -> getAdapter ( FilesModel :: class ) -> findByPath ( $ preview ) ) ) { $ previewFile = $ this -> framework -> getAdapter ( Dbafs :: class ) -> addResource ( $ folder . \ DIRECTORY_SEPARATOR . $ preview ) ; } return $ previewFile ; }
convert pdf to png and return a preview file
delete the other png files.

@param FilesModel $file
@param int        $page

@deprecated Dublicate to PdfPreview util

@return FilesModel
convert pdf to png and return a preview file delete the other png files .