Passed
Push — master ( cff939...bc9af2 )
by Sebastian
02:31
created
src/ImageHelper.php 1 patch
Indentation   +394 added lines, -394 removed lines patch added patch discarded remove patch
@@ -66,54 +66,54 @@  discard block
 block discarded – undo
66 66
     
67 67
     const ERROR_GD_LIBRARY_NOT_INSTALLED = 513024;
68 68
 
69
-   /**
70
-    * @var string
71
-    */
69
+    /**
70
+     * @var string
71
+     */
72 72
     protected $file;
73 73
 
74
-   /**
75
-    * @var ImageHelper_Size
76
-    */
74
+    /**
75
+     * @var ImageHelper_Size
76
+     */
77 77
     protected $info;
78 78
 
79
-   /**
80
-    * @var string
81
-    */
79
+    /**
80
+     * @var string
81
+     */
82 82
     protected $type;
83 83
 
84
-   /**
85
-    * @var resource|NULL
86
-    */
84
+    /**
85
+     * @var resource|NULL
86
+     */
87 87
     protected $newImage;
88 88
 
89
-   /**
90
-    * @var resource
91
-    */
89
+    /**
90
+     * @var resource
91
+     */
92 92
     protected $sourceImage;
93 93
 
94
-   /**
95
-    * @var int
96
-    */
94
+    /**
95
+     * @var int
96
+     */
97 97
     protected $width;
98 98
 
99
-   /**
100
-    * @var int
101
-    */
99
+    /**
100
+     * @var int
101
+     */
102 102
     protected $height;
103 103
 
104
-   /**
105
-    * @var int
106
-    */
104
+    /**
105
+     * @var int
106
+     */
107 107
     protected $newWidth = 0;
108 108
 
109
-   /**
110
-    * @var int
111
-    */
109
+    /**
110
+     * @var int
111
+     */
112 112
     protected $newHeight = 0;
113 113
 
114
-   /**
115
-    * @var int
116
-    */
114
+    /**
115
+     * @var int
116
+     */
117 117
     protected $quality = 85;
118 118
     
119 119
     protected static $imageTypes = array(
@@ -201,17 +201,17 @@  discard block
 block discarded – undo
201 201
         }
202 202
     }
203 203
 
204
-   /**
205
-    * Factory method: creates a new helper with a blank image.
206
-    * 
207
-    * @param integer $width
208
-    * @param integer $height
209
-    * @param string $type The target file type when saving
210
-    * @return ImageHelper
211
-    * @throws ImageHelper_Exception
212
-    *
213
-    * @see ImageHelper::ERROR_CANNOT_CREATE_IMAGE_OBJECT
214
-    */
204
+    /**
205
+     * Factory method: creates a new helper with a blank image.
206
+     * 
207
+     * @param integer $width
208
+     * @param integer $height
209
+     * @param string $type The target file type when saving
210
+     * @return ImageHelper
211
+     * @throws ImageHelper_Exception
212
+     *
213
+     * @see ImageHelper::ERROR_CANNOT_CREATE_IMAGE_OBJECT
214
+     */
215 215
     public static function createNew($width, $height, $type='png')
216 216
     {
217 217
         $img = imagecreatetruecolor($width, $height);
@@ -226,18 +226,18 @@  discard block
 block discarded – undo
226 226
         );
227 227
     }
228 228
     
229
-   /**
230
-    * Factory method: creates an image helper from an
231
-    * existing image resource.
232
-    *
233
-    * Note: while the resource is type independent, the
234
-    * type parameter is required for some methods, as well
235
-    * as to be able to save the image.
236
-    *
237
-    * @param resource $resource
238
-    * @param string $type The target image type, e.g. "jpeg", "png", etc.
239
-    * @return ImageHelper
240
-    */
229
+    /**
230
+     * Factory method: creates an image helper from an
231
+     * existing image resource.
232
+     *
233
+     * Note: while the resource is type independent, the
234
+     * type parameter is required for some methods, as well
235
+     * as to be able to save the image.
236
+     *
237
+     * @param resource $resource
238
+     * @param string $type The target image type, e.g. "jpeg", "png", etc.
239
+     * @return ImageHelper
240
+     */
241 241
     public static function createFromResource($resource, ?string $type=null)
242 242
     {
243 243
         self::requireResource($resource);
@@ -245,29 +245,29 @@  discard block
 block discarded – undo
245 245
         return new ImageHelper(null, $resource, $type);
246 246
     }
247 247
     
248
-   /**
249
-    * Factory method: creates an image helper from an
250
-    * image file on disk.
251
-    *
252
-    * @param string $path
253
-    * @return ImageHelper
254
-    */
248
+    /**
249
+     * Factory method: creates an image helper from an
250
+     * image file on disk.
251
+     *
252
+     * @param string $path
253
+     * @return ImageHelper
254
+     */
255 255
     public static function createFromFile($file)
256 256
     {
257 257
         return new ImageHelper($file);
258 258
     }
