/
PdfThumbnailViewHelper.php
101 lines (97 loc) · 4.54 KB
/
PdfThumbnailViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Media;
/***************************************************************
* Copyright notice
*
* (c) 2013 Björn Fromme <fromme@dreipunktnull.com>, dreipunktnull
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */
use TYPO3\CMS\Core\Utility\CommandUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Converts the provided PDF file into a PNG thumbnail and renders
* the according image tag using Fluid's standard image ViewHelper
* thus implementing its arguments. For PDF documents with multiple
* pages the first page is rendered by default unless specified.
*
* @author Björn Fromme <fromme@dreipunktnull.com>, dreipunktnull
* @package Vhs
* @subpackage ViewHelpers\Media
*/
class PdfThumbnailViewHelper extends ImageViewhelper {
/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('path', 'string', 'Path to PDF source file');
$this->registerArgument('minWidth', 'integer', 'Minimum width of resulting thumbnail image', FALSE, NULL);
$this->registerArgument('minHeight', 'integer', 'Minimum height of resulting thumbnail image', FALSE, NULL);
$this->registerArgument('maxWidth', 'integer', 'Maximum width of resulting thumbnail image', FALSE, NULL);
$this->registerArgument('maxHeight', 'integer', 'Maximum height of resulting thumbnail image', FALSE, NULL);
$this->registerArgument('density', 'integer', 'Canvas resolution for rendering the PDF in dpi (higher means better quality)', FALSE, 100);
$this->registerArgument('background', 'string', 'Fill background of resulting image with this color (for transparent source files)', FALSE, NULL);
$this->registerArgument('rotate', 'integer', 'Number of degress to rotate resulting image by (caution: very slow if not multiple of 90)', FALSE, 0);
$this->registerArgument('page', 'integer', 'Optional page number to render as thumbnail for PDF documents with multiple pages', FALSE, 1);
$this->registerArgument('forceOverwrite', 'boolean', 'Forcibly overwrite existing converted PDF files', FALSE, FALSE);
}
/**
* @return string
*/
public function render() {
$path = GeneralUtility::getFileAbsFileName($this->arguments['path']);
if (FALSE === file_exists($path)) {
return NULL;
}
$density = $this->arguments['density'];
$rotate = $this->arguments['rotate'];
$page = intval($this->arguments['page']);
$background = $this->arguments['background'];
$forceOverwrite = (boolean) $this->arguments['forceOverwrite'];
$width = $this->arguments['width'];
$height = $this->arguments['height'];
$minWidth = $this->arguments['minWidth'];
$minHeight = $this->arguments['minHeight'];
$maxWidth = $this->arguments['maxWidth'];
$maxHeight = $this->arguments['maxHeight'];
$filename = basename($path);
$pageArgument = $page > 0 ? $page - 1 : 0;
$colorspace = TRUE === isset($GLOBALS['TYPO3_CONF_VARS']['GFX']['colorspace']) ? $GLOBALS['TYPO3_CONF_VARS']['GFX']['colorspace'] : 'RGB';
$destination = GeneralUtility::getFileAbsFileName('typo3temp/vhs-pdf-' . $filename . '-page' . $page . '.png');
if (FALSE === file_exists($destination) || TRUE === $forceOverwrite) {
$arguments = '-colorspace ' . $colorspace;
if (0 < intval($density)) {
$arguments .= ' -density ' . $density;
}
if (0 !== intval($rotate)) {
$arguments .= ' -rotate ' . $rotate;
}
$arguments .= ' "' . $path . '"[' . $pageArgument . ']';
if (NULL !== $background) {
$arguments .= ' -background "' . $background . '" -flatten';
}
$arguments .= ' "' . $destination . '"';
$command = CommandUtility::imageMagickCommand('convert', $arguments);
CommandUtility::exec($command);
}
$image = substr($destination, strlen(PATH_site));
return parent::render($image, $width, $height, $minWidth, $minHeight, $maxWidth, $maxHeight);
}
}