Completed
Push — master ( c850b3...ac9323 )
by Roeland
19:22 queued 03:18
created

HEIC::getResizedPreview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @author Thomas Müller <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2018, ownCloud GmbH
7
 * @copyright Copyright (c) 2018, Sebastian Steinmetz ([email protected])
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Preview;
25
26
use OCP\ILogger;
27
28
/**
29
 * Creates a JPG preview using ImageMagick via the PECL extension
30
 *
31
 * @package OC\Preview
32
 */
33
class HEIC extends Provider {
34
	/**
35
	 * {@inheritDoc}
36
	 */
37
	public function getMimeType(): string {
38
		return '/image\/hei(f|c)/';
39
	}
40
41
	/**
42
	 * {@inheritDoc}
43
	 */
44
	public function isAvailable(\OCP\Files\FileInfo $file): bool {
45
		return in_array('HEIC', \Imagick::queryFormats("HEI*"));
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 */
51 View Code Duplication
	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
52
		$tmpPath = $fileview->toTmpFile($path);
53
		if (!$tmpPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tmpPath of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54
			return false;
55
		}
56
57
		// Creates \Imagick object from the heic file
58
		try {
59
			$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
60
			$bp->setFormat('jpg');
61
		} catch (\Exception $e) {
62
			\OC::$server->getLogger()->logException($e, [
0 ignored issues
show
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
				'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:',
64
				'level' => ILogger::ERROR,
65
				'app' => 'core',
66
			]);
67
			return false;
68
		}
69
70
		unlink($tmpPath);
71
72
		//new bitmap image object
73
		$image = new \OC_Image();
74
		$image->loadFromData($bp);
75
		//check if image object is valid
76
		return $image->valid() ? $image : false;
77
	}
78
79
	/**
80
	 * Returns a preview of maxX times maxY dimensions in JPG format
81
	 *
82
	 *    * The default resolution is already 72dpi, no need to change it for a bitmap output
83
	 *    * It's possible to have proper colour conversion using profileimage().
84
	 *    ICC profiles are here: http://www.color.org/srgbprofiles.xalter
85
	 *    * It's possible to Gamma-correct an image via gammaImage()
86
	 *
87
	 * @param string $tmpPath the location of the file to convert
88
	 * @param int $maxX
89
	 * @param int $maxY
90
	 *
91
	 * @return \Imagick
92
	 */
93 View Code Duplication
	private function getResizedPreview($tmpPath, $maxX, $maxY) {
94
		$bp = new \Imagick();
95
96
		// Layer 0 contains either the bitmap or a flat representation of all vector layers
97
		$bp->readImage($tmpPath . '[0]');
98
99
		$bp->setImageFormat('jpg');
100
101
		$bp = $this->resize($bp, $maxX, $maxY);
102
		
103
		return $bp;
104
	}
105
106
	/**
107
	 * Returns a resized \Imagick object
108
	 *
109
	 * If you want to know more on the various methods available to resize an
110
	 * image, check out this link : @link https://stackoverflow.com/questions/8517304/what-the-difference-of-sample-resample-scale-resize-adaptive-resize-thumbnail-im
111
	 *
112
	 * @param \Imagick $bp
113
	 * @param int $maxX
114
	 * @param int $maxY
115
	 *
116
	 * @return \Imagick
117
	 */
118
	private function resize($bp, $maxX, $maxY) {
119
		list($previewWidth, $previewHeight) = array_values($bp->getImageGeometry());
120
121
		// We only need to resize a preview which doesn't fit in the maximum dimensions
122
		if ($previewWidth > $maxX || $previewHeight > $maxY) {
123
			// If we want a small image (thumbnail) let's be most space- and time-efficient
124
			if ($maxX <= 500 && $maxY <= 500) {
125
				$bp->thumbnailImage($maxY, $maxX, true);
126
				$bp->stripImage();
127
			} else {
128
				// A bigger image calls for some better resizing algorithm
129
				// According to http://www.imagemagick.org/Usage/filter/#lanczos
130
				// the catrom filter is almost identical to Lanczos2, but according
131
				// to http://php.net/manual/en/imagick.resizeimage.php it is
132
				// significantly faster
133
				$bp->resizeImage($maxX, $maxY, \Imagick::FILTER_CATROM, 1, true);
134
			}
135
		}
136
137
		return $bp;
138
	}
139
140
}
141