1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
6
|
|
|
* @package MW |
7
|
|
|
* @subpackage Media |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MW\Media\Image; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default image class using ImageMagick. |
16
|
|
|
* |
17
|
|
|
* @package MW |
18
|
|
|
* @subpackage Media |
19
|
|
|
*/ |
20
|
|
|
class Imagick |
21
|
|
|
extends \Aimeos\MW\Media\Image\Base |
|
|
|
|
22
|
|
|
implements \Aimeos\MW\Media\Image\Iface |
23
|
|
|
{ |
24
|
|
|
private $image; |
25
|
|
|
private $options; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the new image object. |
30
|
|
|
* |
31
|
|
|
* @param string $content File content |
32
|
|
|
* @param string $mimetype Mime type of the media data |
33
|
|
|
* @param array $options Associative list of configuration options |
34
|
|
|
* @throws \Aimeos\MW\Media\Exception If image couldn't be retrieved from the given file name |
35
|
|
|
*/ |
36
|
|
|
public function __construct( $content, $mimetype, array $options ) |
37
|
|
|
{ |
38
|
|
|
parent::__construct( $mimetype ); |
39
|
|
|
|
40
|
|
|
$this->options = $options; |
41
|
|
|
|
42
|
|
|
try |
43
|
|
|
{ |
44
|
|
|
$this->image = new \Imagick( array() ); |
45
|
|
|
$this->image->readImageBlob( $content ); |
46
|
|
|
} |
47
|
|
|
catch( \Exception $e ) |
48
|
|
|
{ |
49
|
|
|
throw new \Aimeos\MW\Media\Exception( $e->getMessage() ); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Stores the media data at the given file name. |
56
|
|
|
* |
57
|
|
|
* @param string|null $filename File name to save the data into or null to return the data |
58
|
|
|
* @param string|null $mimetype Mime type to save the content as or null to leave the mime type unchanged |
59
|
|
|
* @return string|null File content if file name is null or null if data is saved to the given file name |
60
|
|
|
* @throws \Aimeos\MW\Media\Exception If image couldn't be saved to the given file name |
61
|
|
|
*/ |
62
|
|
|
public function save( $filename = null, $mimetype = null ) |
63
|
|
|
{ |
64
|
|
|
if( $mimetype === null ) { |
65
|
|
|
$mimetype = $this->getMimeType(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$quality = 25; |
|
|
|
|
69
|
|
|
$mime = explode( '/', $mimetype ); |
70
|
|
|
|
71
|
|
|
if( isset( $this->options['image'][ $mime[1] ]['quality'] ) ) { |
72
|
|
|
$quality = 100 - (int) $this->options['image'][ $mime[1] ]['quality']; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
try |
76
|
|
|
{ |
77
|
|
|
$this->image->setImageFormat( $mime[1] ); |
78
|
|
|
|
79
|
|
|
if( $filename === null ) { |
80
|
|
|
return $this->image->getImageBlob(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->image->writeImage( $filename ); |
84
|
|
|
} |
85
|
|
|
catch( \Exception $e ) |
86
|
|
|
{ |
87
|
|
|
throw new \Aimeos\MW\Media\Exception( $e->getMessage() ); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Scales the image to the given width and height. |
94
|
|
|
* |
95
|
|
|
* @param integer $width New width of the image |
96
|
|
|
* @param integer $height New height of the image |
97
|
|
|
* @param boolean $fit True to keep the width/height ratio of the image |
98
|
|
|
* @return \Aimeos\MW\Media\Iface Self object for method chaining |
99
|
|
|
*/ |
100
|
|
|
public function scale( $width, $height, $fit = true ) |
101
|
|
|
{ |
102
|
|
|
if( $fit === true ) |
103
|
|
|
{ |
104
|
|
|
$w = $this->image->getImageWidth(); |
105
|
|
|
$h = $this->image->getImageHeight(); |
106
|
|
|
|
107
|
|
|
list( $width, $height ) = $this->getSizeFitted( $w, $h, $width, $height ); |
108
|
|
|
|
109
|
|
|
if( $w <= $width && $h <= $height ) { |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
try { |
115
|
|
|
$this->image->resizeImage( $width, $height, \Imagick::FILTER_POINT, 1 ); |
116
|
|
|
} catch( \Exception $e ) { |
117
|
|
|
throw new \Aimeos\MW\Media\Exception( $e->getMessage() ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|