Completed
Pull Request — master (#4620)
by Robin
13:13
created
lib/private/legacy/image.php 2 patches
Indentation   +1072 added lines, -1072 removed lines patch added patch discarded remove patch
@@ -40,565 +40,565 @@  discard block
 block discarded – undo
40 40
  * Class for basic image manipulation
41 41
  */
42 42
 class OC_Image implements \OCP\IImage {
43
-	/** @var false|resource */
44
-	protected $resource = false; // tmp resource.
45
-	/** @var int */
46
-	protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
47
-	/** @var string */
48
-	protected $mimeType = 'image/png'; // Default to png
49
-	/** @var int */
50
-	protected $bitDepth = 24;
51
-	/** @var null|string */
52
-	protected $filePath = null;
53
-	/** @var finfo */
54
-	private $fileInfo;
55
-	/** @var \OCP\ILogger */
56
-	private $logger;
57
-	/** @var \OCP\IConfig */
58
-	private $config;
59
-	/** @var array */
60
-	private $exif;
43
+    /** @var false|resource */
44
+    protected $resource = false; // tmp resource.
45
+    /** @var int */
46
+    protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident.
47
+    /** @var string */
48
+    protected $mimeType = 'image/png'; // Default to png
49
+    /** @var int */
50
+    protected $bitDepth = 24;
51
+    /** @var null|string */
52
+    protected $filePath = null;
53
+    /** @var finfo */
54
+    private $fileInfo;
55
+    /** @var \OCP\ILogger */
56
+    private $logger;
57
+    /** @var \OCP\IConfig */
58
+    private $config;
59
+    /** @var array */
60
+    private $exif;
61 61
 
62
-	/**
63
-	 * Get mime type for an image file.
64
-	 *
65
-	 * @param string|null $filePath The path to a local image file.
66
-	 * @return string The mime type if the it could be determined, otherwise an empty string.
67
-	 */
68
-	static public function getMimeTypeForFile($filePath) {
69
-		// exif_imagetype throws "read error!" if file is less than 12 byte
70
-		if ($filePath !== null && filesize($filePath) > 11) {
71
-			$imageType = exif_imagetype($filePath);
72
-		} else {
73
-			$imageType = false;
74
-		}
75
-		return $imageType ? image_type_to_mime_type($imageType) : '';
76
-	}
62
+    /**
63
+     * Get mime type for an image file.
64
+     *
65
+     * @param string|null $filePath The path to a local image file.
66
+     * @return string The mime type if the it could be determined, otherwise an empty string.
67
+     */
68
+    static public function getMimeTypeForFile($filePath) {
69
+        // exif_imagetype throws "read error!" if file is less than 12 byte
70
+        if ($filePath !== null && filesize($filePath) > 11) {
71
+            $imageType = exif_imagetype($filePath);
72
+        } else {
73
+            $imageType = false;
74
+        }
75
+        return $imageType ? image_type_to_mime_type($imageType) : '';
76
+    }
77 77
 
78
-	/**
79
-	 * Constructor.
80
-	 *
81
-	 * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
82
-	 * an imagecreate* function.
83
-	 * @param \OCP\ILogger $logger
84
-	 * @param \OCP\IConfig $config
85
-	 */
86
-	public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) {
87
-		$this->logger = $logger;
88
-		if ($logger === null) {
89
-			$this->logger = \OC::$server->getLogger();
90
-		}
91
-		$this->config = $config;
92
-		if ($config === null) {
93
-			$this->config = \OC::$server->getConfig();
94
-		}
78
+    /**
79
+     * Constructor.
80
+     *
81
+     * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
82
+     * an imagecreate* function.
83
+     * @param \OCP\ILogger $logger
84
+     * @param \OCP\IConfig $config
85
+     */
86
+    public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) {
87
+        $this->logger = $logger;
88
+        if ($logger === null) {
89
+            $this->logger = \OC::$server->getLogger();
90
+        }
91
+        $this->config = $config;
92
+        if ($config === null) {
93
+            $this->config = \OC::$server->getConfig();
94
+        }
95 95
 
96
-		if (\OC_Util::fileInfoLoaded()) {
97
-			$this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
98
-		}
96
+        if (\OC_Util::fileInfoLoaded()) {
97
+            $this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
98
+        }
99 99
 
100
-		if ($imageRef !== null) {
101
-			$this->load($imageRef);
102
-		}
103
-	}
100
+        if ($imageRef !== null) {
101
+            $this->load($imageRef);
102
+        }
103
+    }
104 104
 
105
-	/**
106
-	 * Determine whether the object contains an image resource.
107
-	 *
108
-	 * @return bool
109
-	 */
110
-	public function valid() { // apparently you can't name a method 'empty'...
111
-		return is_resource($this->resource);
112
-	}
105
+    /**
106
+     * Determine whether the object contains an image resource.
107
+     *
108
+     * @return bool
109
+     */
110
+    public function valid() { // apparently you can't name a method 'empty'...
111
+        return is_resource($this->resource);
112
+    }
113 113
 
114
-	/**
115
-	 * Returns the MIME type of the image or an empty string if no image is loaded.
116
-	 *
117
-	 * @return string
118
-	 */
119
-	public function mimeType() {
120
-		return $this->valid() ? $this->mimeType : '';
121
-	}
114
+    /**
115
+     * Returns the MIME type of the image or an empty string if no image is loaded.
116
+     *
117
+     * @return string
118
+     */
119
+    public function mimeType() {
120
+        return $this->valid() ? $this->mimeType : '';
121
+    }
122 122
 
123
-	/**
124
-	 * Returns the width of the image or -1 if no image is loaded.
125
-	 *
126
-	 * @return int
127
-	 */
128
-	public function width() {
129
-		return $this->valid() ? imagesx($this->resource) : -1;
130
-	}
123
+    /**
124
+     * Returns the width of the image or -1 if no image is loaded.
125
+     *
126
+     * @return int
127
+     */
128
+    public function width() {
129
+        return $this->valid() ? imagesx($this->resource) : -1;
130
+    }
131 131
 
132
-	/**
133
-	 * Returns the height of the image or -1 if no image is loaded.
134
-	 *
135
-	 * @return int
136
-	 */
137
-	public function height() {
138
-		return $this->valid() ? imagesy($this->resource) : -1;
139
-	}
132
+    /**
133
+     * Returns the height of the image or -1 if no image is loaded.
134
+     *
135
+     * @return int
136
+     */
137
+    public function height() {
138
+        return $this->valid() ? imagesy($this->resource) : -1;
139
+    }
140 140
 
141
-	/**
142
-	 * Returns the width when the image orientation is top-left.
143
-	 *
144
-	 * @return int
145
-	 */
146
-	public function widthTopLeft() {
147
-		$o = $this->getOrientation();
148
-		$this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core'));
149
-		switch ($o) {
150
-			case -1:
151
-			case 1:
152
-			case 2: // Not tested
153
-			case 3:
154
-			case 4: // Not tested
155
-				return $this->width();
156
-			case 5: // Not tested
157
-			case 6:
158
-			case 7: // Not tested
159
-			case 8:
160
-				return $this->height();
161
-		}
162
-		return $this->width();
163
-	}
141
+    /**
142
+     * Returns the width when the image orientation is top-left.
143
+     *
144
+     * @return int
145
+     */
146
+    public function widthTopLeft() {
147
+        $o = $this->getOrientation();
148
+        $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core'));
149
+        switch ($o) {
150
+            case -1:
151
+            case 1:
152
+            case 2: // Not tested
153
+            case 3:
154
+            case 4: // Not tested
155
+                return $this->width();
156
+            case 5: // Not tested
157
+            case 6:
158
+            case 7: // Not tested
159
+            case 8:
160
+                return $this->height();
161
+        }
162
+        return $this->width();
163
+    }
164 164
 
165
-	/**
166
-	 * Returns the height when the image orientation is top-left.
167
-	 *
168
-	 * @return int
169
-	 */
170
-	public function heightTopLeft() {
171
-		$o = $this->getOrientation();
172
-		$this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core'));
173
-		switch ($o) {
174
-			case -1:
175
-			case 1:
176
-			case 2: // Not tested
177
-			case 3:
178
-			case 4: // Not tested
179
-				return $this->height();
180
-			case 5: // Not tested
181
-			case 6:
182
-			case 7: // Not tested
183
-			case 8:
184
-				return $this->width();
185
-		}
186
-		return $this->height();
187
-	}
165
+    /**
166
+     * Returns the height when the image orientation is top-left.
167
+     *
168
+     * @return int
169
+     */
170
+    public function heightTopLeft() {
171
+        $o = $this->getOrientation();
172
+        $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core'));
173
+        switch ($o) {
174
+            case -1:
175
+            case 1:
176
+            case 2: // Not tested
177
+            case 3:
178
+            case 4: // Not tested
179
+                return $this->height();
180
+            case 5: // Not tested
181
+            case 6:
182
+            case 7: // Not tested
183
+            case 8:
184
+                return $this->width();
185
+        }
186
+        return $this->height();
187
+    }
188 188
 
189
-	/**
190
-	 * Outputs the image.
191
-	 *
192
-	 * @param string $mimeType
193
-	 * @return bool
194
-	 */
195
-	public function show($mimeType = null) {
196
-		if ($mimeType === null) {
197
-			$mimeType = $this->mimeType();
198
-		}
199
-		header('Content-Type: ' . $mimeType);
200
-		return $this->_output(null, $mimeType);
201
-	}
189
+    /**
190
+     * Outputs the image.
191
+     *
192
+     * @param string $mimeType
193
+     * @return bool
194
+     */
195
+    public function show($mimeType = null) {
196
+        if ($mimeType === null) {
197
+            $mimeType = $this->mimeType();
198
+        }
199
+        header('Content-Type: ' . $mimeType);
200
+        return $this->_output(null, $mimeType);
201
+    }
202 202
 
203
-	/**
204
-	 * Saves the image.
205
-	 *
206
-	 * @param string $filePath
207
-	 * @param string $mimeType
208
-	 * @return bool
209
-	 */
203
+    /**
204
+     * Saves the image.
205
+     *
206
+     * @param string $filePath
207
+     * @param string $mimeType
208
+     * @return bool
209
+     */
210 210
 
211
-	public function save($filePath = null, $mimeType = null) {
212
-		if ($mimeType === null) {
213
-			$mimeType = $this->mimeType();
214
-		}
215
-		if ($filePath === null) {
216
-			if ($this->filePath === null) {
217
-				$this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core'));
218
-				return false;
219
-			} else {
220
-				$filePath = $this->filePath;
221
-			}
222
-		}
223
-		return $this->_output($filePath, $mimeType);
224
-	}
211
+    public function save($filePath = null, $mimeType = null) {
212
+        if ($mimeType === null) {
213
+            $mimeType = $this->mimeType();
214
+        }
215
+        if ($filePath === null) {
216
+            if ($this->filePath === null) {
217
+                $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core'));
218
+                return false;
219
+            } else {
220
+                $filePath = $this->filePath;
221
+            }
222
+        }
223
+        return $this->_output($filePath, $mimeType);
224
+    }
225 225
 
226
-	/**
227
-	 * Outputs/saves the image.
228
-	 *
229
-	 * @param string $filePath
230
-	 * @param string $mimeType
231
-	 * @return bool
232
-	 * @throws Exception
233
-	 */
234
-	private function _output($filePath = null, $mimeType = null) {
235
-		if ($filePath) {
236
-			if (!file_exists(dirname($filePath))) {
237
-				mkdir(dirname($filePath), 0777, true);
238
-			}
239
-			$isWritable = is_writable(dirname($filePath));
240
-			if (!$isWritable) {
241
-				$this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core'));
242
-				return false;
243
-			} elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) {
244
-				$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core'));
245
-				return false;
246
-			}
247
-		}
248
-		if (!$this->valid()) {
249
-			return false;
250
-		}
226
+    /**
227
+     * Outputs/saves the image.
228
+     *
229
+     * @param string $filePath
230
+     * @param string $mimeType
231
+     * @return bool
232
+     * @throws Exception
233
+     */
234
+    private function _output($filePath = null, $mimeType = null) {
235
+        if ($filePath) {
236
+            if (!file_exists(dirname($filePath))) {
237
+                mkdir(dirname($filePath), 0777, true);
238
+            }
239
+            $isWritable = is_writable(dirname($filePath));
240
+            if (!$isWritable) {
241
+                $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core'));
242
+                return false;
243
+            } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) {
244
+                $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core'));
245
+                return false;
246
+            }
247
+        }
248
+        if (!$this->valid()) {
249
+            return false;
250
+        }
251 251
 
252
-		$imageType = $this->imageType;
253
-		if ($mimeType !== null) {
254
-			switch ($mimeType) {
255
-				case 'image/gif':
256
-					$imageType = IMAGETYPE_GIF;
257
-					break;
258
-				case 'image/jpeg':
259
-					$imageType = IMAGETYPE_JPEG;
260
-					break;
261
-				case 'image/png':
262
-					$imageType = IMAGETYPE_PNG;
263
-					break;
264
-				case 'image/x-xbitmap':
265
-					$imageType = IMAGETYPE_XBM;
266
-					break;
267
-				case 'image/bmp':
268
-				case 'image/x-ms-bmp':
269
-					$imageType = IMAGETYPE_BMP;
270
-					break;
271
-				default:
272
-					throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
273
-			}
274
-		}
252
+        $imageType = $this->imageType;
253
+        if ($mimeType !== null) {
254
+            switch ($mimeType) {
255
+                case 'image/gif':
256
+                    $imageType = IMAGETYPE_GIF;
257
+                    break;
258
+                case 'image/jpeg':
259
+                    $imageType = IMAGETYPE_JPEG;
260
+                    break;
261
+                case 'image/png':
262
+                    $imageType = IMAGETYPE_PNG;
263
+                    break;
264
+                case 'image/x-xbitmap':
265
+                    $imageType = IMAGETYPE_XBM;
266
+                    break;
267
+                case 'image/bmp':
268
+                case 'image/x-ms-bmp':
269
+                    $imageType = IMAGETYPE_BMP;
270
+                    break;
271
+                default:
272
+                    throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
273
+            }
274
+        }
275 275
 
