Completed
Push — master ( 81153d...2a6722 )
by Thomas
52s
created

Bitmap   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getThumbnail() 0 20 3
A isAvailable() 0 3 1
A getResizedPreview() 0 11 1
A resize() 0 11 3
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Olivier Paroz <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * @copyright Copyright (c) 2018, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OC\Preview;
26
27
use Imagick;
28
use OCP\Files\File;
29
use OCP\Preview\IProvider2;
30
use OCP\Util;
31
32
/**
33
 * Creates a PNG preview using ImageMagick via the PECL extension
34
 *
35
 * @package OC\Preview
36
 */
37
abstract class Bitmap implements IProvider2 {
38
39
	/**
40
	 * {@inheritDoc}
41
	 */
42
	public function getThumbnail(File $file, $maxX, $maxY, $scalingUp) {
43
44
		$stream = $file->fopen('r');
45
46
		// Creates \Imagick object from bitmap or vector file
47
		try {
48
			$bp = $this->getResizedPreview($stream, $maxX, $maxY);
49
		} catch (\Exception $e) {
50
			Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), Util::ERROR);
51
			return false;
52
		}
53
54
		fclose($stream);
55
56
		//new bitmap image object
57
		$image = new \OC_Image();
58
		$image->loadFromData($bp);
59
		//check if image object is valid
60
		return $image->valid() ? $image : false;
61
	}
62
63
	/**
64
	 * @inheritdoc
65
	 */
66
	public function isAvailable(File $file) {
67
		return true;
68
	}
69
70
	/**
71
	 * Returns a preview of maxX times maxY dimensions in PNG format
72
	 *
73
	 *    * The default resolution is already 72dpi, no need to change it for a bitmap output
74
	 *    * It's possible to have proper colour conversion using profileimage().
75
	 *    ICC profiles are here: http://www.color.org/srgbprofiles.xalter
76
	 *    * It's possible to Gamma-correct an image via gammaImage()
77
	 *
78
	 * @param resource $stream the handle of the file to convert
79
	 * @param int $maxX
80
	 * @param int $maxY
81
	 *
82
	 * @return \Imagick
83
	 */
84
	private function getResizedPreview($stream, $maxX, $maxY) {
85
		$bp = new Imagick();
86
87
		$bp->readImageFile($stream);
88
89
		$bp = $this->resize($bp, $maxX, $maxY);
90
91
		$bp->setImageFormat('png');
92
93
		return $bp;
94
	}
95
96
	/**
97
	 * Returns a resized \Imagick object
98
	 *
99
	 * If you want to know more on the various methods available to resize an
100
	 * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
101
	 *
102
	 * @param \Imagick $bp
103
	 * @param int $maxX
104
	 * @param int $maxY
105
	 *
106
	 * @return \Imagick
107
	 */
108
	private function resize($bp, $maxX, $maxY) {
109
		list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
110
111
		// We only need to resize a preview which doesn't fit in the maximum dimensions
112
		if ($previewWidth > $maxX || $previewHeight > $maxY) {
113
			// TODO: LANCZOS is the default filter, CATROM could bring similar results faster
114
			$bp->resizeImage($maxX, $maxY, imagick::FILTER_LANCZOS, 1, true);
115
		}
116
117
		return $bp;
118
	}
119
120
}
121