class Poppler
{
+ const RESOLUTION_FACTOR = 72;
+
public static function extractArea($file, $page, $rect, $to = null, $options = array(), $cache = null)
{
if (null === $to && null === $cache) {
'internalformat' => 'png');
$options = array_merge($defaultOptions, $options);
- $options['factor'] = $options['resolution'] / 72;
+ $options['factor'] = $options['resolution'] / self::RESOLUTION_FACTOR;
if ($options['format'] === 'jpeg') {
$extension = 'jpg';
$cacheFileWithoutExt = $cache . $hash;
$cacheFile = $cacheFileWithoutExt . '.' . $extension;
+ $lockFile = $cacheFile . '.lock';
+
if (null === $to) {
$res = $cacheFile;
$dest = $cacheFileWithoutExt;
}
}
-// error_log('####--------------####');
-// error_log('___ Resolution: ' . $options['resolution']);
-// error_log('___ Factor: ' . $factor);
-// error_log('extractArea: '. $f);
-// error_log("CACHE MISS: $cacheFile");
-
+ if (isset($lockFile)) {
+ Files::getLock($lockFile);
+ }
$pdftoppm = new CommandLine('pdftoppm');
$pdftoppm->setArg('r', $options['resolution']);
$pdftoppm->setArg(null, $dest);
$pdftoppm->execute();
+ if (isset($lockFile)) {
+ Files::releaseLock($lockFile);
+ }
+
if (null !== $cacheFile) {
if (null === $to) {
return $cacheFile;
}
}
+ public static function getLock($lockFile, $maxAge = 300, $touch = true)
+ {
+ while (file_exists($lockFile) && filemtime($lockFile) > (time() - $maxAge)) {
+ sleep(5);
+ }
+ if ($touch) {
+ touch($lockFile);
+ }
+ }
+
+ public static function releaseLock($lockFile)
+ {
+ if (file_exists($lockFile)) {
+ unlink($lockFile);
+ }
+ }
}
\ No newline at end of file