276
-		switch ($imageType) {
277
-			case IMAGETYPE_GIF:
278
-				$retVal = imagegif($this->resource, $filePath);
279
-				break;
280
-			case IMAGETYPE_JPEG:
281
-				$retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality());
282
-				break;
283
-			case IMAGETYPE_PNG:
284
-				$retVal = imagepng($this->resource, $filePath);
285
-				break;
286
-			case IMAGETYPE_XBM:
287
-				if (function_exists('imagexbm')) {
288
-					$retVal = imagexbm($this->resource, $filePath);
289
-				} else {
290
-					throw new Exception('\OC_Image::_output(): imagexbm() is not supported.');
291
-				}
276
+        switch ($imageType) {
277
+            case IMAGETYPE_GIF:
278
+                $retVal = imagegif($this->resource, $filePath);
279
+                break;
280
+            case IMAGETYPE_JPEG:
281
+                $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality());
282
+                break;
283
+            case IMAGETYPE_PNG:
284
+                $retVal = imagepng($this->resource, $filePath);
285
+                break;
286
+            case IMAGETYPE_XBM:
287
+                if (function_exists('imagexbm')) {
288
+                    $retVal = imagexbm($this->resource, $filePath);
289
+                } else {
290
+                    throw new Exception('\OC_Image::_output(): imagexbm() is not supported.');
291
+                }
292 292
 
293
-				break;
294
-			case IMAGETYPE_WBMP:
295
-				$retVal = imagewbmp($this->resource, $filePath);
296
-				break;
297
-			case IMAGETYPE_BMP:
298
-				$retVal = imagebmp($this->resource, $filePath, $this->bitDepth);
299
-				break;
300
-			default:
301
-				$retVal = imagepng($this->resource, $filePath);
302
-		}
303
-		return $retVal;
304
-	}
293
+                break;
294
+            case IMAGETYPE_WBMP:
295
+                $retVal = imagewbmp($this->resource, $filePath);
296
+                break;
297
+            case IMAGETYPE_BMP:
298
+                $retVal = imagebmp($this->resource, $filePath, $this->bitDepth);
299
+                break;
300
+            default:
301
+                $retVal = imagepng($this->resource, $filePath);
302
+        }
303
+        return $retVal;
304
+    }
305 305
 
306
-	/**
307
-	 * Prints the image when called as $image().
308
-	 */
309
-	public function __invoke() {
310
-		return $this->show();
311
-	}
306
+    /**
307
+     * Prints the image when called as $image().
308
+     */
309
+    public function __invoke() {
310
+        return $this->show();
311
+    }
312 312
 
313
-	/**
314
-	 * @return resource Returns the image resource in any.
315
-	 */
316
-	public function resource() {
317
-		return $this->resource;
318
-	}
313
+    /**
314
+     * @return resource Returns the image resource in any.
315
+     */
316
+    public function resource() {
317
+        return $this->resource;
318
+    }
319 319
 
320
-	/**
321
-	 * @return null|string Returns the raw image data.
322
-	 */
323
-	public function data() {
324
-		if (!$this->valid()) {
325
-			return null;
326
-		}
327
-		ob_start();
328
-		switch ($this->mimeType) {
329
-			case "image/png":
330
-				$res = imagepng($this->resource);
331
-				break;
332
-			case "image/jpeg":
333
-				$quality = $this->getJpegQuality();
334
-				if ($quality !== null) {
335
-					$res = imagejpeg($this->resource, null, $quality);
336
-				} else {
337
-					$res = imagejpeg($this->resource);
338
-				}
339
-				break;
340
-			case "image/gif":
341
-				$res = imagegif($this->resource);
342
-				break;
343
-			default:
344
-				$res = imagepng($this->resource);
345
-				$this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core'));
346
-				break;
347
-		}
348
-		if (!$res) {
349
-			$this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core'));
350
-		}
351
-		return ob_get_clean();
352
-	}
320
+    /**
321
+     * @return null|string Returns the raw image data.
322
+     */
323
+    public function data() {
324
+        if (!$this->valid()) {
325
+            return null;
326
+        }
327
+        ob_start();
328
+        switch ($this->mimeType) {
329
+            case "image/png":
330
+                $res = imagepng($this->resource);
331
+                break;
332
+            case "image/jpeg":
333
+                $quality = $this->getJpegQuality();
334
+                if ($quality !== null) {
335
+                    $res = imagejpeg($this->resource, null, $quality);
336
+                } else {
337
+                    $res = imagejpeg($this->resource);
338
+                }
339
+                break;
340
+            case "image/gif":
341
+                $res = imagegif($this->resource);
342
+                break;
343
+            default:
344
+                $res = imagepng($this->resource);
345
+                $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core'));
346
+                break;
347
+        }
348
+        if (!$res) {
349
+            $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core'));
350
+        }
351
+        return ob_get_clean();
352
+    }
353 353
 
354
-	/**
355
-	 * @return string - base64 encoded, which is suitable for embedding in a VCard.
356
-	 */
357
-	public function __toString() {
358
-		return base64_encode($this->data());
359
-	}
354
+    /**
355
+     * @return string - base64 encoded, which is suitable for embedding in a VCard.
356
+     */
357
+    public function __toString() {
358
+        return base64_encode($this->data());
359
+    }
360 360
 
361
-	/**
362
-	 * @return int|null
363
-	 */
364
-	protected function getJpegQuality() {
365
-		$quality = $this->config->getAppValue('preview', 'jpeg_quality', 90);
366
-		if ($quality !== null) {
367
-			$quality = min(100, max(10, (int) $quality));
368
-		}
369
-		return $quality;
370
-	}
361
+    /**
362
+     * @return int|null
363
+     */
364
+    protected function getJpegQuality() {
365
+        $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90);
366
+        if ($quality !== null) {
367
+            $quality = min(100, max(10, (int) $quality));
368
+        }
369
+        return $quality;
370
+    }
371 371
 
372
-	/**
373
-	 * (I'm open for suggestions on better method name ;)
374
-	 * Get the orientation based on EXIF data.
375
-	 *
376
-	 * @return int The orientation or -1 if no EXIF data is available.
377
-	 */
378
-	public function getOrientation() {
379
-		if ($this->exif !== null) {
380
-			return $this->exif['Orientation'];
381
-		}
372
+    /**
373
+     * (I'm open for suggestions on better method name ;)
374
+     * Get the orientation based on EXIF data.
375
+     *
376
+     * @return int The orientation or -1 if no EXIF data is available.
377
+     */
378
+    public function getOrientation() {
379
+        if ($this->exif !== null) {
380
+            return $this->exif['Orientation'];
381
+        }
382 382
 
383
-		if ($this->imageType !== IMAGETYPE_JPEG) {
384
-			$this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core'));
385
-			return -1;
386
-		}
387
-		if (!is_callable('exif_read_data')) {
388
-			$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
389
-			return -1;
390
-		}
391
-		if (!$this->valid()) {
392
-			$this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
393
-			return -1;
394
-		}
395
-		if (is_null($this->filePath) || !is_readable($this->filePath)) {
396
-			$this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core'));
397
-			return -1;
398
-		}
399
-		$exif = @exif_read_data($this->filePath, 'IFD0');
400
-		if (!$exif) {
401
-			return -1;
402
-		}
403
-		if (!isset($exif['Orientation'])) {
404
-			return -1;
405
-		}
406
-		$this->exif = $exif;
407
-		return $exif['Orientation'];
408
-	}
383
+        if ($this->imageType !== IMAGETYPE_JPEG) {
384
+            $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core'));
385
+            return -1;
386
+        }
387
+        if (!is_callable('exif_read_data')) {
388
+            $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
389
+            return -1;
390
+        }
391
+        if (!$this->valid()) {
392
+            $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
393
+            return -1;
394
+        }
395
+        if (is_null($this->filePath) || !is_readable($this->filePath)) {
396
+            $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core'));
397
+            return -1;
398
+        }
399
+        $exif = @exif_read_data($this->filePath, 'IFD0');
400
+        if (!$exif) {
401
+            return -1;
402
+        }
403
+        if (!isset($exif['Orientation'])) {
404
+            return -1;
405
+        }
406
+        $this->exif = $exif;
407
+        return $exif['Orientation'];
408
+    }
409 409
 
410
-	public function readExif($data) {
411
-		if (!is_callable('exif_read_data')) {
412
-			$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
413
-			return;
414
-		}
415
-		if (!$this->valid()) {
416
-			$this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
417
-			return;
418
-		}
410
+    public function readExif($data) {
411
+        if (!is_callable('exif_read_data')) {
412
+            $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
413
+            return;
414
+        }
415
+        if (!$this->valid()) {
416
+            $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
417
+            return;
418
+        }
419 419
 
420
-		$exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data));
421
-		if (!$exif) {
422
-			return;
423
-		}
424
-		if (!isset($exif['Orientation'])) {
425
-			return;
426
-		}
427
-		$this->exif = $exif;
428
-	}
420
+        $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data));
421
+        if (!$exif) {
422
+            return;
423
+        }
424
+        if (!isset($exif['Orientation'])) {
425
+            return;
426
+        }
427
+        $this->exif = $exif;
428
+    }
429 429
 
430
-	/**
431
-	 * (I'm open for suggestions on better method name ;)
432
-	 * Fixes orientation based on EXIF data.
433
-	 *
434
-	 * @return bool.
435
-	 */
436
-	public function fixOrientation() {
437
-		$o = $this->getOrientation();
438
-		$this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core'));
439
-		$rotate = 0;
440
-		$flip = false;
441
-		switch ($o) {
442
-			case -1:
443
-				return false; //Nothing to fix
444
-			case 1:
445
-				$rotate = 0;
446
-				break;
447
-			case 2:
448
-				$rotate = 0;
449
-				$flip = true;
450
-				break;
451
-			case 3:
452
-				$rotate = 180;
453
-				break;
454
-			case 4:
455
-				$rotate = 180;
456
-				$flip = true;
457
-				break;
458
-			case 5:
459
-				$rotate = 90;
460
-				$flip = true;
461
-				break;
462
-			case 6:
463
-				$rotate = 270;
464
-				break;
465
-			case 7:
466
-				$rotate = 270;
467
-				$flip = true;
468
-				break;
469
-			case 8:
470
-				$rotate = 90;
471
-				break;
472
-		}
473
-		if($flip && function_exists('imageflip')) {
474
-			imageflip($this->resource, IMG_FLIP_HORIZONTAL);
475
-		}
476
-		if ($rotate) {
477
-			$res = imagerotate($this->resource, $rotate, 0);
478
-			if ($res) {
479
-				if (imagealphablending($res, true)) {
480
-					if (imagesavealpha($res, true)) {
481
-						imagedestroy($this->resource);
482
-						$this->resource = $res;
483
-						return true;
484
-					} else {
485
-						$this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core'));
486
-						return false;
487
-					}
488
-				} else {
489
-					$this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core'));
490
-					return false;
491
-				}
492
-			} else {
493
-				$this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core'));
494
-				return false;
495
-			}
496
-		}
497
-		return false;
498
-	}
430
+    /**
431
+     * (I'm open for suggestions on better method name ;)
432
+     * Fixes orientation based on EXIF data.
433
+     *
434
+     * @return bool.
435
+     */
436
+    public function fixOrientation() {
437
+        $o = $this->getOrientation();
438
+        $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core'));
439
+        $rotate = 0;
440
+        $flip = false;
441
+        switch ($o) {
442
+            case -1:
443
+                return false; //Nothing to fix
444
+            case 1:
445
+                $rotate = 0;
446
+                break;
447
+            case 2:
448
+                $rotate = 0;
449
+                $flip = true;
450
+                break;
451
+            case 3:
452
+                $rotate = 180;
453
+                break;
454
+            case 4:
455
+                $rotate = 180;
456
+                $flip = true;
457
+                break;
458
+            case 5:
459
+                $rotate = 90;
460
+                $flip = true;
461
+                break;
462
+            case 6:
463
+                $rotate = 270;
464
+                break;
465
+            case 7:
466
+                $rotate = 270;
467
+                $flip = true;
468
+                break;
469
+            case 8:
470
+                $rotate = 90;
471
+                break;
472
+        }
473
+        if($flip && function_exists('imageflip')) {
474
+            imageflip($this->resource, IMG_FLIP_HORIZONTAL);
475
+        }
476
+        if ($rotate) {
477
+            $res = imagerotate($this->resource, $rotate, 0);
478
+            if ($res) {
479
+                if (imagealphablending($res, true)) {
480
+                    if (imagesavealpha($res, true)) {
481
+                        imagedestroy($this->resource);
482
+                        $this->resource = $res;
483
+                        return true;
484
+                    } else {
485
+                        $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core'));
486
+                        return false;
487
+                    }
488
+                } else {
489
+                    $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core'));
490
+                    return false;
491
+                }
492
+            } else {
493
+                $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core'));
494
+                return false;
495
+            }
496
+        }
497
+        return false;
498
+    }
499 499
 
