Skip to content

Instantly share code, notes, and snippets.

@visay
Last active August 29, 2015 14:10
Show Gist options
  • Save visay/8efb3008cefe438fe509 to your computer and use it in GitHub Desktop.
Save visay/8efb3008cefe438fe509 to your computer and use it in GitHub Desktop.
Render thumbnail from PDF file for a node type
<?php
namespace WE\SitePackage\ViewHelpers;
/* *
* This script belongs to the TYPO3 Flow package "WE.SitePackage". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Resource\Publishing\ResourcePublisher;
use TYPO3\Flow\Resource\Resource;
use TYPO3\Flow\Utility\Files;
use TYPO3\Neos\Domain\Repository\DomainRepository;
/**
* ViewHelper to generate image thumbnail from a pdf resource
*
* @author Visay Keo <visay@web-essentials.asia>
*/
class PdfThumbnailViewHelper extends AbstractViewHelper {
/**
* @var ResourcePublisher
* @Flow\Inject
*/
protected $resourcePublisher;
/**
* @var DomainRepository
* @Flow\Inject
*/
protected $domainRepository;
/**
* @param Resource $resource
* @param string $alt
* @param int $width
* @param int $height
* @param string $type
* @param string $binary
*
* @return string
*/
public function render(Resource $resource, $alt = '', $width = 0, $height = 0, $type = 'png', $binary = '') {
$currentDomain = $this->domainRepository->findOneByActiveRequest();
$packageKey = $currentDomain->getSite()->getSiteResourcesPackageKey();
$thumbnailDirectory = 'Packages/' . $packageKey . '/Thumbnails/';
$hash = $resource->getResourcePointer()->getHash();
$thumbnailPath = Files::concatenatePaths(array(FLOW_PATH_WEB, '_Resources', 'Static', $thumbnailDirectory));
if (!file_exists($thumbnailPath)) {
Files::createDirectoryRecursively($thumbnailPath);
}
$imageFile = $thumbnailPath . '/' . $hash . '.' . $type;
if (!file_exists($imageFile)) {
$binary = $binary ?: '/usr/bin/convert' ;
$param = "+profile '*' -colorspace RGB -density 100";
$pdfSource = FLOW_PATH_DATA . 'Persistent/Resources/' . $hash;
$command = $binary . ' ' . $param . ' ' . $pdfSource . '[0] ' . $imageFile;
exec($command);
}
$imageUri = $this->resourcePublisher->getStaticResourcesWebBaseUri() . $thumbnailDirectory . $hash . '.' . $type;
$width = ($width > 0) ? 'width="' . $width . '" ' : '';
$height = ($height > 0) ? 'height="' . $height . '" ' : '';
$output = '<img src="' . $imageUri . '" alt="' . $alt . '" ' . $width . $height . '/>';
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment