Concrete5: set the file thumbnail for the generated image (for example, for PDF files)

I am using Concrete5 and I am trying to display thumbnails for various downloaded files. Although some of them may be images, most of them are PDF files.

I am currently using:

<?php $file = File::getByID($fID); $imageHelper = Core::make('helper/image'); try { $imageHelper->outputThumbnail($file, 200, 200); } catch(InvalidArgumentException $e) { ?> <img src='https://placehold.it/200x200'> <?php } ?> 

I would prefer to somehow create a smaller thumbnail of PDF files, for example using ghostscript in the background. The built-in file manager displays at least the PDF icon. This would not be the best option, but still better than not showing anything to mean that we are dealing with a PDF file.

How to access the built-in thumbnails? And, more importantly, how can I overwrite them on top of certain types of files when they are downloaded?

EDIT:

I came across $file->getThumbnailURL('type'); and created a type for my own purposes. How do you automatically create a thumbnail when you upload a file? I can probably figure out how to generate a file with simple PHP, but storing it in Concrete5 is something I don't know about.

+5
source share
2 answers

In the end, this is how I did it.

I started by creating a new type of sketch in the configure method of my package controller as follows:

 use Concrete\Core\File\Image\Thumbnail\Type\Type; ... public function configure($pkg) { ... $thumbnailType = new Type(); $thumbnailType->setName(tc('ThumbnailTypeName', 'PDF Thumbnails')); $thumbnailType->setHandle('pdfthumbnails'); $thumbnailType->setWidth(200); $thumbnailType->setHeight(200); $thumbnailType->save(); } 

Then I created the mypackage/src/document_processing/pdfthumbnails.php with the following contents:

 namespace Concrete\Package\Mypackage\Src\DocumentProcessing; use Core; use File; use Concrete\Core\File\Image\Thumbnail\Type\Type; class Pdfthumbnails { public function processPDFThumbnails($fv) { $fi = Core::make('helper/file'); $fvObj = $fv->getFileVersionObject(); $ext = $fi->getExtension($fvObj->getFilename()); $file = $fvObj->getFile(); if ($ext == 'pdf') { $type = Type::getByHandle('pdfthumbnails'); $basetype = $type->getBaseVersion(); $thumbpath = $basetype->getFilePath($fvObj); $fsl = $file->getFileStorageLocationObject()->getFileSystemObject(); $fre = $fvObj->getFileResource(); // this requires sufficient permissions.. // depending on your setup, reconsider 0777 mkdir('application/files'.dirname($thumbpath), 0777, true); exec('gs -o application/files'.escapeshellarg($thumbpath).' -dPDFFitPage -sDEVICE=png16m -g200x200 -dLastPage=1 -f application/files/'.escapeshellarg($fre->getPath())); } } } 

And then I connected to the on_file_version_add event in my package controller:

 use Concrete\Package\Mypackage\Src\DocumentProcessing\Pdfthumbnails; ... public function on_start() { Events::addListener('on_file_version_add', array(new Pdfthumbnails(), 'processPDFThumbnails')); } 
+2
source

In any case, this is possible inside C5 using file inspectors :

Each time a file is imported into Concrete5 (which occurs through an instance of the File Importer class), it can be launched through an optional Inspector file, which is a PHP class that can perform additional operations on files of a certain type when they are loaded or scanned

For more information and implementation examples of file inspectors, see the C5 documentation.

In this discussion of the Concrete5 forum , someone seems to be using this feature to create exactly what you want to build a thumbnail generator for PDF files using ImageMagick.

This example user code does two things. First, it registers a new custom file inspector with a running C5 instance. Then your custom inspector library is added to the project.

+1
source

All Articles