500
-	/**
501
-	 * Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
502
-	 *
503
-	 * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle    ).
504
-	 * @return resource|false An image resource or false on error
505
-	 */
506
-	public function load($imageRef) {
507
-		if (is_resource($imageRef)) {
508
-			if (get_resource_type($imageRef) === 'gd') {
509
-				$this->resource = $imageRef;
510
-				return $this->resource;
511
-			} elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) {
512
-				return $this->loadFromFileHandle($imageRef);
513
-			}
514
-		} elseif ($this->loadFromBase64($imageRef) !== false) {
515
-			return $this->resource;
516
-		} elseif ($this->loadFromFile($imageRef) !== false) {
517
-			return $this->resource;
518
-		} elseif ($this->loadFromData($imageRef) !== false) {
519
-			return $this->resource;
520
-		}
521
-		$this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
522
-		return false;
523
-	}
500
+    /**
501
+     * Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
502
+     *
503
+     * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle    ).
504
+     * @return resource|false An image resource or false on error
505
+     */
506
+    public function load($imageRef) {
507
+        if (is_resource($imageRef)) {
508
+            if (get_resource_type($imageRef) === 'gd') {
509
+                $this->resource = $imageRef;
510
+                return $this->resource;
511
+            } elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) {
512
+                return $this->loadFromFileHandle($imageRef);
513
+            }
514
+        } elseif ($this->loadFromBase64($imageRef) !== false) {
515
+            return $this->resource;
516
+        } elseif ($this->loadFromFile($imageRef) !== false) {
517
+            return $this->resource;
518
+        } elseif ($this->loadFromData($imageRef) !== false) {
519
+            return $this->resource;
520
+        }
521
+        $this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
522
+        return false;
523
+    }
524 524
 
525
-	/**
526
-	 * Loads an image from an open file handle.
527
-	 * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
528
-	 *
529
-	 * @param resource $handle
530
-	 * @return resource|false An image resource or false on error
531
-	 */
532
-	public function loadFromFileHandle($handle) {
533
-		$contents = stream_get_contents($handle);
534
-		if ($this->loadFromData($contents)) {
535
-			return $this->resource;
536
-		}
537
-		return false;
538
-	}
525
+    /**
526
+     * Loads an image from an open file handle.
527
+     * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
528
+     *
529
+     * @param resource $handle
530
+     * @return resource|false An image resource or false on error
531
+     */
532
+    public function loadFromFileHandle($handle) {
533
+        $contents = stream_get_contents($handle);
534
+        if ($this->loadFromData($contents)) {
535
+            return $this->resource;
536
+        }
537
+        return false;
538
+    }
539 539
 
540
-	/**
541
-	 * Loads an image from a local file.
542
-	 *
543
-	 * @param bool|string $imagePath The path to a local file.
544
-	 * @return bool|resource An image resource or false on error
545
-	 */
546
-	public function loadFromFile($imagePath = false) {
547
-		// exif_imagetype throws "read error!" if file is less than 12 byte
548
-		if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
549
-			return false;
550
-		}
551
-		$iType = exif_imagetype($imagePath);
552
-		switch ($iType) {
553
-			case IMAGETYPE_GIF:
554
-				if (imagetypes() & IMG_GIF) {
555
-					$this->resource = imagecreatefromgif($imagePath);
556
-					// Preserve transparency
557
-					imagealphablending($this->resource, true);
558
-					imagesavealpha($this->resource, true);
559
-				} else {
560
-					$this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
561
-				}
562
-				break;
563
-			case IMAGETYPE_JPEG:
564
-				if (imagetypes() & IMG_JPG) {
565
-					if (getimagesize($imagePath) !== false) {
566
-						$this->resource = @imagecreatefromjpeg($imagePath);
567
-					} else {
568
-						$this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core'));
569
-					}
570
-				} else {
571
-					$this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
572
-				}
573
-				break;
574
-			case IMAGETYPE_PNG:
575
-				if (imagetypes() & IMG_PNG) {
576
-					$this->resource = @imagecreatefrompng($imagePath);
577
-					// Preserve transparency
578
-					imagealphablending($this->resource, true);
579
-					imagesavealpha($this->resource, true);
580
-				} else {
581
-					$this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
582
-				}
583
-				break;
584
-			case IMAGETYPE_XBM:
585
-				if (imagetypes() & IMG_XPM) {
586
-					$this->resource = @imagecreatefromxbm($imagePath);
587
-				} else {
588
-					$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
589
-				}
590
-				break;
591
-			case IMAGETYPE_WBMP:
592
-				if (imagetypes() & IMG_WBMP) {
593
-					$this->resource = @imagecreatefromwbmp($imagePath);
594
-				} else {
595
-					$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
596
-				}
597
-				break;
598
-			case IMAGETYPE_BMP:
599
-				$this->resource = $this->imagecreatefrombmp($imagePath);
600
-				break;
601
-			/*
540
+    /**
541
+     * Loads an image from a local file.
542
+     *
543
+     * @param bool|string $imagePath The path to a local file.
544
+     * @return bool|resource An image resource or false on error
545
+     */
546
+    public function loadFromFile($imagePath = false) {
547
+        // exif_imagetype throws "read error!" if file is less than 12 byte
548
+        if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
549
+            return false;
550
+        }
551
+        $iType = exif_imagetype($imagePath);
552
+        switch ($iType) {
553
+            case IMAGETYPE_GIF:
554
+                if (imagetypes() & IMG_GIF) {
555
+                    $this->resource = imagecreatefromgif($imagePath);
556
+                    // Preserve transparency
557
+                    imagealphablending($this->resource, true);
558
+                    imagesavealpha($this->resource, true);
559
+                } else {
560
+                    $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
561
+                }
562
+                break;
563
+            case IMAGETYPE_JPEG:
564
+                if (imagetypes() & IMG_JPG) {
565
+                    if (getimagesize($imagePath) !== false) {
566
+                        $this->resource = @imagecreatefromjpeg($imagePath);
567
+                    } else {
568
+                        $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core'));
569
+                    }
570
+                } else {
571
+                    $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
572
+                }
573
+                break;
574
+            case IMAGETYPE_PNG:
575
+                if (imagetypes() & IMG_PNG) {
576
+                    $this->resource = @imagecreatefrompng($imagePath);
577
+                    // Preserve transparency
578
+                    imagealphablending($this->resource, true);
579
+                    imagesavealpha($this->resource, true);
580
+                } else {
581
+                    $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
582
+                }
583
+                break;
584
+            case IMAGETYPE_XBM:
585
+                if (imagetypes() & IMG_XPM) {
586
+                    $this->resource = @imagecreatefromxbm($imagePath);
587
+                } else {
588
+                    $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
589
+                }
590
+                break;
591
+            case IMAGETYPE_WBMP:
592
+                if (imagetypes() & IMG_WBMP) {
593
+                    $this->resource = @imagecreatefromwbmp($imagePath);
594
+                } else {
595
+                    $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
596
+                }
597
+                break;
598
+            case IMAGETYPE_BMP:
599
+                $this->resource = $this->imagecreatefrombmp($imagePath);
600
+                break;
601
+            /*
602 602
 			case IMAGETYPE_TIFF_II: // (intel byte order)
603 603
 				break;
604 604
 			case IMAGETYPE_TIFF_MM: // (motorola byte order)
@@ -622,581 +622,581 @@  discard block
 block discarded – undo
622 622
 			case IMAGETYPE_PSD:
623 623
 				break;
624 624
 			*/
625
-			default:
625
+            default:
626 626
 
627
-				// this is mostly file created from encrypted file
628
-				$this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
629
-				$iType = IMAGETYPE_PNG;
630
-				$this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
631
-				break;
632
-		}
633
-		if ($this->valid()) {
634
-			$this->imageType = $iType;
635
-			$this->mimeType = image_type_to_mime_type($iType);
636
-			$this->filePath = $imagePath;
637
-		}
638
-		return $this->resource;
639
-	}
627
+                // this is mostly file created from encrypted file
628
+                $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
629
+                $iType = IMAGETYPE_PNG;
630
+                $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
631
+                break;
632
+        }
633
+        if ($this->valid()) {
634
+            $this->imageType = $iType;
635
+            $this->mimeType = image_type_to_mime_type($iType);
636
+            $this->filePath = $imagePath;
637
+        }
638
+        return $this->resource;
639
+    }
640 640
 
641
-	/**
642
-	 * Loads an image from a string of data.
643
-	 *
644
-	 * @param string $str A string of image data as read from a file.
645
-	 * @return bool|resource An image resource or false on error
646
-	 */
647
-	public function loadFromData($str) {
648
-		if (is_resource($str)) {
649
-			return false;
650
-		}
651
-		$this->resource = @imagecreatefromstring($str);
652
-		if ($this->fileInfo) {
653
-			$this->mimeType = $this->fileInfo->buffer($str);
654
-		}
655
-		if (is_resource($this->resource)) {
656
-			imagealphablending($this->resource, false);
657
-			imagesavealpha($this->resource, true);
658
-		}
641
+    /**
642
+     * Loads an image from a string of data.
643
+     *
644
+     * @param string $str A string of image data as read from a file.
645
+     * @return bool|resource An image resource or false on error
646
+     */
647
+    public function loadFromData($str) {
648
+        if (is_resource($str)) {
649
+            return false;
650
+        }
651
+        $this->resource = @imagecreatefromstring($str);
652
+        if ($this->fileInfo) {
653
+            $this->mimeType = $this->fileInfo->buffer($str);
654
+        }
655
+        if (is_resource($this->resource)) {
656
+            imagealphablending($this->resource, false);
657
+            imagesavealpha($this->resource, true);
658
+        }
659 659
 
660
-		if (!$this->resource) {
661
-			$this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core'));
662
-			return false;
663
-		}
664
-		return $this->resource;
665
-	}
660
+        if (!$this->resource) {
661
+            $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core'));
662
+            return false;
663
+        }
664
+        return $this->resource;
665
+    }
666 666
 
667
-	/**
668
-	 * Loads an image from a base64 encoded string.
669
-	 *
670
-	 * @param string $str A string base64 encoded string of image data.
671
-	 * @return bool|resource An image resource or false on error
672
-	 */
673
-	public function loadFromBase64($str) {
674
-		if (!is_string($str)) {
675
-			return false;
676
-		}
677
-		$data = base64_decode($str);
678
-		if ($data) { // try to load from string data
679
-			$this->resource = @imagecreatefromstring($data);
680
-			if ($this->fileInfo) {
681
-				$this->mimeType = $this->fileInfo->buffer($data);
682
-			}
683
-			if (!$this->resource) {
684
-				$this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core'));
685
-				return false;
686
-			}
687
-			return $this->resource;
688
-		} else {
689
-			return false;
690
-		}
691
-	}
667
+    /**
668
+     * Loads an image from a base64 encoded string.
669
+     *
670
+     * @param string $str A string base64 encoded string of image data.
671
+     * @return bool|resource An image resource or false on error
672
+     */
673
+    public function loadFromBase64($str) {
674
+        if (!is_string($str)) {
675
+            return false;
676
+        }
677
+        $data = base64_decode($str);
678
+        if ($data) { // try to load from string data
679
+            $this->resource = @imagecreatefromstring($data);
680
+            if ($this->fileInfo) {
681
+                $this->mimeType = $this->fileInfo->buffer($data);
682
+            }
683
+            if (!$this->resource) {
684
+                $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core'));
685
+                return false;
686
+            }
687
+            return $this->resource;
688
+        } else {
689
+            return false;
690
+        }
691
+    }
692 692
 
