class Poppler
{
- public static function extractArea($file, $page, $rect, $to, $options = array(), $cache = null)
+ public static function extractArea($file, $page, $rect, $to = null, $options = array(), $cache = null)
{
+ if (null === $to && null === $cache) {
+ throw new \Exception('You have to define either a destination file or a cache folder');
+ }
+
$defaultOptions = array('resolution' => 300,
'quality' => '95',
'antialias' => true,
$extension = $options['format'];
}
- $res = $to . '.' . $extension;
+ if (null !== $to) {
+ $res = $to . '.' . $extension;
+ }
$cacheFile = null;
-
-
if (null !== $cache) {
Files::mkdir($cache);
- $f = hash_file('sha256', $file) . '||' . '++' . json_encode($rect) . '**' . json_encode($options) . '||' . $page . '!!';
+ $f = Files::hashFileAttributes($file) . '||' . '++' . json_encode($rect) . '**' . json_encode($options) . '||' . $page . '!!';
$hash = hash('sha256', $f);
- $cacheFile = $cache . '/' . $hash . '.' . $extension;
+
+ $cacheFileWithoutExt = $cache . $hash;
+ $cacheFile = $cacheFileWithoutExt . '.' . $extension;
+ if (null === $to) {
+ $res = $cacheFile;
+ $dest = $cacheFileWithoutExt;
+ } else {
+ $dest = str_replace('.' . $extension, '', $to);
+ }
if (file_exists($cacheFile)) {
+ if (null === $to) {
+ return $cacheFile;
+ }
copy($cacheFile, $res);
return $res;
}
$pdftoppm = new CommandLine('pdftoppm');
$pdftoppm->setArg('r', $options['resolution']);
- if ($options['format'] === 'png') {
- $pdftoppm->setArg('-' . $options['format']);
+ if ($extension === 'png') {
+ $pdftoppm->setArg('-png');
+ } elseif ($extension === 'jpg') {
+ $pdftoppm->setArg('-jpeg');
+ $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"');
}
$aa = $options['antialias'] ? 'yes' : 'no';
$pdftoppm->setArg('-aa', $aa);
$pdftoppm->setArg('-W', round($rect['width'] * $options['factor']));
$pdftoppm->setArg('-H', round($rect['height'] * $options['factor']));
$pdftoppm->setArg(null, $file);
- $pdftoppm->setArg(null, $to);
+ $pdftoppm->setArg(null, $dest);
$pdftoppm->execute();
- $pdftoppm->debug();
-
- if ($options['format'] === 'jpeg') {
- $cjpeg = new CommandLine('cjpeg', $res);
- $cjpeg->setArg('-quality', $options['quality']);
- $cjpeg->setArg(null, $to . '.ppm');
- $cjpeg->execute();
- }
if (null !== $cacheFile) {
+ if (null === $to) {
+ return $cacheFile;
+ }
copy($res, $cacheFile);
}
-
return $res;
-
}
protected static function _pdftojpeg($file, $to, $nbpages, $options = array())
class PDF
{
- public static function extractArea($file, $page, $rect, $to, $options = array(), $cache = null){
+ public static function extractArea($file, $page, $rect, $to=null, $options = array(), $cache = null){
return Poppler::extractArea($file,$page,$rect,$to,$options,$cache);
}
}
\ No newline at end of file