Image   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 62
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Cadmium\Framework\Image
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace {
11
12
	abstract class Image {
13
14
		/**
15
		 * Output an image
16
		 *
17
		 * @return bool : true on success or false if the given image is not a valid resource
18
		 */
19
20
		private static function output($image, string $mime, callable $outputter, array $params = []) : bool {
21
22
			if (!is_resource($image)) return false;
23
24
			Headers::sendNoCache(); Headers::sendContent($mime);
25
26
			$outputter($image, ...$params); imagedestroy($image);
27
28
			# ------------------------
29
30
			return true;
31
		}
32
33
		/**
34
		 * Output a JPEG image
35
		 *
36
		 * @param $quality ranges from 0 (worst) to 100 (best)
37
		 *
38
		 * @return bool : true on success or false if the given image is not a valid resource
39
		 */
40
41
		public static function outputJPEG($image, int $quality = 75) : bool {
42
43
			$params = [null, Number::forceInt($quality, 0, 100)];
44
45
			return self::output($image, MIME_TYPE_JPEG, 'imagejpeg', $params);
46
		}
47
48
		/**
49
		 * Output a PNG image
50
		 *
51
		 * @param $quality ranges from 0 (no compression) to 9
52
		 *
53
		 * @return bool : true on success or false if the given image is not a valid resource
54
		 */
55
56
		public static function outputPNG($image, int $quality = 4) : bool {
57
58
			$params = [null, Number::forceInt($quality, 0, 9)];
59
60
			return self::output($image, MIME_TYPE_PNG, 'imagepng', $params);
61
		}
62
63
		/**
64
		 * Output a GIF image
65
		 *
66
		 * @return bool : true on success or false if the given image is not a valid resource
67
		 */
68
69
		public static function outputGIF($image) : bool {
70
71
			return self::output($image, MIME_TYPE_GIF, 'imagegif');
72
		}
73
	}
74
}
75