693
-	/**
694
-	 * Create a new image from file or URL
695
-	 *
696
-	 * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm
697
-	 * @version 1.00
698
-	 * @param string $fileName <p>
699
-	 * Path to the BMP image.
700
-	 * </p>
701
-	 * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors.
702
-	 */
703
-	private function imagecreatefrombmp($fileName) {
704
-		if (!($fh = fopen($fileName, 'rb'))) {
705
-			$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core'));
706
-			return false;
707
-		}
708
-		// read file header
709
-		$meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14));
710
-		// check for bitmap
711
-		if ($meta['type'] != 19778) {
712
-			fclose($fh);
713
-			$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
714
-			return false;
715
-		}
716
-		// read image header
717
-		$meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40));
718
-		// read additional 16bit header
719
-		if ($meta['bits'] == 16) {
720
-			$meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12));
721
-		}
722
-		// set bytes and padding
723
-		$meta['bytes'] = $meta['bits'] / 8;
724
-		$this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call
725
-		$meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4)));
726
-		if ($meta['decal'] == 4) {
727
-			$meta['decal'] = 0;
728
-		}
729
-		// obtain imagesize
730
-		if ($meta['imagesize'] < 1) {
731
-			$meta['imagesize'] = $meta['filesize'] - $meta['offset'];
732
-			// in rare cases filesize is equal to offset so we need to read physical size
733
-			if ($meta['imagesize'] < 1) {
734
-				$meta['imagesize'] = @filesize($fileName) - $meta['offset'];
735
-				if ($meta['imagesize'] < 1) {
736
-					fclose($fh);
737
-					$this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
738
-					return false;
739
-				}
740
-			}
741
-		}
742
-		// calculate colors
743
-		$meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors'];
744
-		// read color palette
745
-		$palette = array();
746
-		if ($meta['bits'] < 16) {
747
-			$palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4));
748
-			// in rare cases the color value is signed
749
-			if ($palette[1] < 0) {
750
-				foreach ($palette as $i => $color) {
751
-					$palette[$i] = $color + 16777216;
752
-				}
753
-			}
754
-		}
755
-		// create gd image
756
-		$im = imagecreatetruecolor($meta['width'], $meta['height']);
757
-		if ($im == false) {
758
-			fclose($fh);
759
-			$this->logger->warning(
760
-				'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'],
761
-				array('app' => 'core'));
762
-			return false;
763
-		}
693
+    /**
694
+     * Create a new image from file or URL
695
+     *
696
+     * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm
697
+     * @version 1.00
698
+     * @param string $fileName <p>
699
+     * Path to the BMP image.
700
+     * </p>
701
+     * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors.
702
+     */
703
+    private function imagecreatefrombmp($fileName) {
704
+        if (!($fh = fopen($fileName, 'rb'))) {
705
+            $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core'));
706
+            return false;
707
+        }
708
+        // read file header
709
+        $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14));
710
+        // check for bitmap
711
+        if ($meta['type'] != 19778) {
712
+            fclose($fh);
713
+            $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
714
+            return false;
715
+        }
716
+        // read image header
717
+        $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40));
718
+        // read additional 16bit header
719
+        if ($meta['bits'] == 16) {
720
+            $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12));
721
+        }
722
+        // set bytes and padding
723
+        $meta['bytes'] = $meta['bits'] / 8;
724
+        $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call
725
+        $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4)));
726
+        if ($meta['decal'] == 4) {
727
+            $meta['decal'] = 0;
728
+        }
729
+        // obtain imagesize
730
+        if ($meta['imagesize'] < 1) {
731
+            $meta['imagesize'] = $meta['filesize'] - $meta['offset'];
732
+            // in rare cases filesize is equal to offset so we need to read physical size
733
+            if ($meta['imagesize'] < 1) {
734
+                $meta['imagesize'] = @filesize($fileName) - $meta['offset'];
735
+                if ($meta['imagesize'] < 1) {
736
+                    fclose($fh);
737
+                    $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
738
+                    return false;
739
+                }
740
+            }
741
+        }
742
+        // calculate colors
743
+        $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors'];
744
+        // read color palette
745
+        $palette = array();
746
+        if ($meta['bits'] < 16) {
747
+            $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4));
748
+            // in rare cases the color value is signed
749
+            if ($palette[1] < 0) {
750
+                foreach ($palette as $i => $color) {
751
+                    $palette[$i] = $color + 16777216;
752
+                }
753
+            }
754
+        }
755
+        // create gd image
756
+        $im = imagecreatetruecolor($meta['width'], $meta['height']);
757
+        if ($im == false) {
758
+            fclose($fh);
759
+            $this->logger->warning(
760
+                'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'],
761
+                array('app' => 'core'));
762
+            return false;
763
+        }
764 764
 
765
-		$data = fread($fh, $meta['imagesize']);
766
-		$p = 0;
767
-		$vide = chr(0);
768
-		$y = $meta['height'] - 1;
769
-		$error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!';
770
-		// loop through the image data beginning with the lower left corner
771
-		while ($y >= 0) {
772
-			$x = 0;
773
-			while ($x < $meta['width']) {
774
-				switch ($meta['bits']) {
775
-					case 32:
776
-					case 24:
777
-						if (!($part = substr($data, $p, 3))) {
778
-							$this->logger->warning($error, array('app' => 'core'));
779
-							return $im;
780
-						}
781
-						$color = @unpack('V', $part . $vide);
782
-						break;
783
-					case 16:
784
-						if (!($part = substr($data, $p, 2))) {
785
-							fclose($fh);
786
-							$this->logger->warning($error, array('app' => 'core'));
787
-							return $im;
788
-						}
789
-						$color = @unpack('v', $part);
790
-						$color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3);
791
-						break;
792
-					case 8:
793
-						$color = @unpack('n', $vide . substr($data, $p, 1));
794
-						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
795
-						break;
796
-					case 4:
797
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
798
-						$color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
799
-						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
800
-						break;
801
-					case 1:
802
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
803
-						switch (($p * 8) % 8) {
804
-							case 0:
805
-								$color[1] = $color[1] >> 7;
806
-								break;
807
-							case 1:
808
-								$color[1] = ($color[1] & 0x40) >> 6;
809
-								break;
810
-							case 2:
811
-								$color[1] = ($color[1] & 0x20) >> 5;
812
-								break;
813
-							case 3:
814
-								$color[1] = ($color[1] & 0x10) >> 4;
815
-								break;
816
-							case 4:
817
-								$color[1] = ($color[1] & 0x8) >> 3;
818
-								break;
819
-							case 5:
820
-								$color[1] = ($color[1] & 0x4) >> 2;
821
-								break;
822
-							case 6:
823
-								$color[1] = ($color[1] & 0x2) >> 1;
824
-								break;
825
-							case 7:
826
-								$color[1] = ($color[1] & 0x1);
827
-								break;
828
-						}
829
-						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
830
-						break;
831
-					default:
832
-						fclose($fh);
833
-						$this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core'));
834
-						return false;
835
-				}
836
-				imagesetpixel($im, $x, $y, $color[1]);
837
-				$x++;
838
-				$p += $meta['bytes'];
839
-			}
840
-			$y--;
841
-			$p += $meta['decal'];
842
-		}
843
-		fclose($fh);
844
-		return $im;
845
-	}
765
+        $data = fread($fh, $meta['imagesize']);
766
+        $p = 0;
767
+        $vide = chr(0);
768
+        $y = $meta['height'] - 1;
769
+        $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!';
770
+        // loop through the image data beginning with the lower left corner
771
+        while ($y >= 0) {
772
+            $x = 0;
773
+            while ($x < $meta['width']) {
774
+                switch ($meta['bits']) {
775
+                    case 32:
776
+                    case 24:
777
+                        if (!($part = substr($data, $p, 3))) {
778
+                            $this->logger->warning($error, array('app' => 'core'));
779
+                            return $im;
780
+                        }
781
+                        $color = @unpack('V', $part . $vide);
782
+                        break;
783
+                    case 16:
784
+                        if (!($part = substr($data, $p, 2))) {
785
+                            fclose($fh);
786
+                            $this->logger->warning($error, array('app' => 'core'));
787
+                            return $im;
788
+                        }
789
+                        $color = @unpack('v', $part);
790
+                        $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3);
791
+                        break;
792
+                    case 8:
793
+                        $color = @unpack('n', $vide . substr($data, $p, 1));
794
+                        $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
795
+                        break;
796
+                    case 4:
797
+                        $color = @unpack('n', $vide . substr($data, floor($p), 1));
798
+                        $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
799
+                        $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
800
+                        break;
801
+                    case 1:
802
+                        $color = @unpack('n', $vide . substr($data, floor($p), 1));
803
+                        switch (($p * 8) % 8) {
804
+                            case 0:
805
+                                $color[1] = $color[1] >> 7;
806
+                                break;
807
+                            case 1:
808
+                                $color[1] = ($color[1] & 0x40) >> 6;
809
+                                break;
810
+                            case 2:
811
+                                $color[1] = ($color[1] & 0x20) >> 5;
812
+                                break;
813
+                            case 3:
814
+                                $color[1] = ($color[1] & 0x10) >> 4;
815
+                                break;
816
+                            case 4:
817
+                                $color[1] = ($color[1] & 0x8) >> 3;
818
+                                break;
819
+                            case 5:
820
+                                $color[1] = ($color[1] & 0x4) >> 2;
821
+                                break;
822
+                            case 6:
823
+                                $color[1] = ($color[1] & 0x2) >> 1;
824
+                                break;
825
+                            case 7:
826
+                                $color[1] = ($color[1] & 0x1);
827
+                                break;
828
+                        }
829
+                        $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
830
+                        break;
831
+                    default:
832
+                        fclose($fh);
833
+                        $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core'));
834
+                        return false;
835
+                }
836
+                imagesetpixel($im, $x, $y, $color[1]);
837
+                $x++;
838
+                $p += $meta['bytes'];
839
+            }
840
+            $y--;
841
+            $p += $meta['decal'];
842
+        }
843
+        fclose($fh);
844
+        return $im;
845
+    }
846 846
 
847
-	/**
848
-	 * Resizes the image preserving ratio.
849
-	 *
850
-	 * @param integer $maxSize The maximum size of either the width or height.
851
-	 * @return bool
852
-	 */
853
-	public function resize($maxSize) {
854
-		if (!$this->valid()) {
855
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
856
-			return false;
857
-		}
858
-		$widthOrig = imagesx($this->resource);
859
-		$heightOrig = imagesy($this->resource);
860
-		$ratioOrig = $widthOrig / $heightOrig;
847
+    /**
848
+     * Resizes the image preserving ratio.
849
+     *
850
+     * @param integer $maxSize The maximum size of either the width or height.
851
+     * @return bool
852
+     */
853
+    public function resize($maxSize) {
854
+        if (!$this->valid()) {
855
+            $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
856
+            return false;
857
+        }
858
+        $widthOrig = imagesx($this->resource);
859
+        $heightOrig = imagesy($this->resource);
860
+        $ratioOrig = $widthOrig / $heightOrig;
861 861
 
862
-		if ($ratioOrig > 1) {
863
-			$newHeight = round($maxSize / $ratioOrig);
864
-			$newWidth = $maxSize;
865
-		} else {
866
-			$newWidth = round($maxSize * $ratioOrig);
867
-			$newHeight = $maxSize;
868
-		}
862
+        if ($ratioOrig > 1) {
863
+            $newHeight = round($maxSize / $ratioOrig);
864
+            $newWidth = $maxSize;
865
+        } else {
866
+            $newWidth = round($maxSize * $ratioOrig);
867
+            $newHeight = $maxSize;
868
+        }
869 869
 
870
-		$this->preciseResize(round($newWidth), round($newHeight));
871
-		return true;
872
-	}
870
+        $this->preciseResize(round($newWidth), round($newHeight));
871
+        return true;
872
+    }
873 873
 
874
-	/**
875
-	 * @param int $width
876
-	 * @param int $height
877
-	 * @return bool
878
-	 */
879
-	public function preciseResize($width, $height) {
880
-		if (!$this->valid()) {
881
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
882
-			return false;
883
-		}
884
-		$widthOrig = imagesx($this->resource);
885
-		$heightOrig = imagesy($this->resource);
886
-		$process = imagecreatetruecolor($width, $height);
874
+    /**
875
+     * @param int $width
876
+     * @param int $height
877
+     * @return bool
878
+     */
879
+    public function preciseResize($width, $height) {
880
+        if (!$this->valid()) {
881
+            $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
882
+            return false;
883
+        }
884
+        $widthOrig = imagesx($this->resource);
885
+        $heightOrig = imagesy($this->resource);
886
+        $process = imagecreatetruecolor($width, $height);
887 887
 
888
-		if ($process == false) {
889
-			$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
890
-			imagedestroy($process);
891
-			return false;
892
-		}
888
+        if ($process == false) {
889
+            $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
890
+            imagedestroy($process);
891
+            return false;
892
+        }
893 893
 
894
-		// preserve transparency
895
-		if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
896
-			imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
897
-			imagealphablending($process, false);
898
-			imagesavealpha($process, true);
899
-		}
894
+        // preserve transparency
895
+        if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
896
+            imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
897
+            imagealphablending($process, false);
898
+            imagesavealpha($process, true);
899
+        }
900 900
 
901
-		imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
902
-		if ($process == false) {
903
-			$this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core'));
904
-			imagedestroy($process);
905
-			return false;
906
-		}
907
-		imagedestroy($this->resource);
908
-		$this->resource = $process;
909
-		return true;
910
-	}
901
+        imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
902
+        if ($process == false) {
903
+            $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core'));
904
+            imagedestroy($process);
905
+            return false;
906
+        }
907
+        imagedestroy($this->resource);
908
+        $this->resource = $process;
909
+        return true;
910
+    }
911 911
 
912
-	/**
913
-	 * Crops the image to the middle square. If the image is already square it just returns.
914
-	 *
915
-	 * @param int $size maximum size for the result (optional)
916
-	 * @return bool for success or failure
917
-	 */
918
-	public function centerCrop($size = 0) {
919
-		if (!$this->valid()) {
920
-			$this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core'));
921
-			return false;
922
-		}
923
-		$widthOrig = imagesx($this->resource);
924
-		$heightOrig = imagesy($this->resource);
925
-		if ($widthOrig === $heightOrig and $size == 0) {
926
-			return true;
927
-		}
928
-		$ratioOrig = $widthOrig / $heightOrig;
929
-		$width = $height = min($widthOrig, $heightOrig);
912
+    /**
913
+     * Crops the image to the middle square. If the image is already square it just returns.
914
+     *
915
+     * @param int $size maximum size for the result (optional)
916
+     * @return bool for success or failure
917
+     */
918
+    public function centerCrop($size = 0) {
919
+        if (!$this->valid()) {
920
+            $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core'));
921
+            return false;
922
+        }
923
+        $widthOrig = imagesx($this->resource);
924
+        $heightOrig = imagesy($this->resource);
925
+        if ($widthOrig === $heightOrig and $size == 0) {
926
+            return true;
927
+        }
928
+        $ratioOrig = $widthOrig / $heightOrig;
929
+        $width = $height = min($widthOrig, $heightOrig);
930 930
 
931
-		if ($ratioOrig > 1) {
932
-			$x = ($widthOrig / 2) - ($width / 2);
933
-			$y = 0;
934
-		} else {
935
-			$y = ($heightOrig / 2) - ($height / 2);
936
-			$x = 0;
937
-		}
938
-		if ($size > 0) {
939
-			$targetWidth = $size;
940
-			$targetHeight = $size;
941
-		} else {
942
-			$targetWidth = $width;
943
-			$targetHeight = $height;
944
-		}
945
-		$process = imagecreatetruecolor($targetWidth, $targetHeight);
946
-		if ($process == false) {
947
-			$this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core'));
948
-			imagedestroy($process);
949
-			return false;
950
-		}
931
+        if ($ratioOrig > 1) {
932
+            $x = ($widthOrig / 2) - ($width / 2);
933
+            $y = 0;
934
+        } else {
935
+            $y = ($heightOrig / 2) - ($height / 2);
936
+            $x = 0;
937
+        }
938
+        if ($size > 0) {
939
+            $targetWidth = $size;
940
+            $targetHeight = $size;
941
+        } else {
942
+            $targetWidth = $width;
943
+            $targetHeight = $height;
944
+        }
945
+        $process = imagecreatetruecolor($targetWidth, $targetHeight);
946
+        if ($process == false) {
947
+            $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core'));
948
+            imagedestroy($process);
949
+            return false;
950
+        }
951 951
 
952
-		// preserve transparency
953
-		if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
954
-			imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
955
-			imagealphablending($process, false);
956
-			imagesavealpha($process, true);
957
-		}
952
+        // preserve transparency
953
+        if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
954
+            imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
955
+            imagealphablending($process, false);
956
+            imagesavealpha($process, true);
957
+        }
958 958
 
959
-		imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
960
-		if ($process == false) {
961
-			$this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core'));
962
-			imagedestroy($process);
963
-			return false;
964
-		}
965
-		imagedestroy($this->resource);
966
-		$this->resource = $process;
967
-		return true;
968
-	}
959
+        imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
960
+        if ($process == false) {
961
+            $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core'));
962
+            imagedestroy($process);
963
+            return false;
964
+        }
965
+        imagedestroy($this->resource);
966
+        $this->resource = $process;
967
+        return true;
968
+    }
969 969
 
970
-	/**
971
-	 * Crops the image from point $x$y with dimension $wx$h.
972
-	 *
973
-	 * @param int $x Horizontal position
974
-	 * @param int $y Vertical position
975
-	 * @param int $w Width
976
-	 * @param int $h Height
977
-	 * @return bool for success or failure
978
-	 */
979
-	public function crop($x, $y, $w, $h) {
980
-		if (!$this->valid()) {
981
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
982
-			return false;
983
-		}
984
-		$process = imagecreatetruecolor($w, $h);
985
-		if ($process == false) {
986
-			$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
987
-			imagedestroy($process);
988
-			return false;
989
-		}
970
+    /**
971
+     * Crops the image from point $x$y with dimension $wx$h.
972
+     *
973
+     * @param int $x Horizontal position
974
+     * @param int $y Vertical position
975
+     * @param int $w Width
976
+     * @param int $h Height
977
+     * @return bool for success or failure
978
+     */
979
+    public function crop($x, $y, $w, $h) {
980
+        if (!$this->valid()) {
981
+            $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
982
+            return false;
983
+        }
984
+        $process = imagecreatetruecolor($w, $h);
985
+        if ($process == false) {
986
+            $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
987
+            imagedestroy($process);
988
+            return false;
989
+        }
990 990
 
991
-		// preserve transparency
992
-		if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
993
-			imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
994
-			imagealphablending($process, false);
995
-			imagesavealpha($process, true);
996
-		}
991
+        // preserve transparency
992
+        if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) {
993
+            imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127));
994
+            imagealphablending($process, false);
995
+            imagesavealpha($process, true);
996
+        }
997 997
 
998
-		imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
999
-		if ($process == false) {
1000
-			$this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core'));
1001
-			imagedestroy($process);
1002
-			return false;
1003
-		}
1004
-		imagedestroy($this->resource);
1005
-		$this->resource = $process;
1006
-		return true;
1007
-	}
998
+        imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
999
+        if ($process == false) {
1000
+            $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core'));
1001
+            imagedestroy($process);
1002
+            return false;
1003
+        }
1004
+        imagedestroy($this->resource);
1005
+        $this->resource = $process;
1006
+        return true;
1007
+    }
1008 1008
 
1009
-	/**
1010
-	 * Resizes the image to fit within a boundary while preserving ratio.
1011
-	 *
1012
-	 * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up
1013
-	 *
1014
-	 * @param integer $maxWidth
1015
-	 * @param integer $maxHeight
1016
-	 * @return bool
1017
-	 */
1018
-	public function fitIn($maxWidth, $maxHeight) {
1019
-		if (!$this->valid()) {
1020
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1021
-			return false;
1022
-		}
1023
-		$widthOrig = imagesx($this->resource);
1024
-		$heightOrig = imagesy($this->resource);
1025
-		$ratio = $widthOrig / $heightOrig;
1009
+    /**
1010
+     * Resizes the image to fit within a boundary while preserving ratio.
1011
+     *
1012
+     * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up
1013
+     *
1014
+     * @param integer $maxWidth
1015
+     * @param integer $maxHeight
1016
+     * @return bool
1017
+     */
1018
+    public function fitIn($maxWidth, $maxHeight) {
1019
+        if (!$this->valid()) {
1020
+            $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1021
+            return false;
1022
+        }
1023
+        $widthOrig = imagesx($this->resource);
1024
+        $heightOrig = imagesy($this->resource);
1025
+        $ratio = $widthOrig / $heightOrig;
1026 1026
 
1027
-		$newWidth = min($maxWidth, $ratio * $maxHeight);
1028
-		$newHeight = min($maxHeight, $maxWidth / $ratio);
1027
+        $newWidth = min($maxWidth, $ratio * $maxHeight);
1028
+        $newHeight = min($maxHeight, $maxWidth / $ratio);
1029 1029
 
1030
-		$this->preciseResize(round($newWidth), round($newHeight));
1031
-		return true;
1032
-	}
1030
+        $this->preciseResize(round($newWidth), round($newHeight));
1031
+        return true;
1032
+    }
1033 1033
 
1034
-	/**
1035
-	 * Shrinks larger images to fit within specified boundaries while preserving ratio.
1036
-	 *
1037
-	 * @param integer $maxWidth
1038
-	 * @param integer $maxHeight
1039
-	 * @return bool
1040
-	 */
1041
-	public function scaleDownToFit($maxWidth, $maxHeight) {
1042
-		if (!$this->valid()) {
1043
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1044
-			return false;
1045
-		}
1046
-		$widthOrig = imagesx($this->resource);
1047
-		$heightOrig = imagesy($this->resource);
1034
+    /**
1035
+     * Shrinks larger images to fit within specified boundaries while preserving ratio.
1036
+     *
1037
+     * @param integer $maxWidth
1038
+     * @param integer $maxHeight
1039
+     * @return bool
1040
+     */
1041
+    public function scaleDownToFit($maxWidth, $maxHeight) {
1042
+        if (!$this->valid()) {
1043
+            $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1044
+            return false;
1045
+        }
1046
+        $widthOrig = imagesx($this->resource);
1047
+        $heightOrig = imagesy($this->resource);
1048 1048
 
1049
-		if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) {
1050
-			return $this->fitIn($maxWidth, $maxHeight);
1051
-		}
1049
+        if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) {
1050
+            return $this->fitIn($maxWidth, $maxHeight);
1051
+        }
1052 1052
 
1053
-		return false;
1054
-	}
1053
+        return false;
1054
+    }
1055 1055
 
1056
-	/**
1057
-	 * Destroys the current image and resets the object
1058
-	 */
1059
-	public function destroy() {
1060
-		if ($this->valid()) {
1061
-			imagedestroy($this->resource);
1062
-		}
1063
-		$this->resource = null;
1064
-	}
1056
+    /**
1057
+     * Destroys the current image and resets the object
1058
+     */
1059
+    public function destroy() {
1060
+        if ($this->valid()) {
1061
+            imagedestroy($this->resource);
1062
+        }
1063
+        $this->resource = null;
1064
+    }
1065 1065
 
1066
-	public function __destruct() {
1067
-		$this->destroy();
1068
-	}
1066
+    public function __destruct() {
1067
+        $this->destroy();
1068
+    }
1069 1069
 }
1070 1070
 
1071 1071
 if (!function_exists('imagebmp')) {
1072
-	/**
1073
-	 * Output a BMP image to either the browser or a file
1074
-	 *
1075
-	 * @link http://www.ugia.cn/wp-data/imagebmp.php
1076
-	 * @author legend <[email protected]>
1077
-	 * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
1078
-	 * @author mgutt <[email protected]>
1079
-	 * @version 1.00
1080
-	 * @param resource $im
1081
-	 * @param string $fileName [optional] <p>The path to save the file to.</p>
1082
-	 * @param int $bit [optional] <p>Bit depth, (default is 24).</p>
1083
-	 * @param int $compression [optional]
1084
-	 * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
1085
-	 */
1086
-	function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) {
1087
-		if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) {
1088
-			$bit = 24;
1089
-		} else if ($bit == 32) {
1090
-			$bit = 24;
1091
-		}
1092
-		$bits = pow(2, $bit);
1093
-		imagetruecolortopalette($im, true, $bits);
1094
-		$width = imagesx($im);
1095
-		$height = imagesy($im);
1096
-		$colorsNum = imagecolorstotal($im);
1097
-		$rgbQuad = '';
1098
-		if ($bit <= 8) {
1099
-			for ($i = 0; $i < $colorsNum; $i++) {
1100
-				$colors = imagecolorsforindex($im, $i);
1101
-				$rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
1102
-			}
1103
-			$bmpData = '';
1104
-			if ($compression == 0 || $bit < 8) {
1105
-				$compression = 0;
1106
-				$extra = '';
1107
-				$padding = 4 - ceil($width / (8 / $bit)) % 4;
1108
-				if ($padding % 4 != 0) {
1109
-					$extra = str_repeat("\0", $padding);
1110
-				}
1111
-				for ($j = $height - 1; $j >= 0; $j--) {
1112
-					$i = 0;
1113
-					while ($i < $width) {
1114
-						$bin = 0;
1115
-						$limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
1116
-						for ($k = 8 - $bit; $k >= $limit; $k -= $bit) {
1117
-							$index = imagecolorat($im, $i, $j);
1118
-							$bin |= $index << $k;
1119
-							$i++;
1120
-						}
1121
-						$bmpData .= chr($bin);
1122
-					}
1123
-					$bmpData .= $extra;
1124
-				}
1125
-			} // RLE8
1126
-			else if ($compression == 1 && $bit == 8) {
1127
-				for ($j = $height - 1; $j >= 0; $j--) {
1128
-					$lastIndex = "\0";
1129
-					$sameNum = 0;
1130
-					for ($i = 0; $i <= $width; $i++) {
1131
-						$index = imagecolorat($im, $i, $j);
1132
-						if ($index !== $lastIndex || $sameNum > 255) {
1133
-							if ($sameNum != 0) {
1134
-								$bmpData .= chr($sameNum) . chr($lastIndex);
1135
-							}
1136
-							$lastIndex = $index;
1137
-							$sameNum = 1;
1138
-						} else {
1139
-							$sameNum++;
1140
-						}
1141
-					}
1142
-					$bmpData .= "\0\0";
1143
-				}
1144
-				$bmpData .= "\0\1";
1145
-			}
1146
-			$sizeQuad = strlen($rgbQuad);
1147
-			$sizeData = strlen($bmpData);
1148
-		} else {
1149
-			$extra = '';
1150
-			$padding = 4 - ($width * ($bit / 8)) % 4;
1151
-			if ($padding % 4 != 0) {
1152
-				$extra = str_repeat("\0", $padding);
1153
-			}
1154
-			$bmpData = '';
1155
-			for ($j = $height - 1; $j >= 0; $j--) {
1156
-				for ($i = 0; $i < $width; $i++) {
1157
-					$index = imagecolorat($im, $i, $j);
1158
-					$colors = imagecolorsforindex($im, $index);
1159
-					if ($bit == 16) {
1160
-						$bin = 0 << $bit;
1161
-						$bin |= ($colors['red'] >> 3) << 10;
1162
-						$bin |= ($colors['green'] >> 3) << 5;
1163
-						$bin |= $colors['blue'] >> 3;
1164
-						$bmpData .= pack("v", $bin);
1165
-					} else {
1166
-						$bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
1167
-					}
1168
-				}
1169
-				$bmpData .= $extra;
1170
-			}
1171
-			$sizeQuad = 0;
1172
-			$sizeData = strlen($bmpData);
1173
-			$colorsNum = 0;
1174
-		}
1175
-		$fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad);
1176
-		$infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0);
1177
-		if ($fileName != '') {
1178
-			$fp = fopen($fileName, 'wb');
1179
-			fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData);
1180
-			fclose($fp);
1181
-			return true;
1182
-		}
1183
-		echo $fileHeader . $infoHeader . $rgbQuad . $bmpData;
1184
-		return true;
1185
-	}
1072
+    /**
1073
+     * Output a BMP image to either the browser or a file
1074
+     *
1075
+     * @link http://www.ugia.cn/wp-data/imagebmp.php
1076
+     * @author legend <[email protected]>
1077
+     * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm
1078
+     * @author mgutt <[email protected]>
1079
+     * @version 1.00
1080
+     * @param resource $im
1081
+     * @param string $fileName [optional] <p>The path to save the file to.</p>
1082
+     * @param int $bit [optional] <p>Bit depth, (default is 24).</p>
1083
+     * @param int $compression [optional]
1084
+     * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
1085
+     */
1086
+    function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) {
1087
+        if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) {
1088
+            $bit = 24;
1089
+        } else if ($bit == 32) {
1090
+            $bit = 24;
1091
+        }
1092
+        $bits = pow(2, $bit);
1093
+        imagetruecolortopalette($im, true, $bits);
1094
+        $width = imagesx($im);
1095
+        $height = imagesy($im);
1096
+        $colorsNum = imagecolorstotal($im);
1097
+        $rgbQuad = '';
1098
+        if ($bit <= 8) {
1099
+            for ($i = 0; $i < $colorsNum; $i++) {
1100
+                $colors = imagecolorsforindex($im, $i);
1101
+                $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
1102
+            }
1103
+            $bmpData = '';
1104
+            if ($compression == 0 || $bit < 8) {
1105
+                $compression = 0;
1106
+                $extra = '';
1107
+                $padding = 4 - ceil($width / (8 / $bit)) % 4;
1108
+                if ($padding % 4 != 0) {
1109
+                    $extra = str_repeat("\0", $padding);
1110
+                }
1111
+                for ($j = $height - 1; $j >= 0; $j--) {
1112
+                    $i = 0;
1113
+                    while ($i < $width) {
1114
+                        $bin = 0;
1115
+                        $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
1116
+                        for ($k = 8 - $bit; $k >= $limit; $k -= $bit) {
1117
+                            $index = imagecolorat($im, $i, $j);
1118
+                            $bin |= $index << $k;
1119
+                            $i++;
1120
+                        }
1121
+                        $bmpData .= chr($bin);
1122
+                    }
1123
+                    $bmpData .= $extra;
1124
+                }
1125
+            } // RLE8
1126
+            else if ($compression == 1 && $bit == 8) {
1127
+                for ($j = $height - 1; $j >= 0; $j--) {
1128
+                    $lastIndex = "\0";
1129
+                    $sameNum = 0;
1130
+                    for ($i = 0; $i <= $width; $i++) {
1131
+                        $index = imagecolorat($im, $i, $j);
1132
+                        if ($index !== $lastIndex || $sameNum > 255) {
1133
+                            if ($sameNum != 0) {
1134
+                                $bmpData .= chr($sameNum) . chr($lastIndex);
1135
+                            }
1136
+                            $lastIndex = $index;
1137
+                            $sameNum = 1;
1138
+                        } else {
1139
+                            $sameNum++;
1140
+                        }
1141
+                    }
1142
+                    $bmpData .= "\0\0";
1143
+                }
1144
+                $bmpData .= "\0\1";
1145
+            }
1146
+            $sizeQuad = strlen($rgbQuad);
1147
+            $sizeData = strlen($bmpData);
1148
+        } else {
1149
+            $extra = '';
1150
+            $padding = 4 - ($width * ($bit / 8)) % 4;
1151
+            if ($padding % 4 != 0) {
1152
+                $extra = str_repeat("\0", $padding);
1153
+            }
1154
+            $bmpData = '';
1155
+            for ($j = $height - 1; $j >= 0; $j--) {
1156
+                for ($i = 0; $i < $width; $i++) {
1157
+                    $index = imagecolorat($im, $i, $j);
1158
+                    $colors = imagecolorsforindex($im, $index);
1159
+                    if ($bit == 16) {
1160
+                        $bin = 0 << $bit;
1161
+                        $bin |= ($colors['red'] >> 3) << 10;
1162
+                        $bin |= ($colors['green'] >> 3) << 5;
1163
+                        $bin |= $colors['blue'] >> 3;
1164
+                        $bmpData .= pack("v", $bin);
1165
+                    } else {
1166
+                        $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
1167
+                    }
1168
+                }
1169
+                $bmpData .= $extra;
1170
+            }
1171
+            $sizeQuad = 0;
1172
+            $sizeData = strlen($bmpData);
1173
+            $colorsNum = 0;
1174
+        }
1175
+        $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad);
1176
+        $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0);
1177
+        if ($fileName != '') {
1178
+            $fp = fopen($fileName, 'wb');
1179
+            fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData);
1180
+            fclose($fp);
1181
+            return true;
1182
+        }
1183
+        echo $fileHeader . $infoHeader . $rgbQuad . $bmpData;
1184
+        return true;
1185
+    }
1186 1186
 }