259 259
     
260
-   /**
261
-    * Sets a global image helper configuration value. Available
262
-    * configuration settings are:
263
-    * 
264
-    * <ul>
265
-    * <li><code>auto-memory-adjustment</code> <i>boolean</i> Whether totry and adjust the memory limit automatically so there will be enough to load/process the target image.</li>
266
-    * </ul>
267
-    * 
268
-    * @param string $name
269
-    * @param mixed $value
270
-    */
260
+    /**
261
+     * Sets a global image helper configuration value. Available
262
+     * configuration settings are:
263
+     * 
264
+     * <ul>
265
+     * <li><code>auto-memory-adjustment</code> <i>boolean</i> Whether totry and adjust the memory limit automatically so there will be enough to load/process the target image.</li>
266
+     * </ul>
267
+     * 
268
+     * @param string $name
269
+     * @param mixed $value
270
+     */
271 271
     public static function setConfig($name, $value)
272 272
     {
273 273
         if(isset(self::$config[$name])) {
@@ -275,22 +275,22 @@  discard block
 block discarded – undo
275 275
         }
276 276
     }
277 277
     
278
-   /**
279
-    * Shorthand for setting the automatic memory adjustment
280
-    * global configuration setting.
281
-    * 
282
-    * @param bool $enabled
283
-    */
278
+    /**
279
+     * Shorthand for setting the automatic memory adjustment
280
+     * global configuration setting.
281
+     * 
282
+     * @param bool $enabled
283
+     */
284 284
     public static function setAutoMemoryAdjustment($enabled=true)
285 285
     {
286 286
         self::setConfig('auto-memory-adjustment', $enabled);
287 287
     }
288 288
     
289
-   /**
290
-    * Duplicates an image resource.
291
-    * @param resource $img
292
-    * @return resource
293
-    */
289
+    /**
290
+     * Duplicates an image resource.
291
+     * @param resource $img
292
+     * @return resource
293
+     */
294 294
     protected function duplicateImage($img)
295 295
     {
296 296
         self::requireResource($img);
@@ -302,12 +302,12 @@  discard block
 block discarded – undo
302 302
         return $duplicate;
303 303
     }
304 304
     
305
-   /**
306
-    * Duplicates the current state of the image into a new
307
-    * image helper instance.
308
-    * 
309
-    * @return ImageHelper
310
-    */
305
+    /**
306
+     * Duplicates the current state of the image into a new
307
+     * image helper instance.
308
+     * 
309
+     * @return ImageHelper
310
+     */
311 311
     public function duplicate()
312 312
     {
313 313
         return ImageHelper::createFromResource($this->duplicateImage($this->newImage));
@@ -495,13 +495,13 @@  discard block
 block discarded – undo
495 495
         ));
496 496
     }
497 497
     
498
-   /**
499
-    * Resamples the image to a new width, maintaining
500
-    * aspect ratio.
501
-    * 
502
-    * @param int $width
503
-    * @return ImageHelper
504
-    */
498
+    /**
499
+     * Resamples the image to a new width, maintaining
500
+     * aspect ratio.
501
+     * 
502
+     * @param int $width
503
+     * @return ImageHelper
504
+     */
505 505
     public function resampleByWidth(int $width) : ImageHelper
506 506
     {
507 507
         $size = $this->getSizeByWidth($width);
@@ -511,12 +511,12 @@  discard block
 block discarded – undo
511 511
         return $this;
512 512
     }
513 513
 
514
-   /**
515
-    * Resamples the image by height, and creates a new image file on disk.
516
-    * 
517
-    * @param int $height
518
-    * @return ImageHelper
519
-    */
514
+    /**
515
+     * Resamples the image by height, and creates a new image file on disk.
516
+     * 
517
+     * @param int $height
518
+     * @return ImageHelper
519
+     */
520 520
     public function resampleByHeight($height) : ImageHelper
521 521
     {
522 522
         $size = $this->getSizeByHeight($height);
@@ -524,13 +524,13 @@  discard block
 block discarded – undo
524 524
         return $this->resampleImage($size->getWidth(), $size->getHeight());
525 525
     }
526 526
 
527
-   /**
528
-    * Resamples the image without keeping the aspect ratio.
529
-    * 
530
-    * @param int $width
531
-    * @param int $height
532
-    * @return ImageHelper
533
-    */
527
+    /**
528
+     * Resamples the image without keeping the aspect ratio.
529
+     * 
530
+     * @param int $width
531
+     * @param int $height
532
+     * @return ImageHelper
533
+     */
534 534
     public function resample(?int $width = null, ?int $height = null) : ImageHelper
535 535
     {
536 536
         if($this->isVector()) {
@@ -595,12 +595,12 @@  discard block
 block discarded – undo
595 595
     
596 596
     protected $alpha = false;
597 597
 
598
-   /**
599
-    * Configures the specified image resource to make it alpha compatible.
600
-    * 
601
-    * @param resource $canvas
602
-    * @param bool $fill Whether to fill the whole canvas with the transparency
603
-    */
598
+    /**
599
+     * Configures the specified image resource to make it alpha compatible.
600
+     * 
601
+     * @param resource $canvas
602
+     * @param bool $fill Whether to fill the whole canvas with the transparency
603
+     */
604 604
     public static function addAlphaSupport($canvas, $fill=true)
605 605
     {
606 606
         self::requireResource($canvas);
@@ -715,12 +715,12 @@  discard block
 block discarded – undo
715 715
         $this->quality = $quality * 1;
716 716
     }
717 717
 
718
-   /**
719
-    * Attempts to adjust the memory to the required size
720
-    * to work with the current image.
721
-    * 
722
-    * @return boolean
723
-    */
718
+    /**
719
+     * Attempts to adjust the memory to the required size
720
+     * to work with the current image.
721
+     * 
722
+     * @return boolean
723
+     */
724 724
     protected function adjustMemory() : bool
725 725
     {
726 726
         if(!self::$config['auto-memory-adjustment']) {
@@ -760,27 +760,27 @@  discard block
 block discarded – undo
760 760
         return false;
761 761
     }
762 762
 
763
-   /**
764
-    * Stretches the image to the specified dimensions.
765
-    * 
766
-    * @param int $width
767
-    * @param int $height
768
-    * @return ImageHelper
769
-    */
763
+    /**
764
+     * Stretches the image to the specified dimensions.
765
+     * 
766
+     * @param int $width
767
+     * @param int $height
768
+     * @return ImageHelper
769
+     */
770 770
     public function stretch(int $width, int $height) : ImageHelper
771 771
     {
772 772
         return $this->resampleImage($width, $height);
773 773
     }
774 774
 
775
-   /**
776
-    * Creates a new image from the current image,
777
-    * resampling it to the new size.
778
-    * 
779
-    * @param int $newWidth
780
-    * @param int $newHeight   
781
-    * @throws ImageHelper_Exception
782
-    * @return ImageHelper
783
-    */
775
+    /**
776
+     * Creates a new image from the current image,
777
+     * resampling it to the new size.
778
+     * 
779
+     * @param int $newWidth
780
+     * @param int $newHeight   
781
+     * @throws ImageHelper_Exception
782
+     * @return ImageHelper
783
+     */
784 784
     protected function resampleImage(int $newWidth, int $newHeight) : ImageHelper
785 785
     {
786 786
         if($this->isVector()) {
@@ -920,31 +920,31 @@  discard block
 block discarded – undo
920 920
         readfile($imageFile);
921 921
     }
922 922
     
923
-   /**
924
-    * Displays the current image.
925
-    */
923
+    /**
924
+     * Displays the current image.
925
+     */
926 926
     public function display()
927 927
     {
928 928
         $this->displayImageStream($this->newImage, $this->getType(), $this->resolveQuality());
929 929
     }
930 930
     
931
-   /**
932
-    * Trims the current loaded image.
933
-    * 
934
-    * @param array $color A color definition, as an associative array with red, green, and blue keys. If not specified, the color at pixel position 0,0 will be used.
935
-    */
931
+    /**
932
+     * Trims the current loaded image.
933
+     * 
934
+     * @param array $color A color definition, as an associative array with red, green, and blue keys. If not specified, the color at pixel position 0,0 will be used.
935
+     */
936 936
     public function trim($color=null)
937 937
     {
938 938
         return $this->trimImage($this->newImage, $color);
939 939
     }
940 940
     
941
-   /**
942
-    * Retrieves a color definition by its index.
943
-    * 
944
-    * @param resource $img A valid image resource.
945
-    * @param int $colorIndex The color index, as returned by imagecolorat for example.
946
-    * @return array An array with red, green, blue and alpha keys.
947
-    */
941
+    /**
942
+     * Retrieves a color definition by its index.
943
+     * 
944
+     * @param resource $img A valid image resource.
945
+     * @param int $colorIndex The color index, as returned by imagecolorat for example.
946
+     * @return array An array with red, green, blue and alpha keys.
947
+     */
948 948
     public function getIndexedColors($img, int $colorIndex) : array
949 949
     {
950 950
         $color = imagecolorsforindex($img, $colorIndex);
@@ -962,14 +962,14 @@  discard block
 block discarded – undo
962 962
         );
963 963
     }
964 964
         
965
-   /**
966
-    * Trims the specified image resource by removing the specified color.
967
-    * Also works with transparency.
968
-    * 
969
-    * @param resource $img
970
-    * @param array $color A color definition, as an associative array with red, green, blue and alpha keys. If not specified, the color at pixel position 0,0 will be used.
971
-    * @return ImageHelper
972
-    */
965
+    /**
966
+     * Trims the specified image resource by removing the specified color.
967
+     * Also works with transparency.
968
+     * 
969
+     * @param resource $img
970
+     * @param array $color A color definition, as an associative array with red, green, blue and alpha keys. If not specified, the color at pixel position 0,0 will be used.
971
+     * @return ImageHelper
972
+     */
973 973
     protected function trimImage($img, ?array $color=null) : ImageHelper
974 974
     {
975 975
         if($this->isVector()) {
@@ -1057,12 +1057,12 @@  discard block
 block discarded – undo
1057 1057
         return $this;
1058 1058
     }
1059 1059
     
1060
-   /**
1061
-    * Sets the new image after a transformation operation:
1062
-    * automatically adjusts the new size information.
1063
-    * 
1064
-    * @param resource $image
1065
-    */
1060
+    /**
1061
+     * Sets the new image after a transformation operation:
1062
+     * automatically adjusts the new size information.
1063
+     * 
1064
+     * @param resource $image
1065
+     */
1066 1066
     protected function setNewImage($image)
1067 1067
     {
1068 1068
         self::requireResource($image);
@@ -1072,12 +1072,12 @@  discard block
 block discarded – undo
1072 1072
         $this->newHeight= imagesy($image);
1073 1073
     }
1074 1074
     
1075
-   /**
1076
-    * Requires the subject to be a resource.
1077
-    * 
1078
-    * @param resource $subject
1079
-    * @throws ImageHelper_Exception
1080
-    */
1075
+    /**
1076
+     * Requires the subject to be a resource.
1077
+     * 
1078
+     * @param resource $subject
1079
+     * @throws ImageHelper_Exception
1080
+     */
1081 1081
     protected static function requireResource($subject)
1082 1082
     {
1083 1083
         if(is_resource($subject)) {
@@ -1094,14 +1094,14 @@  discard block
 block discarded – undo
1094 1094
         );
1095 1095
     }
1096 1096
     
1097
-   /**
1098
-    * Creates a new image resource, with transparent background.
1099
-    * 
1100
-    * @param int $width
1101
-    * @param int $height
1102
-    * @throws ImageHelper_Exception
1103
-    * @return resource
1104
-    */
1097
+    /**
1098
+     * Creates a new image resource, with transparent background.
1099
+     * 
1100
+     * @param int $width
1101
+     * @param int $height
1102
+     * @throws ImageHelper_Exception
1103
+     * @return resource
1104
+     */
1105 1105
     protected function createNewImage(int $width, int $height)
1106 1106
     {
1107 1107
         $img = imagecreatetruecolor($width, $height);
@@ -1120,54 +1120,54 @@  discard block
 block discarded – undo
1120 1120
         return $img;
1121 1121
     }
1122 1122
     
1123
-   /**
1124
-    * Whether the two specified colors are the same.
1125
-    * 
1126
-    * @param array $a
1127
-    * @param array $b
1128
-    * @return boolean
1129
-    */
1130
-	protected function colorsMatch($a, $b) : bool
1131
-	{
1132
-		$parts = array('red', 'green', 'blue');
1133
-		foreach($parts as $part) {
1134
-			if($a[$part] != $b[$part]) {
1135
-				return false;
1136
-			}
1137
-		} 
1123
+    /**
1124
+     * Whether the two specified colors are the same.
1125
+     * 
1126
+     * @param array $a
1127
+     * @param array $b
1128
+     * @return boolean
1129
+     */
1130
+    protected function colorsMatch($a, $b) : bool
1131
+    {
1132
+        $parts = array('red', 'green', 'blue');
1133
+        foreach($parts as $part) {
1134
+            if($a[$part] != $b[$part]) {
1135
+                return false;
1136
+            }
1137
+        } 
1138 1138
 		
1139
-		return true;
1140
-	}
1139
+        return true;
1140
+    }
1141 1141
 	
1142
-	public function fillWhite($x=0, $y=0)
1143
-	{
1144
-	    $this->addRGBColor('white', 255, 255, 255);
1142
+    public function fillWhite($x=0, $y=0)
1143
+    {
1144
+        $this->addRGBColor('white', 255, 255, 255);
1145 1145
         return $this->fill('white', $x, $y);
1146
-	}
1146
+    }
1147 1147
 	
1148
-	public function fillTransparent() : ImageHelper
1149
-	{
1148
+    public function fillTransparent() : ImageHelper
1149
+    {
1150 1150
         $this->enableAlpha();
1151 1151
 	    
1152
-	    self::fillImageTransparent($this->newImage);
1152
+        self::fillImageTransparent($this->newImage);
1153 1153
 	    
1154
-	    return $this;
1155
-	}
1154
+        return $this;
1155
+    }
1156 1156
 	
1157
-	public static function fillImageTransparent($resource)
1158
-	{
1159
-	    self::requireResource($resource);
1157
+    public static function fillImageTransparent($resource)
1158
+    {
1159
+        self::requireResource($resource);
1160 1160
 	    
1161
-	    $transparent = imagecolorallocatealpha($resource, 89, 14, 207, 127);
1162
-	    imagecolortransparent ($resource, $transparent);
1163
-	    imagefill($resource, 0, 0, $transparent);
1164
-	}
1161
+        $transparent = imagecolorallocatealpha($resource, 89, 14, 207, 127);
1162
+        imagecolortransparent ($resource, $transparent);
1163
+        imagefill($resource, 0, 0, $transparent);
1164
+    }
1165 1165
 	
1166
-	public function fill($colorName, $x=0, $y=0)
1167
-	{
1168
-	    imagefill($this->newImage, $x, $y, $this->colors[$colorName]);
1169
-	    return $this;
1170
-	}
1166
+    public function fill($colorName, $x=0, $y=0)
1167
+    {
1168
+        imagefill($this->newImage, $x, $y, $this->colors[$colorName]);
1169
+        return $this;
1170
+    }
1171 1171
 	
1172 1172
     protected $colors = array();
1173 1173
 
@@ -1188,9 +1188,9 @@  discard block
 block discarded – undo
1188 1188
         return $this;
1189 1189
     }
1190 1190
     
1191
-   /**
1192
-    * @return resource
1193
-    */
1191
+    /**
1192
+     * @return resource
1193
+     */
1194 1194
     public function getImage()
1195 1195
     {
1196 1196
         return $this->newImage;
@@ -1208,27 +1208,27 @@  discard block
 block discarded – undo
1208 1208
         return $this;
1209 1209
     }
1210 1210
     
1211
-   /**
1212
-    * Retrieves the size of the image.
1213
-    * 
1214
-    * @param bool $exception Whether to trigger an exception when the image does not exist
1215
-    * @return ImageHelper_Size
1216
-    * @throws ImageHelper_Exception
1217
-    * @see ImageHelper::ERROR_CANNOT_GET_IMAGE_SIZE
1218
-    */
1219
-	public function getSize() : ImageHelper_Size
1211
+    /**
1212
+     * Retrieves the size of the image.
1213
+     * 
1214
+     * @param bool $exception Whether to trigger an exception when the image does not exist
1215
+     * @return ImageHelper_Size
1216
+     * @throws ImageHelper_Exception
1217
+     * @see ImageHelper::ERROR_CANNOT_GET_IMAGE_SIZE
1218
+     */
1219
+    public function getSize() : ImageHelper_Size
1220 1220
     {
1221
-	    return self::getImageSize($this->newImage);
1221
+        return self::getImageSize($this->newImage);
1222 1222
     }
1223 1223
     
1224 1224
     protected $TTFFile;
1225 1225
     
1226
-   /**
1227
-    * Sets the TTF font file to use for text operations.
1228
-    * 
1229
-    * @param string $filePath
1230
-    * @return ImageHelper
1231
-    */
1226
+    /**
1227
+     * Sets the TTF font file to use for text operations.
1228
+     * 
1229
+     * @param string $filePath
1230
+     * @return ImageHelper
1231
+     */
1232 1232
     public function setFontTTF($filePath)
1233 1233
     {
1234 1234
         $this->TTFFile = $filePath;
@@ -1300,63 +1300,63 @@  discard block
 block discarded – undo
1300 1300
             return;
1301 1301
         }
1302 1302
         
1303
-	    throw new ImageHelper_Exception(
1303
+        throw new ImageHelper_Exception(
1304 1304
             'No true type font specified',
1305 1305
             'This functionality requires a TTF font file to be specified with the [setFontTTF] method.',
1306 1306
             self::ERROR_NO_TRUE_TYPE_FONT_SET    
1307 1307
         );
1308 1308
     }
1309 1309
     
1310
-   /**
1311
-	 * Retrieves the size of an image file on disk, or
1312
-	 * an existing image resource.
1313
-	 *
1314
-	 * <pre>
1315
-	 * array(
1316
-	 *     0: (width),
1317
-	 *     1: (height),
1318
-	 *     "channels": the amount of channels
1319
-	 *     "bits": bits per channel
1310
+    /**
1311
+     * Retrieves the size of an image file on disk, or
1312
+     * an existing image resource.
1313
+     *
1314
+     * <pre>
1315
+     * array(
1316
+     *     0: (width),
1317
+     *     1: (height),
1318
+     *     "channels": the amount of channels
1319
+     *     "bits": bits per channel
1320 1320
      * )     
1321
-	 * </pre>
1322
-	 *
1323
-	 * @param string|resource $pathOrResource
1324
-	 * @return ImageHelper_Size Size object, can also be accessed like the traditional array from getimagesize
1325
-	 * @see ImageHelper_Size
1326
-	 * @throws ImageHelper_Exception
1327
-	 * @see ImageHelper::ERROR_CANNOT_GET_IMAGE_SIZE
1328
-	 * @see ImageHelper::ERROR_CANNOT_READ_SVG_IMAGE
1329
-	 * @see ImageHelper::ERROR_SVG_SOURCE_VIEWBOX_MISSING
1330
-	 * @see ImageHelper::ERROR_SVG_VIEWBOX_INVALID
1331
-	 */
1332
-	public static function getImageSize($pathOrResource) : ImageHelper_Size
1333
-	{
1334
-	    if(is_resource($pathOrResource)) 
1335
-	    {
1336
-	        return new ImageHelper_Size(array(
1337
-	            'width' => imagesx($pathOrResource),
1338
-	            'height' => imagesy($pathOrResource),
1339
-	            'channels' => 1,
1340
-	            'bits' => 8
1341
-	        ));
1342
-	    }
1321
+     * </pre>
1322
+     *
1323
+     * @param string|resource $pathOrResource
1324
+     * @return ImageHelper_Size Size object, can also be accessed like the traditional array from getimagesize
1325
+     * @see ImageHelper_Size
1326
+     * @throws ImageHelper_Exception
1327
+     * @see ImageHelper::ERROR_CANNOT_GET_IMAGE_SIZE
1328
+     * @see ImageHelper::ERROR_CANNOT_READ_SVG_IMAGE
1329
+     * @see ImageHelper::ERROR_SVG_SOURCE_VIEWBOX_MISSING
1330
+     * @see ImageHelper::ERROR_SVG_VIEWBOX_INVALID
1331
+     */
1332
+    public static function getImageSize($pathOrResource) : ImageHelper_Size
1333
+    {
1334
+        if(is_resource($pathOrResource)) 
1335
+        {
1336
+            return new ImageHelper_Size(array(
1337
+                'width' => imagesx($pathOrResource),
1338
+                'height' => imagesy($pathOrResource),
1339
+                'channels' => 1,
1340
+                'bits' => 8
1341
+            ));
1342
+        }
1343 1343
 	    
1344
-	    $type = self::getFileImageType($pathOrResource);
1344
+        $type = self::getFileImageType($pathOrResource);
1345 1345
 	    
1346
-	    $info = false;
1347
-	    $method = 'getImageSize_'.$type;
1348
-	    if(method_exists(__CLASS__, $method)) 
1349
-	    {
1350
-	        $info = call_user_func(array(__CLASS__, $method), $pathOrResource);
1351
-	    } 
1352
-	    else 
1353
-	    {
1354
-	        $info = getimagesize($pathOrResource);
1355
-	    }
1346
+        $info = false;
1347
+        $method = 'getImageSize_'.$type;
1348
+        if(method_exists(__CLASS__, $method)) 
1349
+        {
1350
+            $info = call_user_func(array(__CLASS__, $method), $pathOrResource);
1351
+        } 
1352
+        else 
1353
+        {
1354
+            $info = getimagesize($pathOrResource);
1355
+        }
1356 1356
 	    
1357
-	    if($info !== false) {
1358
-	        return new ImageHelper_Size($info);
1359
-	    }
1357
+        if($info !== false) {
1358
+            return new ImageHelper_Size($info);
1359
+        }
1360 1360
 	    
1361 1361
         throw new ImageHelper_Exception(
1362 1362
             'Error opening image file',
@@ -1366,96 +1366,96 @@  discard block
 block discarded – undo
1366 1366
             ),
1367 1367
             self::ERROR_CANNOT_GET_IMAGE_SIZE
1368 1368
         );
1369
-	}
1369
+    }
1370 1370
 	
1371
-   /**
1372
-    * @param string $imagePath
1373
-    * @throws ImageHelper_Exception
1374
-    * @return array
1375
-    * 
1376
-    * @todo This should return a ImageHelper_Size instance.
1377
-    */
1378
-	protected static function getImageSize_svg(string $imagePath) : array
1379
-	{
1380
-	    $xml = XMLHelper::createSimplexml();
1381
-	    $xml->loadFile($imagePath);
1371
+    /**
1372
+     * @param string $imagePath
1373
+     * @throws ImageHelper_Exception
1374
+     * @return array
1375
+     * 
1376
+     * @todo This should return a ImageHelper_Size instance.
1377
+     */
1378
+    protected static function getImageSize_svg(string $imagePath) : array
1379
+    {
1380
+        $xml = XMLHelper::createSimplexml();
1381
+        $xml->loadFile($imagePath);
1382 1382
 	    
1383
-	    if($xml->hasErrors()) {
1384
-	        throw new ImageHelper_Exception(
1385
-	            'Error opening SVG image',
1386
-	            sprintf(
1387
-	                'The XML content of the image [%s] could not be parsed.',
1388
-	                $imagePath
1383
+        if($xml->hasErrors()) {
1384
+            throw new ImageHelper_Exception(
1385
+                'Error opening SVG image',
1386
+                sprintf(
1387
+                    'The XML content of the image [%s] could not be parsed.',
1388
+                    $imagePath
1389 1389
                 ),
1390
-	            self::ERROR_CANNOT_READ_SVG_IMAGE
1390
+                self::ERROR_CANNOT_READ_SVG_IMAGE
1391 1391
             );
1392
-	    }
1392
+        }
1393 1393
 	    
1394
-	    $data = $xml->toArray();
1395
-	    $xml->dispose();
1396
-	    unset($xml);
1394
+        $data = $xml->toArray();
1395
+        $xml->dispose();
1396
+        unset($xml);
1397 1397
 	    
1398
-	    if(!isset($data['@attributes']) || !isset($data['@attributes']['viewBox'])) {
1399
-	        throw new ImageHelper_Exception(
1400
-	            'SVG Image is corrupted',
1401
-	            sprintf(
1402
-	                'The [viewBox] attribute is missing in the XML of the image at path [%s].',
1403
-	                $imagePath
1398
+        if(!isset($data['@attributes']) || !isset($data['@attributes']['viewBox'])) {
1399
+            throw new ImageHelper_Exception(
1400
+                'SVG Image is corrupted',
1401
+                sprintf(
1402
+                    'The [viewBox] attribute is missing in the XML of the image at path [%s].',
1403
+                    $imagePath
1404 1404
                 ),
1405
-	            self::ERROR_SVG_SOURCE_VIEWBOX_MISSING
1405
+                self::ERROR_SVG_SOURCE_VIEWBOX_MISSING
1406 1406
             );
1407
-	    }
1407
+        }
1408 1408
 	    
1409
-	    $svgWidth = parseNumber($data['@attributes']['width'])->getNumber();
1410
-	    $svgHeight = parseNumber($data['@attributes']['height'])->getNumber();
1409
+        $svgWidth = parseNumber($data['@attributes']['width'])->getNumber();
1410
+        $svgHeight = parseNumber($data['@attributes']['height'])->getNumber();
1411 1411
 	    
1412
-	    $viewBox = str_replace(' ', ',', $data['@attributes']['viewBox']);
1413
-	    $viewBox = explode(',', $viewBox);
1414
-	    if(count($viewBox) != 4) {
1415
-	        throw new ImageHelper_Exception(
1416
-	            'SVG image has an invalid viewBox attribute',
1417
-	            sprintf(
1418
-	               'The [viewBox] attribute does not have an expected value: [%s] in path [%s].',
1419
-	                $viewBox,
1420
-	                $imagePath
1412
+        $viewBox = str_replace(' ', ',', $data['@attributes']['viewBox']);
1413
+        $viewBox = explode(',', $viewBox);
1414
+        if(count($viewBox) != 4) {
1415
+            throw new ImageHelper_Exception(
1416
+                'SVG image has an invalid viewBox attribute',
1417
+                sprintf(
1418
+                    'The [viewBox] attribute does not have an expected value: [%s] in path [%s].',
1419
+                    $viewBox,
1420
+                    $imagePath
1421 1421
                 ),
1422
-	            self::ERROR_SVG_VIEWBOX_INVALID
1422
+                self::ERROR_SVG_VIEWBOX_INVALID
1423 1423
             );
1424
-	    }
1424
+        }
1425 1425
 	    
1426
-	    $boxWidth = $viewBox[2];
1427
-	    $boxHeight = $viewBox[3];
1426
+        $boxWidth = $viewBox[2];
1427
+        $boxHeight = $viewBox[3];
1428 1428
 	    
1429
-	    // calculate the x and y units of the document: 
1430
-	    // @see http://tutorials.jenkov.com/svg/svg-viewport-view-box.html#viewbox
1431
-	    //
1432
-	    // The viewbox combined with the width and heigt of the svg
1433
-	    // allow calculating how many pixels are in one unit of the 
1434
-	    // width and height of the document.
1429
+        // calculate the x and y units of the document: 
1430
+        // @see http://tutorials.jenkov.com/svg/svg-viewport-view-box.html#viewbox
1431
+        //
1432
+        // The viewbox combined with the width and heigt of the svg
1433
+        // allow calculating how many pixels are in one unit of the 
1434
+        // width and height of the document.
1435 1435
         //
1436
-	    $xUnits = $svgWidth / $boxWidth;
1437
-	    $yUnits = $svgHeight / $boxHeight;
1436
+        $xUnits = $svgWidth / $boxWidth;
1437
+        $yUnits = $svgHeight / $boxHeight;
1438 1438
 	    
1439
-	    $pxWidth = $xUnits * $svgWidth;
1440
-	    $pxHeight = $yUnits * $svgHeight;
1439
+        $pxWidth = $xUnits * $svgWidth;
1440
+        $pxHeight = $yUnits * $svgHeight;
1441 1441
 	    
1442
-	    return array(
1443
-	        $pxWidth,
1444
-	        $pxHeight,
1445
-	        'bits' => 8
1446
-	    );
1447
-	}
1442
+        return array(
1443
+            $pxWidth,
1444
+            $pxHeight,
1445
+            'bits' => 8
1446
+        );
1447
+    }
1448 1448
 	
1449
-	/**
1450
-    * Crops the image to the specified width and height, optionally
1451
-    * specifying the origin position to crop from.
1452
-    * 
1453
-    * @param integer $width
1454
-    * @param integer $height
1455
-    * @param integer $x
1456
-    * @param integer $y
1457
-    * @return ImageHelper
1458
-    */
1449
+    /**
1450
+     * Crops the image to the specified width and height, optionally
1451
+     * specifying the origin position to crop from.
1452
+     * 
1453
+     * @param integer $width
1454
+     * @param integer $height
1455
+     * @param integer $x
1456
+     * @param integer $y
1457
+     * @return ImageHelper
1458
+     */
1459 1459
     public function crop(int $width, int $height, int $x=0, int $y=0) : ImageHelper
1460 1460
     {
1461 1461
         $new = $this->createNewImage($width, $height);
@@ -1477,14 +1477,14 @@  discard block
 block discarded – undo
1477 1477
         return $this->newHeight;
1478 1478
     }
1479 1479
 
1480
-   /**
1481
-    * Calculates the average color value used in 
1482
-    * the image. Returns an associative array
1483
-    * with the red, green, blue and alpha components.
1484
-    * 
1485
-    * @param int $format The format in which to return the color value.
1486
-    * @return array|string
1487
-    */
1480
+    /**
1481
+     * Calculates the average color value used in 
1482
+     * the image. Returns an associative array
1483
+     * with the red, green, blue and alpha components.
1484
+     * 
1485
+     * @param int $format The format in which to return the color value.
1486
+     * @return array|string
1487
+     */
1488 1488
     public function calcAverageColor(int $format=self::COLORFORMAT_RGB)
1489 1489
     {
1490 1490
         $image = $this->duplicate();
@@ -1507,18 +1507,18 @@  discard block
 block discarded – undo
1507 1507
     
1508 1508
     const COLORFORMAT_HEX = 2;
1509 1509
     
1510
-   /**
1511
-    * Retrieves the color value at the specified pixel
1512
-    * coordinates in the image.
1513
-    * 
1514
-    * @param int $x
1515
-    * @param int $y
1516
-    * @param int $format The format in which to return the color value.
1517
-    * @return array|string
1518
-    * 
1519
-    * @see ImageHelper::COLORFORMAT_RGB
1520
-    * @see ImageHelper::COLORFORMAT_HEX
1521
-    */
1510
+    /**
1511
+     * Retrieves the color value at the specified pixel
1512
+     * coordinates in the image.
1513
+     * 
1514
+     * @param int $x
1515
+     * @param int $y
1516
+     * @param int $format The format in which to return the color value.
1517
+     * @return array|string
1518
+     * 
1519
+     * @see ImageHelper::COLORFORMAT_RGB
1520
+     * @see ImageHelper::COLORFORMAT_HEX
1521
+     */
1522 1522
     public function getColorAt(int $x, int $y, int $format=self::COLORFORMAT_RGB)
1523 1523
     {
1524 1524
         if($x > $this->getWidth() || $y > $this->getHeight()) 
@@ -1546,21 +1546,21 @@  discard block
 block discarded – undo
1546 1546
         return $rgb;
1547 1547
     }
1548 1548
     
1549
-   /**
1550
-    * Converts an RGB value to its luminance equivalent.
1551
-    * 
1552
-    * @param array $rgb
1553
-    * @return integer Integer, from 0 to 255 (0=black, 255=white)
1554
-    */
1549
+    /**
1550
+     * Converts an RGB value to its luminance equivalent.
1551
+     * 
1552
+     * @param array $rgb
1553
+     * @return integer Integer, from 0 to 255 (0=black, 255=white)
1554
+     */
1555 1555
     public static function rgb2luma($rgb)
1556 1556
     {
1557 1557
         return (($rgb['red']*2)+$rgb['blue']+($rgb['green']*3))/6;
1558 1558
     }
1559 1559
     
1560
-   /**
1561
-    * Retrieves the brightness of the image, in percent.
1562
-    * @return number
1563
-    */
1560
+    /**
1561
+     * Retrieves the brightness of the image, in percent.
1562
+     * @return number
1563
+     */
1564 1564
     public function getBrightness()
1565 1565
     {
1566 1566
         $luma = self::rgb2luma($this->calcAverageColor());
@@ -1568,15 +1568,15 @@  discard block
 block discarded – undo
1568 1568
         return $percent;
1569 1569
     }
1570 1570
     
1571
-   /**
1572
-    * Retrieves an md5 hash of the source image file.
1573
-    * 
1574
-    * NOTE: Only works when the helper has been created
1575
-    * from a file. Otherwise an exception is thrown.
1576
-    * 
1577
-    * @return string
1578
-    * @throws ImageHelper_Exception
1579
-    */
1571
+    /**
1572
+     * Retrieves an md5 hash of the source image file.
1573
+     * 
1574
+     * NOTE: Only works when the helper has been created
1575
+     * from a file. Otherwise an exception is thrown.
1576
+     * 
1577
+     * @return string
1578
+     * @throws ImageHelper_Exception
1579
+     */
1580 1580
     public function getHash()
1581 1581
     {
1582 1582
         ob_start();
Please login to merge, or discard this patch.