Skip to content

Commit de669a6

Browse files
committed
#91 - Create a zip file containing multiple image files
1 parent a05001c commit de669a6

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

src/Configuration/ConfigurationDefaults.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,19 @@ public function __construct(&$settings) {
116116
'#engine' => 'Convert\\ImageMagick',
117117
),
118118
),
119-
'pdf->jpg' => array(
119+
'pdf->(jpg|png)' => array(
120120
'imagemagick:default' => array(
121121
'#engine' => 'Convert\\ImageMagick',
122122
"colorspace" => "sRGB",
123123
"flatten" => 1,
124124
),
125125
),
126+
'pdf->(zip/png)' => array(
127+
'imagemagick:default' => array(
128+
'#engine' => 'Convert\\ImageMagick',
129+
"colorspace" => "sRGB",
130+
),
131+
),
126132
'ps->pdf' => array(
127133
'ghostscript:default' => array(
128134
'#engine' => 'Convert\\GhostScript',

src/Engine/Convert/ImageMagick.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use FileConverter\Engine\EngineBase;
1414
use FileConverter\Util\Shell;
15+
use FileConverter\Engine\Helper\Archive;
1516
class ImageMagick extends EngineBase {
1617
protected $cmd_options = array(
1718
array(
@@ -46,8 +47,49 @@ class ImageMagick extends EngineBase {
4647
),
4748
);
4849

50+
public function convertFile($source, $destination) {
51+
if (preg_match('@zip/(?<ext>.*)$@s', $this->conversion[1], $arr)) {
52+
// Create a temp directory and convert the images.
53+
$ext = $arr['ext'];
54+
$imageArchive = new Archive($this);
55+
$imagePath = $imageArchive->getTempDirectory();
56+
$this->shell(array(
57+
$this->cmd,
58+
Shell::argOptions($this->cmd_options, $this->configuration, 1),
59+
$source,
60+
"$imagePath/page.$ext",
61+
));
62+
63+
// Create the temp directory
64+
$archive = new Archive($this);
65+
$tmp = $archive->getTempDirectory();
66+
// Rename the multiple image files in a standardized way
67+
if (is_file("$imagePath/page.$ext")) {
68+
$path = "$tmp/img/page1.$ext";
69+
$this->isTempWritable($path);
70+
rename("$imagePath/page.$ext", $path);
71+
}
72+
else {
73+
$i = 0;
74+
while (is_file("$imagePath/page-$i.$ext")) {
75+
$path = "$tmp/img/page" . ($i + 1) . ".$ext";
76+
$this->isTempWritable($path);
77+
rename("$imagePath/page-$i.$ext", $path);
78+
++$i;
79+
}
80+
}
81+
// Zip the files
82+
$archive->save($destination);
83+
return;
84+
}
85+
86+
return parent::convertFile($source, $destination);
87+
}
88+
4989
public function getConvertFileShell($source, &$destination) {
50-
$multipage = array('pdf');
90+
$multipage = array(
91+
'pdf',
92+
);
5193
if (in_array($this->conversion[0], $multipage)) {
5294
if (!in_array($this->conversion[1], $multipage)) {
5395
$source .= '[0]';

src/Engine/EngineBase.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class EngineBase {
2626
protected $converter = NULL;
2727
protected $conversion = array(
2828
'null',
29-
'null'
29+
'null',
3030
);
3131
protected $settings = array();
3232
protected $configuration = array();
@@ -142,6 +142,23 @@ public function getConfiguration() {
142142
return $this->configuration;
143143
}
144144

145+
/**
146+
* Get the conversion type
147+
* @param string $target source|destination
148+
* @param number $depth 0=full|1=top|2=2-levels
149+
* @return string
150+
*/
151+
public function getConversion($target = 'source', $depth = 0) {
152+
$type = $this->conversion[$target === 'source' ? 0 : 1];
153+
if ($depth < 1 || $depth > 1 + substr_count($type, '/')) {
154+
return $type;
155+
}
156+
$tmp = explode('/', $type, $depth + 1);
157+
array_pop($tmp);
158+
$type = join('/', $tmp);
159+
return $type;
160+
}
161+
145162
public function getHelp($type = 'installation') {
146163
$os = $this->settings['operating_system'];
147164
$os_version = $this->settings['operating_system_version'];
@@ -182,6 +199,26 @@ public function getVersionInfo() {
182199

183200
abstract public function isAvailable();
184201

202+
protected function isTempWritable($path) {
203+
$dir = $this->settings['temp_dir'];
204+
$path = preg_replace('@/[^/]*$@s', '', $path);
205+
if (strpos($path, $dir) !== 0) {
206+
return FALSE;
207+
}
208+
if (is_dir($path)) {
209+
return TRUE;
210+
}
211+
$tmp = explode('/', $path);
212+
$path = '';
213+
while (!empty($tmp)) {
214+
$path .= '/' . array_shift($tmp);
215+
if (!is_dir($path)) {
216+
mkdir($path);
217+
}
218+
}
219+
return (bool) is_dir($path);
220+
}
221+
185222
public function shell($command) {
186223
$cmd = "";
187224
$stderr = NULL;

src/Engine/Helper/Archive.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/*
3+
* This file is part of the FileConverter package.
4+
*
5+
* (c) Greg Payne
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace FileConverter\Engine\Helper;
12+
13+
use FileConverter\Engine\EngineBase;
14+
use FileConverter\Util\Shell;
15+
class Archive {
16+
/**
17+
* @var EngineBase
18+
*/
19+
protected $engine = NULL;
20+
/**
21+
* @var string
22+
*/
23+
protected $tempDirectory = NULL;
24+
25+
public function __construct(EngineBase &$engine) {
26+
$this->engine =& $engine;
27+
}
28+
29+
public function __destruct() {
30+
if (isset($this->tempDirectory)) {
31+
$this->engine->shell(array(
32+
'rm',
33+
Shell::arg('rf', Shell::SHELL_ARG_BOOL_SGL, TRUE),
34+
Shell::arg($this->tempDirectory, Shell::SHELL_ARG_BASIC),
35+
));
36+
}
37+
}
38+
39+
public function getTempDirectory() {
40+
if (!isset($this->tempDirectory)) {
41+
$this->tempDirectory = $this->engine->getTempFile('dir');
42+
@unlink($this->tempDirectory);
43+
mkdir($this->tempDirectory);
44+
}
45+
return $this->tempDirectory;
46+
}
47+
48+
public function save($destination) {
49+
switch ($ext = $this->engine->getConversion('destination', 1)) {
50+
case 'zip':
51+
$temp = $this->engine->getTempFile('zip');
52+
@unlink($temp);
53+
$cmd = array(
54+
'cd',
55+
Shell::arg($this->getTempDirectory(), Shell::SHELL_ARG_BASIC),
56+
Shell::arg('; ', Shell::SHELL_SAFE),
57+
$this->engine->shellWhich('zip'),
58+
Shell::arg('r', Shell::SHELL_ARG_BOOL_SGL, TRUE),
59+
Shell::arg($temp, Shell::SHELL_ARG_BASIC),
60+
Shell::arg('.', Shell::SHELL_SAFE),
61+
);
62+
echo $this->engine->shell($cmd);
63+
rename($temp, $destination);
64+
break;
65+
66+
default:
67+
throw new \InvalidArgumentException("Invalid archive format spec: $ext");
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)