1187 1187
 
1188 1188
 if (!function_exists('exif_imagetype')) {
1189
-	/**
1190
-	 * Workaround if exif_imagetype does not exist
1191
-	 *
1192
-	 * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383
1193
-	 * @param string $fileName
1194
-	 * @return string|boolean
1195
-	 */
1196
-	function exif_imagetype($fileName) {
1197
-		if (($info = getimagesize($fileName)) !== false) {
1198
-			return $info[2];
1199
-		}
1200
-		return false;
1201
-	}
1189
+    /**
1190
+     * Workaround if exif_imagetype does not exist
1191
+     *
1192
+     * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383
1193
+     * @param string $fileName
1194
+     * @return string|boolean
1195
+     */
1196
+    function exif_imagetype($fileName) {
1197
+        if (($info = getimagesize($fileName)) !== false) {
1198
+            return $info[2];
1199
+        }
1200
+        return false;
1201
+    }
1202 1202
 }
Please login to merge, or discard this patch.
Spacing   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
 	 */
146 146
 	public function widthTopLeft() {
147 147
 		$o = $this->getOrientation();
148
-		$this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core'));
148
+		$this->logger->debug('OC_Image->widthTopLeft() Orientation: '.$o, array('app' => 'core'));
149 149
 		switch ($o) {
150 150
 			case -1:
151 151
 			case 1:
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
 	 */
170 170
 	public function heightTopLeft() {
171 171
 		$o = $this->getOrientation();
172
-		$this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core'));
172
+		$this->logger->debug('OC_Image->heightTopLeft() Orientation: '.$o, array('app' => 'core'));
173 173
 		switch ($o) {
174 174
 			case -1:
175 175
 			case 1:
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
 		if ($mimeType === null) {
197 197
 			$mimeType = $this->mimeType();
198 198
 		}
199
-		header('Content-Type: ' . $mimeType);
199
+		header('Content-Type: '.$mimeType);
200 200
 		return $this->_output(null, $mimeType);
201 201
 	}
202 202
 
@@ -214,7 +214,7 @@  discard block
 block discarded – undo
214 214
 		}
215 215
 		if ($filePath === null) {
216 216
 			if ($this->filePath === null) {
217
-				$this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core'));
217
+				$this->logger->error(__METHOD__.'(): called with no path.', array('app' => 'core'));
218 218
 				return false;
219 219
 			} else {
220 220
 				$filePath = $this->filePath;
@@ -238,10 +238,10 @@  discard block
 block discarded – undo
238 238
 			}
239 239
 			$isWritable = is_writable(dirname($filePath));
240 240
 			if (!$isWritable) {
241
-				$this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core'));
241
+				$this->logger->error(__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.', array('app' => 'core'));
242 242
 				return false;
243 243
 			} elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) {
244
-				$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core'));
244
+				$this->logger->error(__METHOD__.'(): File \''.$filePath.'\' is not writable.', array('app' => 'core'));
245 245
 				return false;
246 246
 			}
247 247
 		}
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 					$imageType = IMAGETYPE_BMP;
270 270
 					break;
271 271
 				default:
272
-					throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
272
+					throw new Exception('\OC_Image::_output(): "'.$mimeType.'" is not supported when forcing a specific output format');
273 273
 			}
274 274
 		}
275 275
 
@@ -417,7 +417,7 @@  discard block
 block discarded – undo
417 417
 			return;
418 418
 		}
419 419
 
420
-		$exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data));
420
+		$exif = @exif_read_data('data://image/jpeg;base64,'.base64_encode($data));
421 421
 		if (!$exif) {
422 422
 			return;
423 423
 		}
@@ -435,7 +435,7 @@  discard block
 block discarded – undo
435 435
 	 */
436 436
 	public function fixOrientation() {
437 437
 		$o = $this->getOrientation();
438
-		$this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core'));
438
+		$this->logger->debug('OC_Image->fixOrientation() Orientation: '.$o, array('app' => 'core'));
439 439
 		$rotate = 0;
440 440
 		$flip = false;
441 441
 		switch ($o) {
@@ -470,7 +470,7 @@  discard block
 block discarded – undo
470 470
 				$rotate = 90;
471 471
 				break;
472 472
 		}
473
-		if($flip && function_exists('imageflip')) {
473
+		if ($flip && function_exists('imageflip')) {
474 474
 			imageflip($this->resource, IMG_FLIP_HORIZONTAL);
475 475
 		}
476 476
 		if ($rotate) {
@@ -518,7 +518,7 @@  discard block
 block discarded – undo
518 518
 		} elseif ($this->loadFromData($imageRef) !== false) {
519 519
 			return $this->resource;
520 520
 		}
521
-		$this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
521
+		$this->logger->debug(__METHOD__.'(): could not load anything. Giving up!', array('app' => 'core'));
522 522
 		return false;
523 523
 	}
524 524
 
@@ -557,7 +557,7 @@  discard block
 block discarded – undo
557 557
 					imagealphablending($this->resource, true);
558 558
 					imagesavealpha($this->resource, true);
559 559
 				} else {
560
-					$this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
560
+					$this->logger->debug('OC_Image->loadFromFile, GIF images not supported: '.$imagePath, array('app' => 'core'));
561 561
 				}
562 562
 				break;
563 563
 			case IMAGETYPE_JPEG:
@@ -565,10 +565,10 @@  discard block
 block discarded – undo
565 565
 					if (getimagesize($imagePath) !== false) {
566 566
 						$this->resource = @imagecreatefromjpeg($imagePath);
567 567
 					} else {
568
-						$this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core'));
568
+						$this->logger->debug('OC_Image->loadFromFile, JPG image not valid: '.$imagePath, array('app' => 'core'));
569 569
 					}
570 570
 				} else {
571
-					$this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
571
+					$this->logger->debug('OC_Image->loadFromFile, JPG images not supported: '.$imagePath, array('app' => 'core'));
572 572
 				}
573 573
 				break;
574 574
 			case IMAGETYPE_PNG:
@@ -578,21 +578,21 @@  discard block
 block discarded – undo
578 578
 					imagealphablending($this->resource, true);
579 579
 					imagesavealpha($this->resource, true);
580 580
 				} else {
581
-					$this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
581
+					$this->logger->debug('OC_Image->loadFromFile, PNG images not supported: '.$imagePath, array('app' => 'core'));
582 582
 				}
583 583
 				break;
584 584
 			case IMAGETYPE_XBM:
585 585
 				if (imagetypes() & IMG_XPM) {
586 586
 					$this->resource = @imagecreatefromxbm($imagePath);
587 587
 				} else {
588
-					$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
588
+					$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath, array('app' => 'core'));
589 589
 				}
590 590
 				break;
591 591
 			case IMAGETYPE_WBMP:
592 592
 				if (imagetypes() & IMG_WBMP) {
593 593
 					$this->resource = @imagecreatefromwbmp($imagePath);
594 594
 				} else {
595
-					$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
595
+					$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: '.$imagePath, array('app' => 'core'));
596 596
 				}
597 597
 				break;
598 598
 			case IMAGETYPE_BMP:
@@ -702,7 +702,7 @@  discard block
 block discarded – undo
702 702
 	 */
703 703
 	private function imagecreatefrombmp($fileName) {
704 704
 		if (!($fh = fopen($fileName, 'rb'))) {
705
-			$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core'));
705
+			$this->logger->warning('imagecreatefrombmp: Can not open '.$fileName, array('app' => 'core'));
706 706
 			return false;
707 707
 		}
708 708
 		// read file header
@@ -710,7 +710,7 @@  discard block
 block discarded – undo
710 710
 		// check for bitmap
711 711
 		if ($meta['type'] != 19778) {
712 712
 			fclose($fh);
713
-			$this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
713
+			$this->logger->warning('imagecreatefrombmp: Can not open '.$fileName.' is not a bitmap!', array('app' => 'core'));
714 714
 			return false;
715 715
 		}
716 716
 		// read image header
@@ -734,7 +734,7 @@  discard block
 block discarded – undo
734 734
 				$meta['imagesize'] = @filesize($fileName) - $meta['offset'];
735 735
 				if ($meta['imagesize'] < 1) {
736 736
 					fclose($fh);
737
-					$this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core'));
737
+					$this->logger->warning('imagecreatefrombmp: Can not obtain file size of '.$fileName.' is not a bitmap!', array('app' => 'core'));
738 738
 					return false;
739 739
 				}
740 740
 			}
@@ -744,7 +744,7 @@  discard block
 block discarded – undo
744 744
 		// read color palette
745 745
 		$palette = array();
746 746
 		if ($meta['bits'] < 16) {
747
-			$palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4));
747
+			$palette = unpack('l'.$meta['colors'], fread($fh, $meta['colors'] * 4));
748 748
 			// in rare cases the color value is signed
749 749
 			if ($palette[1] < 0) {
750 750
 				foreach ($palette as $i => $color) {
@@ -757,7 +757,7 @@  discard block
 block discarded – undo
757 757
 		if ($im == false) {
758 758
 			fclose($fh);
759 759
 			$this->logger->warning(
760
-				'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'],
760
+				'imagecreatefrombmp: imagecreatetruecolor failed for file "'.$fileName.'" with dimensions '.$meta['width'].'x'.$meta['height'],
761 761
 				array('app' => 'core'));
762 762
 			return false;
763 763
 		}
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
 		$p = 0;
767 767
 		$vide = chr(0);
768 768
 		$y = $meta['height'] - 1;
769
-		$error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!';
769
+		$error = 'imagecreatefrombmp: '.$fileName.' has not enough data!';
770 770
 		// loop through the image data beginning with the lower left corner
771 771
 		while ($y >= 0) {
772 772
 			$x = 0;
@@ -778,7 +778,7 @@  discard block
 block discarded – undo
778 778
 							$this->logger->warning($error, array('app' => 'core'));
779 779
 							return $im;
780 780
 						}
781
-						$color = @unpack('V', $part . $vide);
781
+						$color = @unpack('V', $part.$vide);
782 782
 						break;
783 783
 					case 16:
784 784
 						if (!($part = substr($data, $p, 2))) {
@@ -790,16 +790,16 @@  discard block
 block discarded – undo
790 790
 						$color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3);
791 791
 						break;
792 792
 					case 8:
793
-						$color = @unpack('n', $vide . substr($data, $p, 1));
793
+						$color = @unpack('n', $vide.substr($data, $p, 1));
794 794
 						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
795 795
 						break;
796 796
 					case 4:
797
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
797
+						$color = @unpack('n', $vide.substr($data, floor($p), 1));
798 798
 						$color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F;
799 799
 						$color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1];
