1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Image\Imagick\Effect; |
4
|
|
|
|
5
|
|
|
use \Imagick; |
6
|
|
|
|
7
|
|
|
use \Charcoal\Image\Effect\AbstractCropEffect; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Resize Effect for the Imagick driver |
11
|
|
|
*/ |
12
|
|
|
class ImagickCropEffect extends AbstractCropEffect |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param integer $width The crop width. |
16
|
|
|
* @param integer $height The crop height. |
17
|
|
|
* @param integer $x The x-position (in pixel) of the crop. |
18
|
|
|
* @param integer $y The y-position (in pixel) of the crop. |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
protected function doCrop($width, $height, $x, $y) |
22
|
|
|
{ |
23
|
|
|
$gravity = $this->image()->imagickGravity($this->gravity()); |
|
|
|
|
24
|
|
|
|
25
|
|
|
// This sets the gravity for the rest of the chain |
26
|
|
|
$this->image()->imagick()->setGravity($gravity); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// Apply gravity to crop coordinates |
29
|
|
|
|
30
|
|
|
$imageWidth = $this->image()->width(); |
31
|
|
|
$imageHeight = $this->image()->height(); |
32
|
|
|
|
33
|
|
|
switch ($this->image()->imagick()->getGravity()) { |
|
|
|
|
34
|
|
|
case Imagick::GRAVITY_NORTHWEST: |
35
|
|
|
break; |
36
|
|
|
case Imagick::GRAVITY_NORTH: |
37
|
|
|
$x = ($imageWidth / 2 - $width / 2); |
38
|
|
|
break; |
39
|
|
|
case Imagick::GRAVITY_NORTHEAST: |
40
|
|
|
$x = ($imageWidth - $width); |
41
|
|
|
break; |
42
|
|
|
case Imagick::GRAVITY_WEST: |
43
|
|
|
$y = ($imageHeight / 2 - $height / 2); |
44
|
|
|
break; |
45
|
|
View Code Duplication |
case Imagick::GRAVITY_CENTER: |
|
|
|
|
46
|
|
|
$x = ($imageWidth / 2 - $width / 2); |
47
|
|
|
$y = ($imageHeight / 2 - $height / 2); |
48
|
|
|
break; |
49
|
|
View Code Duplication |
case Imagick::GRAVITY_EAST: |
|
|
|
|
50
|
|
|
$x = ($imageWidth - $width); |
51
|
|
|
$y = ($imageHeight / 2 - $height / 2); |
52
|
|
|
break; |
53
|
|
|
case Imagick::GRAVITY_SOUTHWEST: |
54
|
|
|
$y = ($imageHeight - $height); |
55
|
|
|
break; |
56
|
|
View Code Duplication |
case Imagick::GRAVITY_SOUTH: |
|
|
|
|
57
|
|
|
$x = ($imageWidth / 2 - $width / 2); |
58
|
|
|
$y = ($imageHeight - $height); |
59
|
|
|
break; |
60
|
|
|
case Imagick::GRAVITY_SOUTHEAST: |
61
|
|
|
$x = ($imageWidth - $width); |
62
|
|
|
$y = ($imageHeight - $height); |
63
|
|
|
break; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->image()->imagick()->cropImage($width, $height, $x, $y); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: