-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathImageMerge.php
More file actions
206 lines (178 loc) · 4.89 KB
/
ImageMerge.php
File metadata and controls
206 lines (178 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace SimpleSoftwareIO\QrCode;
use InvalidArgumentException;
class ImageMerge
{
/**
* Holds the QrCode image.
*
* @var Image
*/
protected $sourceImage;
/**
* Holds the merging image.
*
* @var Image
*/
protected $mergeImage;
/**
* The height of the source image.
*
* @var int
*/
protected $sourceImageHeight;
/**
* The width of the source image.
*
* @var int
*/
protected $sourceImageWidth;
/**
* The height of the merge image.
*
* @var int
*/
protected $mergeImageHeight;
/**
* The width of the merge image.
*
* @var int
*/
protected $mergeImageWidth;
/**
* Holds the radio of the merging image.
*
* @var float
*/
protected $mergeRatio;
/**
* The height of the merge image after it is merged.
*
* @var int
*/
protected $postMergeImageHeight;
/**
* The width of the merge image after it is merged.
*
* @var int
*/
protected $postMergeImageWidth;
/**
* The position that the merge image is placed on top of the source image.
*
* @var int
*/
protected $centerY;
/**
* The position that the merge image is placed on top of the source image.
*
* @var int
*/
protected $centerX;
/**
* Creates a new ImageMerge object.
*
* @param $sourceImage Image The image that will be merged over.
* @param $mergeImage Image The image that will be used to merge with $sourceImage
*/
public function __construct(Image $sourceImage, Image $mergeImage)
{
$this->sourceImage = $sourceImage;
$this->mergeImage = $mergeImage;
}
/**
* Returns an QrCode that has been merge with another image.
* This is usually used with logos to imprint a logo into a QrCode.
*
* @param $percentage float The percentage of size relative to the entire QR of the merged image
*
* @return string
*/
public function merge($percentage)
{
$this->setProperties($percentage);
$img = imagecreatetruecolor($this->sourceImage->getWidth(), $this->sourceImage->getHeight());
imagealphablending($img, true);
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $transparent);
imagealphablending( $img, false );
imagesavealpha( $img, true );
imagecopy(
$img,
$this->sourceImage->getImageResource(),
0,
0,
0,
0,
$this->sourceImage->getWidth(),
$this->sourceImage->getHeight()
);
imagecopyresampled(
$img,
$this->mergeImage->getImageResource(),
$this->centerX,
$this->centerY,
0,
0,
$this->postMergeImageWidth,
$this->postMergeImageHeight,
$this->mergeImageWidth,
$this->mergeImageHeight
);
$this->sourceImage->setImageResource($img);
return $this->createImage();
}
/**
* Creates a PNG Image.
*
* @return string
*/
protected function createImage()
{
ob_start();
imagepng($this->sourceImage->getImageResource());
return ob_get_clean();
}
/**
* Sets the objects properties.
*
* @param $percentage float The percentage that the merge image should take up.
*
* @return void
*/
protected function setProperties($percentage)
{
if ($percentage > 1) {
throw new InvalidArgumentException('$percentage must be less than 1');
}
$this->sourceImageHeight = $this->sourceImage->getHeight();
$this->sourceImageWidth = $this->sourceImage->getWidth();
$this->mergeImageHeight = $this->mergeImage->getHeight();
$this->mergeImageWidth = $this->mergeImage->getWidth();
$this->calculateOverlap($percentage);
$this->calculateCenter();
}
/**
* Calculates the center of the source Image using the Merge image.
*
* @return void
*/
protected function calculateCenter()
{
$this->centerX = intval(($this->sourceImageWidth / 2) - ($this->postMergeImageWidth / 2));
$this->centerY = intval(($this->sourceImageHeight / 2) - ($this->postMergeImageHeight / 2));
}
/**
* Calculates the width of the merge image being placed on the source image.
*
* @param float $percentage
*
* @return void
*/
protected function calculateOverlap($percentage)
{
$this->mergeRatio = round($this->mergeImageWidth / $this->mergeImageHeight, 2);
$this->postMergeImageWidth = intval($this->sourceImageWidth * $percentage);
$this->postMergeImageHeight = intval($this->postMergeImageWidth / $this->mergeRatio);
}
}