800 800
 						break;
801 801
 					case 1:
802
-						$color = @unpack('n', $vide . substr($data, floor($p), 1));
802
+						$color = @unpack('n', $vide.substr($data, floor($p), 1));
803 803
 						switch (($p * 8) % 8) {
804 804
 							case 0:
805 805
 								$color[1] = $color[1] >> 7;
@@ -830,7 +830,7 @@  discard block
 block discarded – undo
830 830
 						break;
831 831
 					default:
832 832
 						fclose($fh);
833
-						$this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core'));
833
+						$this->logger->warning('imagecreatefrombmp: '.$fileName.' has '.$meta['bits'].' bits and this is not supported!', array('app' => 'core'));
834 834
 						return false;
835 835
 				}
836 836
 				imagesetpixel($im, $x, $y, $color[1]);
@@ -852,7 +852,7 @@  discard block
 block discarded – undo
852 852
 	 */
853 853
 	public function resize($maxSize) {
854 854
 		if (!$this->valid()) {
855
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
855
+			$this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core'));
856 856
 			return false;
857 857
 		}
858 858
 		$widthOrig = imagesx($this->resource);
@@ -878,7 +878,7 @@  discard block
 block discarded – undo
878 878
 	 */
879 879
 	public function preciseResize($width, $height) {
880 880
 		if (!$this->valid()) {
881
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
881
+			$this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core'));
882 882
 			return false;
883 883
 		}
884 884
 		$widthOrig = imagesx($this->resource);
@@ -886,7 +886,7 @@  discard block
 block discarded – undo
886 886
 		$process = imagecreatetruecolor($width, $height);
887 887
 
888 888
 		if ($process == false) {
889
-			$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
889
+			$this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core'));
890 890
 			imagedestroy($process);
891 891
 			return false;
892 892
 		}
@@ -900,7 +900,7 @@  discard block
 block discarded – undo
900 900
 
901 901
 		imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig);
902 902
 		if ($process == false) {
903
-			$this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core'));
903
+			$this->logger->error(__METHOD__.'(): Error re-sampling process image', array('app' => 'core'));
904 904
 			imagedestroy($process);
905 905
 			return false;
906 906
 		}
@@ -958,7 +958,7 @@  discard block
 block discarded – undo
958 958
 
959 959
 		imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
960 960
 		if ($process == false) {
961
-			$this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core'));
961
+			$this->logger->error('OC_Image->centerCrop, Error re-sampling process image '.$width.'x'.$height, array('app' => 'core'));
962 962
 			imagedestroy($process);
963 963
 			return false;
964 964
 		}
@@ -978,12 +978,12 @@  discard block
 block discarded – undo
978 978
 	 */
979 979
 	public function crop($x, $y, $w, $h) {
980 980
 		if (!$this->valid()) {
981
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
981
+			$this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core'));
982 982
 			return false;
983 983
 		}
984 984
 		$process = imagecreatetruecolor($w, $h);
985 985
 		if ($process == false) {
986
-			$this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core'));
986
+			$this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core'));
987 987
 			imagedestroy($process);
988 988
 			return false;
989 989
 		}
@@ -997,7 +997,7 @@  discard block
 block discarded – undo
997 997
 
998 998
 		imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h);
999 999
 		if ($process == false) {
1000
-			$this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core'));
1000
+			$this->logger->error(__METHOD__.'(): Error re-sampling process image '.$w.'x'.$h, array('app' => 'core'));
1001 1001
 			imagedestroy($process);
1002 1002
 			return false;
1003 1003
 		}
@@ -1017,7 +1017,7 @@  discard block
 block discarded – undo
1017 1017
 	 */
1018 1018
 	public function fitIn($maxWidth, $maxHeight) {
1019 1019
 		if (!$this->valid()) {
1020
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1020
+			$this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core'));
1021 1021
 			return false;
1022 1022
 		}
1023 1023
 		$widthOrig = imagesx($this->resource);
@@ -1040,7 +1040,7 @@  discard block
 block discarded – undo
1040 1040
 	 */
1041 1041
 	public function scaleDownToFit($maxWidth, $maxHeight) {
1042 1042
 		if (!$this->valid()) {
1043
-			$this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core'));
1043
+			$this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core'));
1044 1044
 			return false;
1045 1045
 		}
1046 1046
 		$widthOrig = imagesx($this->resource);
@@ -1098,7 +1098,7 @@  discard block
 block discarded – undo
1098 1098
 		if ($bit <= 8) {
1099 1099
 			for ($i = 0; $i < $colorsNum; $i++) {
1100 1100
 				$colors = imagecolorsforindex($im, $i);
1101
-				$rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0";
1101
+				$rgbQuad .= chr($colors['blue']).chr($colors['green']).chr($colors['red'])."\0";
1102 1102
 			}
1103 1103
 			$bmpData = '';
1104 1104
 			if ($compression == 0 || $bit < 8) {
@@ -1131,7 +1131,7 @@  discard block
 block discarded – undo
1131 1131
 						$index = imagecolorat($im, $i, $j);
1132 1132
 						if ($index !== $lastIndex || $sameNum > 255) {
1133 1133
 							if ($sameNum != 0) {
1134
-								$bmpData .= chr($sameNum) . chr($lastIndex);
1134
+								$bmpData .= chr($sameNum).chr($lastIndex);
1135 1135
 							}
1136 1136
 							$lastIndex = $index;
1137 1137
 							$sameNum = 1;
@@ -1172,15 +1172,15 @@  discard block
 block discarded – undo
1172 1172
 			$sizeData = strlen($bmpData);
1173 1173
 			$colorsNum = 0;
1174 1174
 		}
1175
-		$fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad);
1175
+		$fileHeader = 'BM'.pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad);
1176 1176
 		$infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0);
1177 1177
 		if ($fileName != '') {
1178 1178
 			$fp = fopen($fileName, 'wb');
1179
-			fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData);
1179
+			fwrite($fp, $fileHeader.$infoHeader.$rgbQuad.$bmpData);
1180 1180
 			fclose($fp);
1181 1181
 			return true;
1182 1182
 		}
1183
-		echo $fileHeader . $infoHeader . $rgbQuad . $bmpData;
1183
+		echo $fileHeader.$infoHeader.$rgbQuad.$bmpData;
1184 1184
 		return true;
1185 1185
 	}
1186 1186
 }
Please login to merge, or discard this patch.
lib/private/Preview/Generator.php 1 patch
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -38,336 +38,336 @@
 block discarded – undo
38 38
 
39 39
 class Generator {
40 40
 
41
-	/** @var IPreview */
42
-	private $previewManager;
43
-	/** @var IConfig */
44
-	private $config;
45
-	/** @var IAppData */
46
-	private $appData;
47
-	/** @var GeneratorHelper */
48
-	private $helper;
49
-	/** @var EventDispatcherInterface */
50
-	private $eventDispatcher;
51
-
52
-	/**
53
-	 * @param IConfig $config
54
-	 * @param IPreview $previewManager
55
-	 * @param IAppData $appData
56
-	 * @param GeneratorHelper $helper
57
-	 * @param EventDispatcherInterface $eventDispatcher
58
-	 */
59
-	public function __construct(
60
-		IConfig $config,
61
-		IPreview $previewManager,
62
-		IAppData $appData,
63
-		GeneratorHelper $helper,
64
-		EventDispatcherInterface $eventDispatcher
65
-	) {
66
-		$this->config = $config;
67
-		$this->previewManager = $previewManager;
68
-		$this->appData = $appData;
69
-		$this->helper = $helper;
70
-		$this->eventDispatcher = $eventDispatcher;
71
-	}
72
-
73
-	/**
74
-	 * Returns a preview of a file
75
-	 *
76
-	 * The cache is searched first and if nothing usable was found then a preview is
77
-	 * generated by one of the providers
78
-	 *
79
-	 * @param File $file
80
-	 * @param int $width
81
-	 * @param int $height
82
-	 * @param bool $crop
83
-	 * @param string $mode
84
-	 * @param string $mimeType
85
-	 * @return ISimpleFile
86
-	 * @throws NotFoundException
87
-	 */
88
-	public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
89
-		$this->eventDispatcher->dispatch(
90
-			IPreview::EVENT,
91
-			new GenericEvent($file,[
92
-				'width' => $width,
93
-				'height' => $height,
94
-				'crop' => $crop,
95
-				'mode' => $mode
96
-			])
97
-		);
98
-
99
-		if ($mimeType === null) {
100
-			$mimeType = $file->getMimeType();
101
-		}
102
-		if (!$this->previewManager->isMimeSupported($mimeType)) {
103
-			throw new NotFoundException();
104
-		}
105
-
106
-		$previewFolder = $this->getPreviewFolder($file);
107
-
108
-		// Get the max preview and infer the max preview sizes from that
109
-		$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
110
-		list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
111
-
112
-		// Calculate the preview size
113
-		list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
114
-
115
-		// No need to generate a preview that is just the max preview
116
-		if ($width === $maxWidth && $height === $maxHeight) {
117
-			return $maxPreview;
118
-		}
119
-
120
-		// Try to get a cached preview. Else generate (and store) one
121
-		try {
122
-			$file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
123
-		} catch (NotFoundException $e) {
124
-			$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
125
-		}
126
-
127
-		return $file;
128
-	}
129
-
130
-	/**
131
-	 * @param ISimpleFolder $previewFolder
132
-	 * @param File $file
133
-	 * @param string $mimeType
134
-	 * @return ISimpleFile
135
-	 * @throws NotFoundException
136
-	 */
137
-	private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
138
-		$nodes = $previewFolder->getDirectoryListing();
139
-
140
-		foreach ($nodes as $node) {
141
-			if (strpos($node->getName(), 'max')) {
142
-				return $node;
143
-			}
144
-		}
145
-
146
-		$previewProviders = $this->previewManager->getProviders();
147
-		foreach ($previewProviders as $supportedMimeType => $providers) {
148
-			if (!preg_match($supportedMimeType, $mimeType)) {
149
-				continue;
150
-			}
151
-
152
-			foreach ($providers as $provider) {
153
-				$provider = $this->helper->getProvider($provider);
154
-				if (!($provider instanceof IProvider)) {
155
-					continue;
156
-				}
157
-
158
-				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
159
-				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);
160
-
161
-				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
162
-
163
-				if (!($preview instanceof IImage)) {
164
-					continue;
165
-				}
166
-
167
-				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
168
-				try {
169
-					$file = $previewFolder->newFile($path);
170
-					$file->putContent($preview->data());
171
-				} catch (NotPermittedException $e) {
172
-					throw new NotFoundException();
173
-				}
174
-
175
-				return $file;
176
-			}
177
-		}
178
-
179
-		throw new NotFoundException();
180
-	}
181
-
182
-	/**
183
-	 * @param ISimpleFile $file
184
-	 * @return int[]
185
-	 */
186
-	private function getPreviewSize(ISimpleFile $file) {
187
-		$size = explode('-', $file->getName());
188
-		return [(int)$size[0], (int)$size[1]];
189
-	}
190
-
191
-	/**
192
-	 * @param int $width
193
-	 * @param int $height
194
-	 * @param bool $crop
195
-	 * @return string
196
-	 */
197
-	private function generatePath($width, $height, $crop) {
198
-		$path = (string)$width . '-' . (string)$height;
199
-		if ($crop) {
200
-			$path .= '-crop';
201
-		}
202
-		$path .= '.png';
203
-		return $path;
204
-	}
205
-
206
-
207
-
208
-	/**
209
-	 * @param int $width
210
-	 * @param int $height
211
-	 * @param bool $crop
212
-	 * @param string $mode
213
-	 * @param int $maxWidth
214
-	 * @param int $maxHeight
215
-	 * @return int[]
216
-	 */
217
-	private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
218
-
219
-		/*
41
+    /** @var IPreview */
42
+    private $previewManager;
43
+    /** @var IConfig */
44
+    private $config;
45
+    /** @var IAppData */
46
+    private $appData;
47
+    /** @var GeneratorHelper */
48
+    private $helper;
49
+    /** @var EventDispatcherInterface */
50
+    private $eventDispatcher;
51
+
52
+    /**
53
+     * @param IConfig $config
54
+     * @param IPreview $previewManager
55
+     * @param IAppData $appData
56
+     * @param GeneratorHelper $helper
57
+     * @param EventDispatcherInterface $eventDispatcher
58
+     */
59
+    public function __construct(
60
+        IConfig $config,
61
+        IPreview $previewManager,
62
+        IAppData $appData,
63
+        GeneratorHelper $helper,
64
+        EventDispatcherInterface $eventDispatcher
65
+    ) {
66
+        $this->config = $config;
67
+        $this->previewManager = $previewManager;
68
+        $this->appData = $appData;
69
+        $this->helper = $helper;
70
+        $this->eventDispatcher = $eventDispatcher;
71
+    }
72
+
73
+    /**
74
+     * Returns a preview of a file
75
+     *
76
+     * The cache is searched first and if nothing usable was found then a preview is
77
+     * generated by one of the providers
78
+     *
79
+     * @param File $file
80
+     * @param int $width
81
+     * @param int $height
82
+     * @param bool $crop
83
+     * @param string $mode
84
+     * @param string $mimeType
85
+     * @return ISimpleFile
86
+     * @throws NotFoundException
87
+     */
88
+    public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
89
+        $this->eventDispatcher->dispatch(
90
+            IPreview::EVENT,
91
+            new GenericEvent($file,[
92
+                'width' => $width,
93
+                'height' => $height,
94
+                'crop' => $crop,
95
+                'mode' => $mode
96
+            ])
97
+        );
98
+
99
+        if ($mimeType === null) {
100
+            $mimeType = $file->getMimeType();
101
+        }
102
+        if (!$this->previewManager->isMimeSupported($mimeType)) {
103
+            throw new NotFoundException();
104
+        }
105
+
106
+        $previewFolder = $this->getPreviewFolder($file);
107
+
108
+        // Get the max preview and infer the max preview sizes from that
109
+        $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
110
+        list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
111
+
112
+        // Calculate the preview size
113
+        list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
114
+
115
+        // No need to generate a preview that is just the max preview
116
+        if ($width === $maxWidth && $height === $maxHeight) {
117
+            return $maxPreview;
118
+        }
119
+
120
+        // Try to get a cached preview. Else generate (and store) one
121
+        try {
122
+            $file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
123
+        } catch (NotFoundException $e) {
124
+            $file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
125
+        }
126
+
127
+        return $file;
128
+    }
129
+
130
+    /**
131
+     * @param ISimpleFolder $previewFolder
132
+     * @param File $file
133
+     * @param string $mimeType
134
+     * @return ISimpleFile
135
+     * @throws NotFoundException
136
+     */
137
+    private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
138
+        $nodes = $previewFolder->getDirectoryListing();
139
+
140
+        foreach ($nodes as $node) {
141
+            if (strpos($node->getName(), 'max')) {
142
+                return $node;
143
+            }
144
+        }
145
+
146
+        $previewProviders = $this->previewManager->getProviders();
147
+        foreach ($previewProviders as $supportedMimeType => $providers) {
148
+            if (!preg_match($supportedMimeType, $mimeType)) {
149
+                continue;
150
+            }
151
+
152
+            foreach ($providers as $provider) {
153
+                $provider = $this->helper->getProvider($provider);
154
+                if (!($provider instanceof IProvider)) {
155
+                    continue;
156
+                }
157
+
158
+                $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
159
+                $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);
160
+
161
+                $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
162
+
163
+                if (!($preview instanceof IImage)) {
164
+                    continue;
165
+                }
166
+
167
+                $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
168
+                try {
169
+                    $file = $previewFolder->newFile($path);
170
+                    $file->putContent($preview->data());
171
+                } catch (NotPermittedException $e) {
172
+                    throw new NotFoundException();
173
+                }
174
+
175
+                return $file;
176
+            }
177
+        }
178
+
179
+        throw new NotFoundException();
180
+    }
181
+
182
+    /**
183
+     * @param ISimpleFile $file
184
+     * @return int[]
185
+     */
186
+    private function getPreviewSize(ISimpleFile $file) {
187
+        $size = explode('-', $file->getName());
188
+        return [(int)$size[0], (int)$size[1]];
189
+    }
190
+
191
+    /**
192
+     * @param int $width
193
+     * @param int $height
194
+     * @param bool $crop
195
+     * @return string
196
+     */
197
+    private function generatePath($width, $height, $crop) {
198
+        $path = (string)$width . '-' . (string)$height;
199
+        if ($crop) {
200
+            $path .= '-crop';
201
+        }
202
+        $path .= '.png';
203
+        return $path;
204
+    }
205
+
206
+
207
+
208
+    /**
209
+     * @param int $width
210
+     * @param int $height
211
+     * @param bool $crop
212
+     * @param string $mode
213
+     * @param int $maxWidth
214
+     * @param int $maxHeight
215
+     * @return int[]
216
+     */
217
+    private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
218
+
219
+        /*
220 220
 		 * If we are not cropping we have to make sure the requested image
221 221
 		 * respects the aspect ratio of the original.
222 222
 		 */
223
-		if (!$crop) {
224
-			$ratio = $maxHeight / $maxWidth;
223
+        if (!$crop) {
224
+            $ratio = $maxHeight / $maxWidth;
225 225
 
226
-			if ($width === -1) {
227
-				$width = $height / $ratio;
228
-			}
229
-			if ($height === -1) {
230
-				$height = $width * $ratio;
231
-			}
226
+            if ($width === -1) {
227
+                $width = $height / $ratio;
228
+            }
229
+            if ($height === -1) {
230
+                $height = $width * $ratio;
231
+            }
232 232
 
233
-			$ratioH = $height / $maxHeight;
234
-			$ratioW = $width / $maxWidth;
233
+            $ratioH = $height / $maxHeight;
234
+            $ratioW = $width / $maxWidth;
235 235
 
236
-			/*
236
+            /*
237 237
 			 * Fill means that the $height and $width are the max
238 238
 			 * Cover means min.
239 239
 			 */
240
-			if ($mode === IPreview::MODE_FILL) {
241
-				if ($ratioH > $ratioW) {
242
-					$height = $width * $ratio;
243
-				} else {
244
-					$width = $height / $ratio;
245
-				}
246
-			} else if ($mode === IPreview::MODE_COVER) {
247
-				if ($ratioH > $ratioW) {
248
-					$width = $height / $ratio;
249
-				} else {
250
-					$height = $width * $ratio;
251
-				}
252
-			}
253
-		}
254
-
255
-		if ($height !== $maxHeight && $width !== $maxWidth) {
256
-			/*
240
+            if ($mode === IPreview::MODE_FILL) {
241
+                if ($ratioH > $ratioW) {
242
+                    $height = $width * $ratio;
243
+                } else {
244
+                    $width = $height / $ratio;
245
+                }
246
+            } else if ($mode === IPreview::MODE_COVER) {
247
+                if ($ratioH > $ratioW) {
248
+                    $width = $height / $ratio;
249
+                } else {
250
+                    $height = $width * $ratio;
251
+                }
252
+            }
253
+        }
254
+
255
+        if ($height !== $maxHeight && $width !== $maxWidth) {
256
+            /*
257 257
 			 * Scale to the nearest power of two
258 258
 			 */
259
-			$pow2height = 2 ** ceil(log($height) / log(2));
260
-			$pow2width = 2 ** ceil(log($width) / log(2));
261
-
262
-			$ratioH = $height / $pow2height;
263
-			$ratioW = $width / $pow2width;
264
-
265
-			if ($ratioH < $ratioW) {
266
-				$width = $pow2width;
267
-				$height /= $ratioW;
268
-			} else {
269
-				$height = $pow2height;
270
-				$width /= $ratioH;
271
-			}
272
-		}
273
-
274
-		/*
259
+            $pow2height = 2 ** ceil(log($height) / log(2));
260
+            $pow2width = 2 ** ceil(log($width) / log(2));
261
+
262
+            $ratioH = $height / $pow2height;
263
+            $ratioW = $width / $pow2width;
264
+
265
+            if ($ratioH < $ratioW) {
266
+                $width = $pow2width;
267
+                $height /= $ratioW;
268
+            } else {
269
+                $height = $pow2height;
270
+                $width /= $ratioH;
271
+            }
272
+        }
273
+
274
+        /*
275 275
  		 * Make sure the requested height and width fall within the max
276 276
  		 * of the preview.
277 277
  		 */
278
-		if ($height > $maxHeight) {
279
-			$ratio = $height / $maxHeight;
280
-			$height = $maxHeight;
281
-			$width /= $ratio;
282
-		}
283
-		if ($width > $maxWidth) {
284
-			$ratio = $width / $maxWidth;
285
-			$width = $maxWidth;
286
-			$height /= $ratio;
287
-		}
288
-
289
-		return [(int)round($width), (int)round($height)];
290
-	}
291
-
292
-	/**
293
-	 * @param ISimpleFolder $previewFolder
294
-	 * @param ISimpleFile $maxPreview
295
-	 * @param int $width
296
-	 * @param int $height
297
-	 * @param bool $crop
298
-	 * @param int $maxWidth
299
-	 * @param int $maxHeight
300
-	 * @return ISimpleFile
301
-	 * @throws NotFoundException
302
-	 */
303
-	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
304
-		$preview = $this->helper->getImage($maxPreview);
305
-
306
-		if ($crop) {
307
-			if ($height !== $preview->height() && $width !== $preview->width()) {
308
-				//Resize
309
-				$widthR = $preview->width() / $width;
310
-				$heightR = $preview->height() / $height;
311
-
312
-				if ($widthR > $heightR) {
313
-					$scaleH = $height;
314
-					$scaleW = $maxWidth / $heightR;
315
-				} else {
316
-					$scaleH = $maxHeight / $widthR;
317
-					$scaleW = $width;
318
-				}
319
-				$preview->preciseResize(round($scaleW), round($scaleH));
320
-			}
321
-			$cropX = floor(abs($width - $preview->width()) * 0.5);
322
-			$cropY = 0;
323
-			$preview->crop($cropX, $cropY, $width, $height);
324
-		} else {
325
-			$preview->resize(max($width, $height));
326
-		}
327
-
328
-		if (!$preview->valid()) {
329
-			throw new \InvalidArgumentException('Failed to generate preview');
330
-		}
331
-
332
-		$path = $this->generatePath($width, $height, $crop);
333
-		try {
334
-			$file = $previewFolder->newFile($path);
335
-			$file->putContent($preview->data());
336
-		} catch (NotPermittedException $e) {
337
-			throw new NotFoundException();
338
-		}
339
-
340
-		return $file;
341
-	}
342
-
343
-	/**
344
-	 * @param ISimpleFolder $previewFolder
345
-	 * @param int $width
346
-	 * @param int $height
347
-	 * @param bool $crop
348
-	 * @return ISimpleFile
349
-	 *
350
-	 * @throws NotFoundException
351
-	 */
352
-	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
353
-		$path = $this->generatePath($width, $height, $crop);
354
-
355
-		return $previewFolder->getFile($path);
356
-	}
357
-
358
-	/**
359
-	 * Get the specific preview folder for this file
360
-	 *
361
-	 * @param File $file
362
-	 * @return ISimpleFolder
363
-	 */
364
-	private function getPreviewFolder(File $file) {
365
-		try {
366
-			$folder = $this->appData->getFolder($file->getId());
367
-		} catch (NotFoundException $e) {
368
-			$folder = $this->appData->newFolder($file->getId());
369
-		}
370
-
371
-		return $folder;
372
-	}
278
+        if ($height > $maxHeight) {
279
+            $ratio = $height / $maxHeight;
280
+            $height = $maxHeight;
281
+            $width /= $ratio;
282
+        }
283
+        if ($width > $maxWidth) {
284
+            $ratio = $width / $maxWidth;
285
+            $width = $maxWidth;
286
+            $height /= $ratio;
287
+        }
288
+
289
+        return [(int)round($width), (int)round($height)];
290
+    }
291
+
292
+    /**
293
+     * @param ISimpleFolder $previewFolder
294
+     * @param ISimpleFile $maxPreview
295
+     * @param int $width
296
+     * @param int $height
297
+     * @param bool $crop
298
+     * @param int $maxWidth
299
+     * @param int $maxHeight
300
+     * @return ISimpleFile
301
+     * @throws NotFoundException
302
+     */
303
+    private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
304
+        $preview = $this->helper->getImage($maxPreview);
305
+
306
+        if ($crop) {
307
+            if ($height !== $preview->height() && $width !== $preview->width()) {
308
+                //Resize
309
+                $widthR = $preview->width() / $width;
310
+                $heightR = $preview->height() / $height;
311
+
312
+                if ($widthR > $heightR) {
313
+                    $scaleH = $height;
314
+                    $scaleW = $maxWidth / $heightR;
315
+                } else {
316
+                    $scaleH = $maxHeight / $widthR;
317
+                    $scaleW = $width;
318
+                }
319
+                $preview->preciseResize(round($scaleW), round($scaleH));
320
+            }
321
+            $cropX = floor(abs($width - $preview->width()) * 0.5);
322
+            $cropY = 0;
323
+            $preview->crop($cropX, $cropY, $width, $height);
324
+        } else {
325
+            $preview->resize(max($width, $height));
326
+        }
327
+
328
+        if (!$preview->valid()) {
329
+            throw new \InvalidArgumentException('Failed to generate preview');
330
+        }
331
+
332
+        $path = $this->generatePath($width, $height, $crop);
333
+        try {
334
+            $file = $previewFolder->newFile($path);
335
+            $file->putContent($preview->data());
336
+        } catch (NotPermittedException $e) {
337
+            throw new NotFoundException();
338
+        }
339
+
340
+        return $file;
341
+    }
342
+
343
+    /**
344
+     * @param ISimpleFolder $previewFolder
345
+     * @param int $width
346
+     * @param int $height
347
+     * @param bool $crop
348
+     * @return ISimpleFile
349
+     *
350
+     * @throws NotFoundException
351
+     */
352
+    private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
353
+        $path = $this->generatePath($width, $height, $crop);
354
+
355
+        return $previewFolder->getFile($path);
356
+    }
357
+
358
+    /**
359
+     * Get the specific preview folder for this file
360
+     *
361
+     * @param File $file
362
+     * @return ISimpleFolder
363
+     */
364
+    private function getPreviewFolder(File $file) {
365
+        try {
366
+            $folder = $this->appData->getFolder($file->getId());
367
+        } catch (NotFoundException $e) {
368
+            $folder = $this->appData->newFolder($file->getId());
369
+        }
370
+
371
+        return $folder;
372
+    }
373 373
 }
Please login to merge, or discard this patch.