1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSSPrites\ImageProcessor; |
4
|
|
|
|
5
|
|
|
use Imagine\Gd\Imagine as ImagineGd; |
6
|
|
|
use Imagine\Gmagick\Imagine as ImagineGmagick; |
7
|
|
|
use Imagine\Image\Box; |
8
|
|
|
use Imagine\Image\Palette\RGB; |
9
|
|
|
use Imagine\Image\Point; |
10
|
|
|
use Imagine\Imagick\Imagine as ImagineImagick; |
11
|
|
|
|
12
|
|
|
class ImagineImageProcessor extends AbstractImageProcessor implements ImageProcessorInterface |
13
|
|
|
{ |
14
|
21 |
|
public function setConfig($config) |
15
|
|
|
{ |
16
|
21 |
|
if ($config['driver'] == 'gd') { |
17
|
18 |
|
$this->manager = new ImagineGd(); |
18
|
21 |
|
} elseif ($config['driver'] == 'imagick') { |
19
|
|
|
$this->manager = new ImagineImagick(); |
20
|
3 |
|
} elseif ($config['driver'] == 'gmagick') { |
21
|
|
|
$this->manager = new ImagineGmagick(); |
22
|
|
|
} else { |
23
|
3 |
|
throw new \Exception('Unknown Imagine Driver "'.$config['driver'].'"'); |
24
|
|
|
} |
25
|
18 |
|
} |
26
|
|
|
|
27
|
24 |
|
public function create($width, $height, $background = null) |
28
|
|
|
{ |
29
|
24 |
|
$palette = new RGB(); |
30
|
24 |
|
$color = is_null($background) ? $palette->color('#fff', 0) : $palette->color($background, 100); |
31
|
|
|
|
32
|
24 |
|
$size = new Box($width, $height); |
33
|
|
|
|
34
|
24 |
|
$newImage = new self(); |
35
|
24 |
|
$newImage->setImage($this->manager->create($size, $color)); |
36
|
|
|
|
37
|
24 |
|
return $newImage; |
38
|
|
|
} |
39
|
|
|
|
40
|
48 |
|
public function load($path) |
41
|
|
|
{ |
42
|
48 |
|
$newImage = new self(); |
43
|
48 |
|
$newImage->setImage($this->manager->open($path)); |
44
|
|
|
|
45
|
48 |
|
return $newImage; |
46
|
|
|
} |
47
|
|
|
|
48
|
18 |
|
public function save($path) |
49
|
|
|
{ |
50
|
18 |
|
return $this->image->save($path); |
51
|
|
|
} |
52
|
|
|
|
53
|
21 |
|
public function insert(ImageProcessorInterface $image, $x = 0, $y = 0) |
54
|
|
|
{ |
55
|
21 |
|
return $this->image->paste($image->getImage(), new Point($x, $y)); |
56
|
|
|
} |
57
|
|
|
|
58
|
27 |
View Code Duplication |
public function getWidth() |
|
|
|
|
59
|
|
|
{ |
60
|
27 |
|
if (is_null($this->image)) { |
61
|
3 |
|
throw new \Exception('No image created / loaded'); |
62
|
|
|
} |
63
|
|
|
|
64
|
24 |
|
$size = $this->image->getSize(); |
65
|
|
|
|
66
|
24 |
|
return $size->getWidth(); |
67
|
|
|
} |
68
|
|
|
|
69
|
27 |
View Code Duplication |
public function getHeight() |
|
|
|
|
70
|
|
|
{ |
71
|
27 |
|
if (is_null($this->image)) { |
72
|
3 |
|
throw new \Exception('No image created / loaded'); |
73
|
|
|
} |
74
|
|
|
|
75
|
24 |
|
$size = $this->image->getSize(); |
76
|
|
|
|
77
|
24 |
|
return $size->getHeight(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.