aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Thumbs/Pdf.php
blob: 5d413140f6e6b79a9deb11a1595ee403d4e077f3 (plain) (blame)
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
<?php

namespace Zotlabs\Thumbs;


class Pdf {

	function Match($type) {
		return(($type === 'application/pdf') ? true : false );
	}

	function Thumb($attach,$preview_style,$height = 300, $width = 300) {

		$photo = false;

		$file = dbunescbin($attach['content']);
		$tmpfile = $file . '.pdf';
		$outfile = $file . '.jpg';

		$istream = fopen($file,'rb');
		$ostream = fopen($tmpfile,'wb');
        if($istream && $ostream) {
            pipe_streams($istream,$ostream);
            fclose($istream);
            fclose($ostream);
		}

		$imagick_path = get_config('system','imagick_convert_path');
		if($imagick_path && @file_exists($imagick_path)) {
			$cmd = $imagick_path . ' ' . escapeshellarg(PROJECT_BASE . '/' . $tmpfile . '[0]') . ' -thumbnail ' . $width . 'x' . $height . ' ' . escapeshellarg(PROJECT_BASE . '/' . $outfile);
			//  logger('imagick thumbnail command: ' . $cmd);
			for($x = 0; $x < 4; $x ++) {
				exec($cmd);
				if(! file_exists($outfile)) {
					logger('imagick scale failed. Retrying.');
					continue;
				}
			}
			if(! file_exists($outfile)) {
				logger('imagick scale failed.');
			}
			else {
				@rename($outfile,$file . '.thumb');
			}
			@unlink($tmpfile);
		}
	}
}