summaryrefslogtreecommitdiff
path: root/extensions/PdfHandler/PdfHandler.image.php
blob: 49da7f4ecd8cc626afc50990ae7b9b14201764b4 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/**
 *
 * Copyright © 2007 Xarax <jodeldi@gmx.de>
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */

/**
 * inspired by djvuimage from Brion Vibber
 * modified and written by xarax
 */

class PdfImage {

	/**
	 * @param $filename
	 */
	function __construct( $filename ) {
		$this->mFilename = $filename;
	}

	/**
	 * @return bool
	 */
	public function isValid() {
		return true;
	}

	/**
	 * @return array|bool
	 */
	public function getImageSize() {
		$data = $this->retrieveMetadata();
		$size = self::getPageSize( $data, 1 );

		if( $size ) {
			$width = $size['width'];
			$height = $size['height'];
			return array( $width, $height, 'Pdf',
				"width=\"$width\" height=\"$height\"" );
		}
		return false;
	}

	/**
	 * @param $data array
	 * @param $page
	 * @return array|bool
	 */
	public static function getPageSize( $data, $page ) {
		global $wgPdfHandlerDpi;

		if( isset( $data['pages'][$page]['Page size'] ) ) {
			$o = $data['pages'][$page]['Page size'];
		} elseif( isset( $data['Page size'] ) ) {
			$o = $data['Page size'];
		} else {
			$o = false;
		}

		if ( $o ) {
			if( isset( $data['pages'][$page]['Page rot'] ) ) {
				$r = $data['pages'][$page]['Page rot'];
			} elseif( isset( $data['Page rot'] ) ) {
				$r = $data['Page rot'];
			} else {
				$r = 0;
			}
			$size = explode( 'x', $o, 2 );

			if ( $size ) {
				$width  = intval( trim( $size[0] ) / 72 * $wgPdfHandlerDpi );
				$height = explode( ' ', trim( $size[1] ), 2 );
				$height = intval( trim( $height[0] ) / 72 * $wgPdfHandlerDpi );
				if ( ( $r/90 ) & 1 ) {
					// Swap width and height for landscape pages
					$t = $width;
					$width = $height;
					$height = $t;
				}

				return array(
					'width' => $width,
					'height' => $height
				);
			}
		}

		return false;
	}

	/**
	 * @return array|bool|null
	 */
	public function retrieveMetaData() {
		global $wgPdfInfo, $wgPdftoText;

		if ( $wgPdfInfo ) {
			wfProfileIn( 'pdfinfo' );
			$cmd = wfEscapeShellArg( $wgPdfInfo ) .
				" -enc UTF-8 " . # Report metadata as UTF-8 text...
				" -l 9999999 " . # Report page sizes for all pages
				" -meta " .      # Report XMP metadata
				wfEscapeShellArg( $this->mFilename );
			$retval = '';
			$dump = wfShellExec( $cmd, $retval );
			$data = $this->convertDumpToArray( $dump );
			wfProfileOut( 'pdfinfo' );
		} else {
			$data = null;
		}

		# Read text layer
		if ( isset( $wgPdftoText ) ) {
			wfProfileIn( 'pdftotext' );
			$cmd = wfEscapeShellArg( $wgPdftoText ) . ' '. wfEscapeShellArg( $this->mFilename ) . ' - ';
			wfDebug( __METHOD__.": $cmd\n" );
			$retval = '';
			$txt = wfShellExec( $cmd, $retval );
			wfProfileOut( 'pdftotext' );
			if( $retval == 0 ) {
				$txt = str_replace( "\r\n", "\n", $txt );
				$pages = explode( "\f", $txt );
				foreach( $pages as $page => $pageText ) {
					# Get rid of invalid UTF-8, strip control characters
					# Note we need to do this per page, as \f page feed would be stripped.
					$pages[$page] = UtfNormal::cleanUp( $pageText );
				}
				$data['text'] = $pages;
			}
		}
		return $data;
	}

	/**
	 * @param $dump string
	 * @return array|bool
	 */
	protected function convertDumpToArray( $dump ) {
		if ( strval( $dump ) == '' ) {
			return false;
		}

		$lines = explode( "\n", $dump );
		$data = array();

		// Metadata is always the last item, and spans multiple lines.
		$inMetadata = false;

		// Basically this loop will go through each line, splitting key value
		// pairs on the colon, until it gets to a "Metadata:\n" at which point
		// it will gather all remaining lines into the xmp key.
		foreach( $lines as $line ) {
			if ( $inMetadata ) {
				# Handle XMP differently due to diffence in line break
				$data['xmp'] .= "\n$line";
				continue;
			}
			$bits = explode( ':', $line, 2 );
			if( count( $bits ) > 1 ) {
				$key = trim( $bits[0] );
				if ( $key === 'Metadata' ) {
					$inMetadata = true;
					$data['xmp'] = '';
					continue;
				}
				$value = trim( $bits[1] );
				$matches = array();
				// "Page xx rot" will be in poppler 0.20's pdfinfo output
				// See https://bugs.freedesktop.org/show_bug.cgi?id=41867
				if( preg_match( '/^Page +(\d+) (size|rot)$/', $key, $matches ) ) {
					$data['pages'][$matches[1]][$matches[2] == 'size' ? 'Page size' : 'Page rot'] = $value;
				} else {
					$data[$key] = $value;
				}
			}
		}
		$data = $this->postProcessDump( $data );
		return $data;
	}

	/**
	 * Postprocess the metadata (convert xmp into useful form, etc)
	 *
	 * This is used to generate the metadata table at the bottom
	 * of the image description page.
	 *
	 * @param $data Array metadata
	 * @return Array post-processed metadata
	 */
	protected function postProcessDump( array $data ) {

		$meta = new BitmapMetadataHandler();
		$items = array();
		foreach( $data as $key => $val ) {
			switch ( $key ) {
				case 'Title':
					$items['ObjectName'] = $val;
					break;
				case 'Subject':
					$items['ImageDescription'] = $val;
					break;
				case 'Keywords':
					// Sometimes we have empty keywords. This seems
					// to be a product of how pdfinfo deals with keywords
					// with spaces in them. Filter such empty keywords
					$keyList = array_filter( explode( ' ', $val ) );
					if ( count( $keyList ) > 0 ) {
						$items['Keywords'] = $keyList;
					}
					break;
				case 'Author':
					$items['Artist'] = $val;
					break;
				case 'Creator':
					// Program used to create file.
					// Different from program used to convert to pdf.
					$items['Software'] = $val;
					break;
				case 'Producer':
					// Conversion program
					$items['pdf-Producer'] = $val;
					break;
				case 'ModTime':
					$timestamp = wfTimestamp( TS_EXIF, $val );
					if ( $timestamp ) {
						// 'if' is just paranoia
						$items['DateTime'] = $timestamp;
					}
					break;
				case 'CreationTime':
					$timestamp = wfTimestamp( TS_EXIF, $val );
					if ( $timestamp ) {
						$items['DateTimeDigitized'] = $timestamp;
					}
					break;
				// These last two (version and encryption) I was unsure
				// if we should include in the table, since they aren't
				// all that useful to editors. I leaned on the side
				// of including. However not including if file
				// is optimized/linearized since that is really useless
				// to an editor.
				case 'PDF version':
					$items['pdf-Version'] = $val;
					break;
				case 'Encrypted':
					// @todo: The value isn't i18n-ised. The appropriate
					// place to do that is in FormatMetadata.php
					// should add a hook a there.
					// For reference, if encrypted this fields value looks like:
					// "yes (print:yes copy:no change:no addNotes:no)"
					$items['pdf-Encrypted'] = $val;
					break;
				// Note 'pages' and 'Pages' are different keys (!)
				case 'pages':
					// A pdf document can have multiple sized pages in it.
					// (However 95% of the time, all pages are the same size)
					// get a list of all the unique page sizes in document.
					// This doesn't do anything with rotation as of yet,
					// mostly because I am unsure of what a good way to
					// present that information to the user would be.
					$pageSizes = array();
					foreach( $val as $page ) {
						if( isset( $page['Page size'] ) ) {
							$pageSizes[ $page['Page size'] ] = true;
						}
					}

					$pageSizeArray = array_keys( $pageSizes );
					if ( count( $pageSizeArray ) > 0 ) {
						$items['pdf-PageSize'] = $pageSizeArray;
					}
					break;
			}

		}
		$meta->addMetadata( $items, 'native' );

		if ( isset( $data['xmp'] ) && function_exists( 'xml_parser_create_ns' ) ) {
			// func exists verifies that the xml extension required for XMPReader
			// is present (Almost always is present)
			// @todo: This only handles generic xmp properties. Would be improved
			// by handling pdf xmp properties (pdf and pdfx) via XMPInfo hook.
			$xmp = new XMPReader();
			$xmp->parse( $data['xmp'] );
			$xmpRes = $xmp->getResults();
			foreach ( $xmpRes as $type => $xmpSection ) {
				$meta->addMetadata( $xmpSection, $type );
			}
		}
		unset( $data['xmp'] );
		$data['mergedMetadata'] = $meta->getMetadataArray();
		return $data;
	}
}