1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Posprint\Graphics; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Basic method for graphics classes |
7
|
|
|
* |
8
|
|
|
* @codeCoverageIgnore |
9
|
|
|
* @category NFePHP |
10
|
|
|
* @package Posprint |
11
|
|
|
* @copyright Copyright (c) 2016 |
12
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
13
|
|
|
* @author Roberto L. Machado <linux dot rlm at gmail dot com> |
14
|
|
|
* @link http://github.com/nfephp-org/posprint for the canonical source repository |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
abstract class Basic |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Image GD |
21
|
|
|
* |
22
|
|
|
* @var resource |
23
|
|
|
*/ |
24
|
|
|
protected $img; |
25
|
|
|
/** |
26
|
|
|
* Image Height |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $imgHeight = 0; |
31
|
|
|
/** |
32
|
|
|
* Image Width |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $imgWidth = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return int height of the image in pixels |
40
|
|
|
*/ |
41
|
2 |
|
public function getHeight() |
42
|
|
|
{ |
43
|
2 |
|
return $this->imgHeight; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return int Number of bytes to represent a row of this image |
48
|
|
|
*/ |
49
|
1 |
|
public function getHeightBytes() |
50
|
|
|
{ |
51
|
1 |
|
return (int)(($this->imgHeight + 7) / 8); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int Width of the image |
56
|
|
|
*/ |
57
|
1 |
|
public function getWidth() |
58
|
|
|
{ |
59
|
1 |
|
return $this->imgWidth; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int Number of bytes to represent a row of this image |
64
|
|
|
*/ |
65
|
1 |
|
public function getWidthBytes() |
66
|
|
|
{ |
67
|
1 |
|
return (int)(($this->imgWidth + 7) / 8); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* getDimImage |
72
|
|
|
* Get width and height of resource image |
73
|
|
|
* and save in properties |
74
|
|
|
* |
75
|
|
|
* @return array with dimentions of image |
76
|
|
|
*/ |
77
|
9 |
|
public function getDimImage() |
78
|
|
|
{ |
79
|
9 |
|
if (is_resource($this->img)) { |
80
|
8 |
|
$this->imgHeight = imagesy($this->img); |
81
|
8 |
|
$this->imgWidth = imagesx($this->img); |
82
|
8 |
|
} |
83
|
9 |
|
return ['height' => $this->imgHeight, 'width' => $this->imgWidth]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return boolean True if GD is supported, false otherwise (a wrapper for the version, for mocking in tests) |
88
|
|
|
*/ |
89
|
12 |
|
protected function isGdSupported() |
90
|
|
|
{ |
91
|
12 |
|
return $this->isGdLoaded(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return boolean True if GD is loaded, false otherwise |
96
|
|
|
*/ |
97
|
12 |
|
protected function isGdLoaded() |
98
|
|
|
{ |
99
|
12 |
|
$gdImgProcessing = extension_loaded('gd'); |
100
|
12 |
|
return $gdImgProcessing; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* identifyImg |
105
|
|
|
* Identifies image file type |
106
|
|
|
* |
107
|
|
|
* @param string $filename |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
8 |
|
protected function identifyImg($filename) |
111
|
|
|
{ |
112
|
8 |
|
$imgtype = exif_imagetype($filename); |
113
|
|
|
$aImgTypes = [ |
114
|
8 |
|
0 => 'NULL', |
115
|
8 |
|
1 => 'GIF', |
116
|
8 |
|
2 => 'JPEG', |
117
|
8 |
|
3 => 'PNG', |
118
|
8 |
|
4 => 'SWF', |
119
|
8 |
|
5 => 'PSD', |
120
|
8 |
|
6 => 'BMP', |
121
|
8 |
|
7 => 'TIFF_II', |
122
|
8 |
|
8 => 'TIFF_MM', |
123
|
8 |
|
9 => 'JPC', |
124
|
8 |
|
10 => 'JP2', |
125
|
8 |
|
11 => 'JPX', |
126
|
8 |
|
12 => 'JB2', |
127
|
8 |
|
13 => 'SWC', |
128
|
8 |
|
14 => 'IFF', |
129
|
8 |
|
15 => 'WBMP', |
130
|
8 |
|
16 => 'XBM', |
131
|
8 |
|
17 => 'ICO']; |
132
|
8 |
|
return $aImgTypes[$imgtype]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|