Concrete5:将文件缩略图设置为生成的图像(例如 PDF)

Concrete5: set File thumbnail to generated image (e.g. for PDFs)

我正在使用 Concrete5,我正在尝试显示各种上传文件的缩略图。虽然其中一些可能是图片,但大部分是 PDF。

我目前正在使用:

<?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 } ?>

我更愿意以某种方式创建较小的 PDF 文件缩略图,例如在后台使用 ghostscript。在内置的文件管理器中,至少会显示一个 PDF 图标。这将不是最佳选择,但总比不显示任何东西来表示我们正在处理 PDF 更好..

如何访问内置缩略图?而且,更重要的是,我如何在上传某些文件类型时正确覆盖它们?

编辑:

我遇到了 $file->getThumbnailURL('type'); 并为我自己的目的创建了一个类型。上传文件时如何自动生成这样的缩略图?我可能会弄清楚如何使用纯 PHP 生成文件,但我不确定将其存储在 Concrete5 中。

毕竟在 C5 中这似乎是可能的,使用 file inspectors:

Any time a file is imported into Concrete5 (which happens through an instance of the File Importer class) it may be run through an optional file Inspector, which is a PHP class that can perform additional operations on files of a certain type when they're uploaded or rescanned

可以找到有关文件检查器的更多信息和实施示例in the C5 documentation.

this Concrete5 forum discussion 中,似乎有人使用此功能构建了您想要构建的内容,即使用 ImageMagick 的 PDF 缩略图生成器。

该用户的示例代码做了两件事。首先,它向 运行 C5 实例注册一个新的自定义文件检查器。然后,您的自定义检查器库将添加到项目中。

最后,这是我的做法。

我首先在包控制器的配置方法中创建了一个新的缩略图类型,如下所示:

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();
}

然后我创建了一个class mypackage/src/document_processing/pdfthumbnails.php,内容如下:

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()));
        }
    }
}

然后我在包的控制器中连接到 on_file_version_add 事件:

use Concrete\Package\Mypackage\Src\DocumentProcessing\Pdfthumbnails;

...

    public function on_start() {
        Events::addListener('on_file_version_add', array(new Pdfthumbnails(), 'processPDFThumbnails'));
    }