1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Metaways Infosystems GmbH, 2014 |
5
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2017 |
7
|
|
|
* @package MW |
8
|
|
|
* @subpackage Media |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MW\Media\Image; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default image class using GDLib. |
17
|
|
|
* |
18
|
|
|
* @package MW |
19
|
|
|
* @subpackage Media |
20
|
|
|
*/ |
21
|
|
|
class Standard |
22
|
|
|
extends \Aimeos\MW\Media\Image\Base |
|
|
|
|
23
|
|
|
implements \Aimeos\MW\Media\Image\Iface |
24
|
|
|
{ |
25
|
|
|
private $image; |
26
|
|
|
private $origimage; |
27
|
|
|
private $options; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initializes the new image object. |
32
|
|
|
* |
33
|
|
|
* @param string $filename Name of the media file |
34
|
|
|
* @param string $mimetype Mime type of the media data |
35
|
|
|
* @param array $options Associative list of configuration options |
36
|
|
|
* @throws \Aimeos\MW\Media\Exception If image couldn't be retrieved from the given file name |
37
|
|
|
*/ |
38
|
|
|
public function __construct( $filename, $mimetype, array $options ) |
39
|
|
|
{ |
40
|
|
|
parent::__construct( $filename, $mimetype ); |
41
|
|
|
|
42
|
|
|
if( ( $content = @file_get_contents( $filename ) ) === false ) { |
43
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to read from file "%1$s"', $filename ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if( ( $this->image = @imagecreatefromstring( $content ) ) === false ) { |
47
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'The image type in "%1$s" seems to be not supported by gdlib.', $filename) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if( imagealphablending( $this->image, false ) === false ) { |
51
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagealphablending)') ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->options = $options; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Cleans up |
60
|
|
|
*/ |
61
|
|
|
public function __destruct() |
62
|
|
|
{ |
63
|
|
|
if( $this->origimage ) { |
64
|
|
|
imagedestroy( $this->origimage ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if( $this->image ) { |
68
|
|
|
imagedestroy( $this->image ); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Stores the media data at the given file name. |
75
|
|
|
* |
76
|
|
|
* @param string $filename Name of the file to save the media data into |
77
|
|
|
* @param string $mimetype Mime type to save the image as |
78
|
|
|
* @throws \Aimeos\MW\Media\Exception If image couldn't be saved to the given file name |
79
|
|
|
*/ |
80
|
|
|
public function save( $filename, $mimetype ) |
81
|
|
|
{ |
82
|
|
|
switch( $mimetype ) |
83
|
|
|
{ |
84
|
|
|
case 'image/gif': |
85
|
|
|
|
86
|
|
|
if( @imagegif( $this->image, $filename ) === false ) { |
87
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to save image to file "%1$s"', $filename ) ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
break; |
91
|
|
|
|
92
|
|
|
case 'image/jpeg': |
93
|
|
|
|
94
|
|
|
$quality = 75; |
95
|
|
|
if( isset( $this->options['image']['jpeg']['quality'] ) ) { |
96
|
|
|
$quality = (int) $this->options['image']['jpeg']['quality']; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if( @imagejpeg( $this->image, $filename, $quality ) === false ) { |
100
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to save image to file "%1$s"', $filename ) ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
break; |
104
|
|
|
|
105
|
|
|
case 'image/png': |
106
|
|
|
|
107
|
|
|
$quality = 9; |
108
|
|
|
if( isset( $this->options['image']['png']['quality'] ) ) { |
109
|
|
|
$quality = (int) $this->options['image']['png']['quality']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if( imagesavealpha( $this->image, true ) === false ) { |
113
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagesavealpha)') ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if( @imagepng( $this->image, $filename, $quality ) === false ) { |
117
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'Unable to save image to file "%1$s"', $filename ) ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
default: |
123
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'File format "%1$s" is not supported', $this->getMimeType() ) ); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Scales the image to the given width and height. |
130
|
|
|
* |
131
|
|
|
* @param integer $width New width of the image |
132
|
|
|
* @param integer $height New height of the image |
133
|
|
|
* @param boolean $fit True to keep the width/height ratio of the image |
134
|
|
|
*/ |
135
|
|
|
public function scale( $width, $height, $fit = true ) |
136
|
|
|
{ |
137
|
|
|
if( ( $info = getimagesize( $this->getFilepath() ) ) === false ) { |
138
|
|
|
throw new \Aimeos\MW\Media\Exception( 'Unable to retrieve image size for: ' . $this->getFilepath() ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if( $fit === true ) |
142
|
|
|
{ |
143
|
|
|
list( $width, $height ) = $this->getSizeFitted( $info[0], $info[1], $width, $height ); |
144
|
|
|
|
145
|
|
|
if( $info[0] <= $width && $info[1] <= $height ) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if( !isset( $this->origimage ) ) { |
151
|
|
|
$this->origimage = $this->image; |
152
|
|
|
} else { |
153
|
|
|
imagedestroy( $this->image ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if( ( $this->image = imagecreatetruecolor( $width, $height ) ) === false ) { |
157
|
|
|
throw new \Aimeos\MW\Media\Exception( 'Unable to create new image' ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if( imagealphablending( $this->image, false ) === false ) { |
161
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagealphablending)') ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if( ( $transparent = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ) ) === false ) { |
165
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagecolorallocatealpha)') ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if( imagefilledrectangle( $this->image, 0, 0, $width, $height, $transparent ) === false ) { |
169
|
|
|
throw new \Aimeos\MW\Media\Exception( sprintf( 'GD library failed (imagefilledrectangle)') ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if( imagecopyresampled( $this->image, $this->origimage, 0, 0, 0, 0, $width, $height, $info[0], $info[1] ) === false ) { |
173
|
|
|
throw new \Aimeos\MW\Media\Exception( 'Unable to resize image' ); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|