@@ -12,12 +12,12 @@ final class IMagickLuminanceSource extends LuminanceSource
12
12
private $ dataWidth ;
13
13
private $ dataHeight ;
14
14
/**
15
- * @var mixed|int
16
- */
15
+ * @var mixed|int
16
+ */
17
17
private $ left ;
18
18
/**
19
- * @var mixed|int
20
- */
19
+ * @var mixed|int
20
+ */
21
21
private $ top ;
22
22
private ?\Imagick $ image = null ;
23
23
@@ -66,13 +66,40 @@ public function _IMagickLuminanceSource(\Imagick $image, $width, $height): void
66
66
$ this ->top = 0 ;
67
67
$ this ->image = $ image ;
68
68
69
+ /**
70
+ * Converts shorthand memory notation value to bytes
71
+ * From http://php.net/manual/en/function.ini-get.php
72
+ *
73
+ * @param int $val Memory size shorthand notation string
74
+ */
75
+ function kmgStringToBytes (string $ val )
76
+ {
77
+ $ val = trim ($ val );
78
+ $ last = strtolower ($ val [strlen ($ val ) - 1 ]);
79
+ $ val = substr ($ val , 0 , -1 );
80
+ switch ($ last ) {
81
+ // The 'G' modifier is available since PHP 5.1.0
82
+ case 'g ' :
83
+ $ val *= 1024 ;
84
+ // no break
85
+ case 'm ' :
86
+ $ val *= 1024 ;
87
+ // no break
88
+ case 'k ' :
89
+ $ val *= 1024 ;
90
+ }
91
+ return $ val ;
92
+ }
69
93
70
94
// In order to measure pure decoding speed, we convert the entire image to a greyscale array
71
95
// up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
72
96
$ this ->luminances = [];
73
97
74
98
$ image ->setImageColorspace (\Imagick::COLORSPACE_GRAY );
75
- // $image->newPseudoImage(0, 0, "magick:rose");
99
+ // Check that we actually have enough space to do it
100
+ if ($ width * $ height * 16 * 3 > kmgStringToBytes (ini_get ('memory_limit ' ))) {
101
+ throw new \RuntimeException ("PHP Memory Limit does not allow pixel export. " );
102
+ }
76
103
$ pixels = $ image ->exportImagePixels (1 , 1 , $ width , $ height , "RGB " , \Imagick::PIXEL_CHAR );
77
104
78
105
$ array = [];
@@ -85,16 +112,15 @@ public function _IMagickLuminanceSource(\Imagick $image, $width, $height): void
85
112
$ b = $ pixels [$ i + 2 ] & 0xff ;
86
113
if ($ r == $ g && $ g == $ b ) {
87
114
// Image is already greyscale, so pick any channel.
88
-
89
- $ this ->luminances [] = $ r ;//(($r + 128) % 256) - 128;
115
+ $ this ->luminances [] = $ r ; //(($r + 128) % 256) - 128;
90
116
} else {
91
117
// Calculate luminance cheaply, favoring green.
92
- $ this ->luminances [] = ($ r + 2 * $ g + $ b ) / 4 ;//(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
118
+ $ this ->luminances [] = ($ r + 2 * $ g + $ b ) / 4 ; //(((($r + 2 * $g + $b) / 4) + 128) % 256) - 128;
93
119
}
94
120
}
95
121
}
96
122
97
-
123
+
98
124
public function getRow ($ y , $ row = null )
99
125
{
100
126
if ($ y < 0 || $ y >= $ this ->getHeight ()) {
@@ -110,7 +136,7 @@ public function getRow($y, $row = null)
110
136
return $ row ;
111
137
}
112
138
113
-
139
+
114
140
public function getMatrix ()
115
141
{
116
142
$ width = $ this ->getWidth ();
@@ -144,13 +170,13 @@ public function getMatrix()
144
170
return $ matrix ;
145
171
}
146
172
147
-
173
+
148
174
public function isCropSupported (): bool
149
175
{
150
176
return true ;
151
177
}
152
178
153
-
179
+
154
180
public function crop ($ left , $ top , $ width , $ height ): LuminanceSource
155
181
{
156
182
return $ this ->luminances ->cropImage ($ width , $ height , $ left , $ top );
0 commit comments