18
18
*/
19
19
class Image extends AbstractElement
20
20
{
21
+ /**
22
+ * Image source type constants
23
+ */
24
+ const SOURCE_LOCAL = 'local ' ; // Local images
25
+ const SOURCE_GD = 'gd ' ; // Generated using GD
26
+ const SOURCE_ARCHIVE = 'archive ' ; // Image in archives zip://$archive#$image
27
+
21
28
/**
22
29
* Image source
23
30
*
24
31
* @var string
25
32
*/
26
33
private $ source ;
27
34
35
+ /**
36
+ * Source type: local|gd|archive
37
+ *
38
+ * @var string
39
+ */
40
+ private $ sourceType ;
41
+
28
42
/**
29
43
* Image style
30
44
*
@@ -199,49 +213,87 @@ public function getIsMemImage()
199
213
*/
200
214
private function checkImage ($ source )
201
215
{
202
- $ isArchive = strpos ($ source, ' zip:// ' ) !== false ;
216
+ $ this -> setSourceType ($ source) ;
203
217
204
- // Check is memory image
205
- if (stripos (strrev ($ source ), strrev ('.php ' )) === 0 ) {
206
- $ this ->isMemImage = true ;
207
- } elseif ($ isArchive ) {
208
- $ this ->isMemImage = false ;
209
- } else {
210
- $ this ->isMemImage = (filter_var ($ source , FILTER_VALIDATE_URL ) !== false );
211
- }
212
-
213
- // Define supported types
214
- if ($ this ->isMemImage ) {
215
- $ supportedTypes = array (
216
- IMAGETYPE_JPEG , IMAGETYPE_GIF , IMAGETYPE_PNG
217
- );
218
- } else {
219
- $ supportedTypes = array (
220
- IMAGETYPE_JPEG , IMAGETYPE_GIF , IMAGETYPE_PNG ,
221
- IMAGETYPE_BMP , IMAGETYPE_TIFF_II , IMAGETYPE_TIFF_MM
222
- );
223
- }
224
-
225
- // Check from zip file or actual file
226
- if ($ isArchive ) {
227
- $ imageData = $ this ->getArchivedImageSize ($ source );
218
+ // Check image data
219
+ if ($ this ->sourceType == self ::SOURCE_ARCHIVE ) {
220
+ $ imageData = $ this ->getArchiveImageSize ($ source );
228
221
} else {
229
222
$ imageData = @getimagesize ($ source );
230
223
}
231
-
232
- // Check if image exists by detecting image data
233
224
if (!is_array ($ imageData )) {
234
225
throw new InvalidImageException ();
235
226
}
236
- // Put image data into variables
237
227
list ($ actualWidth , $ actualHeight , $ imageType ) = $ imageData ;
238
- // Check if image type is supported
228
+
229
+ // Check image type support
230
+ $ supportedTypes = array (IMAGETYPE_JPEG , IMAGETYPE_GIF , IMAGETYPE_PNG );
231
+ if ($ this ->sourceType != self ::SOURCE_GD ) {
232
+ $ supportedTypes = array_merge ($ supportedTypes , array (IMAGETYPE_BMP , IMAGETYPE_TIFF_II , IMAGETYPE_TIFF_MM ));
233
+ }
239
234
if (!in_array ($ imageType , $ supportedTypes )) {
240
235
throw new UnsupportedImageTypeException ();
241
236
}
242
237
243
238
// Define image functions
244
239
$ this ->imageType = image_type_to_mime_type ($ imageType );
240
+ $ this ->setFunctions ();
241
+ $ this ->setProportionalSize ($ actualWidth , $ actualHeight );
242
+ }
243
+
244
+ /**
245
+ * Set source type
246
+ *
247
+ * @param string $source
248
+ */
249
+ private function setSourceType ($ source )
250
+ {
251
+ if (stripos (strrev ($ source ), strrev ('.php ' )) === 0 ) {
252
+ $ this ->isMemImage = true ;
253
+ $ this ->sourceType = self ::SOURCE_GD ;
254
+ } elseif (strpos ($ source , 'zip:// ' ) !== false ) {
255
+ $ this ->isMemImage = false ;
256
+ $ this ->sourceType = self ::SOURCE_ARCHIVE ;
257
+ } else {
258
+ $ this ->isMemImage = (filter_var ($ source , FILTER_VALIDATE_URL ) !== false );
259
+ $ this ->sourceType = $ this ->isMemImage ? self ::SOURCE_GD : self ::SOURCE_LOCAL ;
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Get image size from archive
265
+ *
266
+ * @param string $source
267
+ * @return array|null
268
+ */
269
+ private function getArchiveImageSize ($ source )
270
+ {
271
+ $ imageData = null ;
272
+ $ source = substr ($ source , 6 );
273
+ list ($ zipFilename , $ imageFilename ) = explode ('# ' , $ source );
274
+ $ tempFilename = tempnam (sys_get_temp_dir (), 'PHPWordImage ' );
275
+
276
+ $ zip = new \ZipArchive ();
277
+ if ($ zip ->open ($ zipFilename ) !== false ) {
278
+ if ($ zip ->locateName ($ imageFilename )) {
279
+ $ imageContent = $ zip ->getFromName ($ imageFilename );
280
+ if ($ imageContent !== false ) {
281
+ file_put_contents ($ tempFilename , $ imageContent );
282
+ $ imageData = @getimagesize ($ tempFilename );
283
+ unlink ($ tempFilename );
284
+ }
285
+ }
286
+ $ zip ->close ();
287
+ }
288
+
289
+ return $ imageData ;
290
+ }
291
+
292
+ /**
293
+ * Set image functions and extensions
294
+ */
295
+ private function setFunctions ()
296
+ {
245
297
switch ($ this ->imageType ) {
246
298
case 'image/png ' :
247
299
$ this ->imageCreateFunc = 'imagecreatefrompng ' ;
@@ -254,6 +306,7 @@ private function checkImage($source)
254
306
$ this ->imageExtension = 'gif ' ;
255
307
break ;
256
308
case 'image/jpeg ' :
309
+ case 'image/jpg ' :
257
310
$ this ->imageCreateFunc = 'imagecreatefromjpeg ' ;
258
311
$ this ->imageFunc = 'imagejpeg ' ;
259
312
$ this ->imageExtension = 'jpg ' ;
@@ -267,8 +320,13 @@ private function checkImage($source)
267
320
$ this ->imageExtension = 'tif ' ;
268
321
break ;
269
322
}
323
+ }
270
324
271
- // Check image width & height
325
+ /**
326
+ * Set proportional width/height if one dimension not available
327
+ */
328
+ private function setProportionalSize ($ actualWidth , $ actualHeight )
329
+ {
272
330
$ styleWidth = $ this ->style ->getWidth ();
273
331
$ styleHeight = $ this ->style ->getHeight ();
274
332
if (!($ styleWidth && $ styleHeight )) {
@@ -281,35 +339,5 @@ private function checkImage($source)
281
339
$ this ->style ->setWidth ($ actualWidth * ($ styleHeight / $ actualHeight ));
282
340
}
283
341
}
284
-
285
- }
286
-
287
- /**
288
- * Get image size from archive
289
- *
290
- * @param string $source
291
- * @return array|null
292
- */
293
- private function getArchivedImageSize ($ source )
294
- {
295
- $ imageData = null ;
296
- $ source = substr ($ source , 6 );
297
- list ($ zipFilename , $ imageFilename ) = explode ('# ' , $ source );
298
- $ tempFilename = tempnam (sys_get_temp_dir (), 'PHPWordImage ' );
299
-
300
- $ zip = new \ZipArchive ();
301
- if ($ zip ->open ($ zipFilename ) !== false ) {
302
- if ($ zip ->locateName ($ imageFilename )) {
303
- $ imageContent = $ zip ->getFromName ($ imageFilename );
304
- if ($ imageContent !== false ) {
305
- file_put_contents ($ tempFilename , $ imageContent );
306
- $ imageData = @getimagesize ($ tempFilename );
307
- unlink ($ tempFilename );
308
- }
309
- }
310
- $ zip ->close ();
311
- }
312
-
313
- return $ imageData ;
314
342
}
315
343
}
0 commit comments