|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
|
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2023 |
|
7
|
|
|
* @package MW |
|
8
|
|
|
* @subpackage Media |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Aimeos\MW\Media; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Creates a new media object. |
|
17
|
|
|
* |
|
18
|
|
|
* @package MW |
|
19
|
|
|
* @subpackage Media |
|
20
|
|
|
*/ |
|
21
|
|
|
class Factory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Creates a new media object. |
|
25
|
|
|
* |
|
26
|
|
|
* Options for the factory are: |
|
27
|
|
|
* - image: Associative list of image related options |
|
28
|
|
|
* - application: Associative list of application related options |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Psr\Http\Message\StreamInterface|resource|string $file File resource, path to the file or file content |
|
31
|
|
|
* @param array $options Associative list of options for configuring the media class |
|
32
|
|
|
* @return \Aimeos\MW\Media\Iface Media object |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function get( $file, array $options = [] ) |
|
35
|
|
|
{ |
|
36
|
|
|
if( is_resource( $file ) ) |
|
37
|
|
|
{ |
|
38
|
|
|
if( ( $content = stream_get_contents( $file ) ) === false ) { |
|
39
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from stream' ) ); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
elseif( $file instanceof \Psr\Http\Message\StreamInterface ) |
|
43
|
|
|
{ |
|
44
|
|
|
$content = $file->getContents(); |
|
45
|
|
|
} |
|
46
|
|
|
elseif( is_string( $file ) ) |
|
47
|
|
|
{ |
|
48
|
|
|
if( strpos( $file, "\0" ) === false && is_file( $file ) ) |
|
49
|
|
|
{ |
|
50
|
|
|
if( ( $content = file_get_contents( $file ) ) === false ) { |
|
51
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $file ) ); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
else |
|
55
|
|
|
{ |
|
56
|
|
|
$content = $file; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
else |
|
60
|
|
|
{ |
|
61
|
|
|
throw new \Aimeos\MW\Media\Exception( 'Unsupported file parameter type' ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$finfo = new \finfo( FILEINFO_MIME_TYPE ); |
|
66
|
|
|
$mimetype = $finfo->buffer( $content ); |
|
67
|
|
|
$mime = explode( '/', $mimetype ); |
|
68
|
|
|
|
|
69
|
|
|
$type = $mime[0] === 'image' ? 'Image' : 'Application'; |
|
70
|
|
|
$name = $type === 'Image' && extension_loaded( 'imagick' ) ? 'Imagick' : 'Standard'; |
|
71
|
|
|
$name = ucfirst( $options[$mime[0]]['name'] ?? $name ); |
|
72
|
|
|
|
|
73
|
|
|
if( in_array( $mimetype, ['image/svg', 'image/svg+xml'] ) |
|
74
|
|
|
|| in_array( $mimetype, ['application/gzip', 'application/x-gzip'] ) |
|
75
|
|
|
&& is_string( $file ) && in_array( pathinfo( $file, PATHINFO_EXTENSION ), ['svg', 'svgz'] ) |
|
76
|
|
|
) { |
|
77
|
|
|
$mimetype = 'image/svg+xml'; |
|
78
|
|
|
$type = 'Image'; |
|
79
|
|
|
$name = 'Svg'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
if( ctype_alnum( $name ) === false ) { |
|
84
|
|
|
throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ) ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$interface = \Aimeos\MW\Media\Iface::class; |
|
88
|
|
|
$classname = '\Aimeos\MW\Media\\' . $type . '\\' . $name; |
|
89
|
|
|
|
|
90
|
|
|
return \Aimeos\Utils::create( $classname, [$content, $mimetype, $options], $interface ); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|