|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BWC\Share\Image; |
|
4
|
|
|
|
|
5
|
|
|
class ImageCropper extends ImageManipulator |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var resource |
|
9
|
|
|
*/ |
|
10
|
|
|
protected $image; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var resource |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $cropped; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Loads image from string |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $imageData |
|
21
|
|
|
*/ |
|
22
|
|
|
public function loadImageFromString($imageData) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->image = imagecreatefromstring($imageData); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Loads image from file |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $filepath |
|
31
|
|
|
*/ |
|
32
|
|
|
public function loadImageFromFile($filepath) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->image = imagecreatefromstring(file_get_contents($filepath)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Crops image |
|
39
|
|
|
* |
|
40
|
|
|
* @param int $sourceX Source image top left X coordinate |
|
41
|
|
|
* @param int $sourceY Source image top left Y coordinate |
|
42
|
|
|
* @param int $sourceWidth Area width on source image |
|
43
|
|
|
* @param int $sourceHeight Area height on source image |
|
44
|
|
|
* @param int $targetWidth Area width on destination image |
|
45
|
|
|
* @param int $targetHeight Area height on destination image |
|
46
|
|
|
* |
|
47
|
|
|
* @throws \LogicException |
|
48
|
|
|
*/ |
|
49
|
|
|
public function crop($sourceX, $sourceY, $sourceWidth, $sourceHeight, $targetWidth, $targetHeight) |
|
50
|
|
|
{ |
|
51
|
|
|
if (null === $this->image) { |
|
52
|
|
|
throw new \LogicException( |
|
53
|
|
|
'You have to load an image before cropping it. |
|
54
|
|
|
Use loadImageFromString or loadImageFromFile' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$destination = imagecreatetruecolor($targetWidth, $targetHeight); |
|
59
|
|
|
imagealphablending($destination, false); |
|
60
|
|
|
imagesavealpha($destination, true); |
|
61
|
|
|
$transparent = imagecolorallocatealpha($destination, 255, 255, 255, 127); |
|
62
|
|
|
imagefill($destination, 0, 0, $transparent); |
|
63
|
|
|
|
|
64
|
|
|
imagecopyresampled( |
|
65
|
|
|
$destination, |
|
66
|
|
|
$this->image, |
|
67
|
|
|
0, |
|
68
|
|
|
0, |
|
69
|
|
|
$sourceX, |
|
70
|
|
|
$sourceY, |
|
71
|
|
|
$targetWidth, |
|
72
|
|
|
$targetHeight, |
|
73
|
|
|
$sourceWidth, |
|
74
|
|
|
$sourceHeight |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$this->cropped = $destination; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns image as string |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $format |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \LogicException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getCroppedImageAsString($format = self::FORMAT_PNG) |
|
90
|
|
|
{ |
|
91
|
|
|
if (null === $this->cropped) { |
|
92
|
|
|
throw new \LogicException('You have to crop image first before getting it'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->formatImageData($this->cropped, $format); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Saves image to a file |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $filepath |
|
102
|
|
|
* @param string $format |
|
103
|
|
|
*/ |
|
104
|
|
|
public function saveCroppedImageToFile($filepath, $format = self::FORMAT_PNG) |
|
105
|
|
|
{ |
|
106
|
|
|
file_put_contents($filepath, $this->getCroppedImageAsString($format)); |
|
107
|
|
|
} |
|
108
|
|
|
} |