Completed
Push — master ( e595c8...dedff5 )
by Aimeos
08:25
created

Imagick   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
B save() 0 28 5
B scale() 0 22 5
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
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
$quality is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
		$mime = explode( '/', $mimetype );
70
71
		if( isset( $this->options['image'][ $mime[1] ]['quality'] ) ) {
72
			$quality = 100 - (int) $this->options['image'][ $mime[1] ]['quality'];
0 ignored issues
show
Unused Code introduced by
$quality is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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