GdTrait.php
20.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
<?php
/**
* Author: lang
* Email: 732853989@qq.com
* Date: 2023/3/27
* Time: 11:10
*/
namespace Kkokk\Poster\Image\Traits;
use Kkokk\Poster\Exception\PosterException;
trait GdTrait
{
/**
* 返回图片流或者图片
* @Author lang
* @Date 2020-08-14T14:29:57+0800
* @return void|array
*/
protected function returnImage($type, $outfile = true)
{
if (!isset($this->im) || empty($this->im)) throw new PosterException('没有创建任何资源');
if ($outfile) {
$this->dirExists($this->pathname);
if (strripos($this->filename, '.') === false) {
$this->filename = $this->filename . '.' . $this->type;
}
$this->poster_type[$type]($this->im, $this->path . $this->pathname . '/' . $this->filename);
return ['url' => $this->pathname . '/' . $this->filename];
}
header('Content-Type:Image/' . $this->type);
$this->poster_type[$type]($this->im);
}
protected function setImage($source)
{
if (strpos($source, 'http') === 0) {
throw new PosterException("unable to set the remote source {$source}");
}
if (!empty($source)) {
return $this->poster_type[$this->type]($this->im, $source);
}
throw new PosterException("source not found {$source}");
}
/**
* 创建画布
*/
public function createIm($w, $h, $rgba, $alpha = false)
{
$cut = imagecreatetruecolor($w, $h);
$color = $alpha ? $this->createColorAlpha($cut, $rgba) : $this->createColor($cut, $rgba);
if ($alpha) {
// imagecolortransparent($cut, $color);
imagesavealpha($cut, true);
}
imagefill($cut, 0, 0, $color);
return $cut;
}
/**
* 获取颜色值,可设置透明度
*/
public function createColorAlpha($cut, $rgba = [255, 255, 255, 127])
{
if (empty($rgba)) $rgba = [255, 255, 255, 127];
if (count($rgba) != 4) throw new PosterException('The length of the rgba parameter is 4');
foreach ($rgba as $k => $value) {
if (!is_int($rgba[$k])) {
throw new PosterException('The value must be an integer');
} elseif ($k < 3 && ($rgba[$k] > 255 || $rgba[$k] < 0)) {
throw new PosterException('The color value is between 0-255');
} elseif ($k == 3 && ($rgba[$k] > 127 || $rgba[$k] < 0)) {
throw new PosterException('The alpha value is between 0-127');
}
}
return imagecolorallocatealpha($cut, $rgba[0], $rgba[1], $rgba[2], $rgba[3]);
}
/**
* 获取颜色值,没有透明度
*/
public function createColor($cut, $rgba = [255, 255, 255, 1])
{
if (empty($rgba)) $rgba = [255, 255, 255, 1];
if (count($rgba) < 4) throw new PosterException('The length of the rgba parameter is 4');
foreach ($rgba as $k => $value) {
if (!is_int($rgba[$k])) {
throw new PosterException('The text value must be an integer');
} elseif ($k < 3 && ($rgba[$k] > 255 || $rgba[$k] < 0)) {
throw new PosterException('The text color value is between 0-255');
}
}
return imagecolorallocate($cut, $rgba[0], $rgba[1], $rgba[2]);
}
/**
* 计算渐变颜色值
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/10/18
* Time: 16:08
* @param $h
* @param $i
* @param $c1
* @param $c2
* @return array
*/
protected function calcColor($h, $i, $c1, $c2)
{
$res = [];
$r = abs($c2[0] - $c1[0]);
$rr = $c2[0] - $c1[0];
$b = abs($c2[1] - $c1[1]);
$bb = $c2[1] - $c1[1];
$g = abs($c2[2] - $c1[2]);
$gg = $c2[2] - $c1[2];
if ($r == 0) {
$res[] = $c2[0];
} else {
$res[] = $rr > 0 ? ($c1[0] + $r / $h * $i) : ($c1[0] - $r / $h * $i);
}
if ($b == 0) {
$res[] = $c2[1];
} else {
$res[] = $bb > 0 ? ($c1[1] + $b / $h * $i) : ($c1[1] - $b / $h * $i);
}
if ($g == 0) {
$res[] = $c2[2];
} else {
$res[] = $gg > 0 ? ($c1[2] + $g / $h * $i) : ($c1[2] - $g / $h * $i);
}
return $res;
}
/**
* 计算颜色渐变区块->等比分配
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/10/18
* Time: 10:56
* @param $rgba
* @param $h
* @param $i
* @return array
*/
protected function calcColorArea($rgbaColor, $rgbaCount, $h, $i)
{
// 单色
if ($rgbaCount == 1) {
$colorRgb = $this->calcColor($h, $i, $rgbaColor[0], $rgbaColor[0]);
} elseif ($rgbaCount == 2) {
// 两种颜色
$colorRgb = $this->calcColor($h, $i, $rgbaColor[0], $rgbaColor[1]);
} else {
// 多种颜色 计算距离并分配颜色
$rgbaCount = $rgbaCount - 1;
$d = ceil($h / $rgbaCount);
$index1 = 0;
$index2 = 1;
$di = 0;
for ($j = $rgbaCount + 1; $j > 0; $j--) {
if ($i >= ceil((($j - 1) * $d)) && $i <= ceil($j * $d)) {
$index1 = $j - 1;
$index2 = $j;
$di = $i - (($j - 1) * $d);
break;
}
}
$colorRgb = $this->calcColor($d, $di, $rgbaColor[$index1], $rgbaColor[$index2]);
}
return $colorRgb;
}
/**
* 计算颜色渐变方向
* @Author lang
* @Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 上午12:17
* @param $im
* @param $rgbaColor
* @param $rgbaCount
* @param $to
* @param $w
* @param $h
* @return mixed
*/
public function calcColorDirection($im, $rgbaColor, $rgbaCount, $to, $w, $h)
{
$to = preg_replace('~\s+~', ' ', trim($to, ' '));
switch ($to) {
case '':
case 'bottom':
$toi = $h;
$toj = $w;
$im = $this->linearGradient($im, $toi, $toj, $rgbaColor, $rgbaCount);
break;
case 'top':
$toi = $h;
$toj = $w;
$rgbaColor = array_reverse($rgbaColor);
$im = $this->linearGradient($im, $toi, $toj, $rgbaColor, $rgbaCount);
break;
case 'left':
$toi = $w;
$toj = $h;
$rgbaColor = array_reverse($rgbaColor);
$im = $this->linearGradient($im, $toi, $toj, $rgbaColor, $rgbaCount, 'j', 'i');
break;
case 'right':
$toi = $w;
$toj = $h;
$im = $this->linearGradient($im, $toi, $toj, $rgbaColor, $rgbaCount, 'j', 'i');
break;
case 'right bottom':
case 'bottom right':
$toi = $w;
$toj = $h;
$rgbaColor = array_reverse($rgbaColor);
$im = $this->linearGradientLeftTopRightBottomDiagonal($im, $toi, $toj, $rgbaColor, $rgbaCount);
break;
case 'right top':
case 'top right':
$toi = $w;
$toj = $h;
$rgbaColor = array_reverse($rgbaColor);
$im = $this->linearGradientLeftTopRightBottomDiagonal($im, $toi, $toj, $rgbaColor, $rgbaCount, 0, $toj);
break;
case 'left bottom':
case 'bottom left':
$toi = $w;
$toj = $h;
$im = $this->linearGradientLeftTopRightBottomDiagonal($im, $toi, $toj, $rgbaColor, $rgbaCount, 0, $toj);
break;
case 'left top':
case 'top left':
$toi = $w;
$toj = $h;
$im = $this->linearGradientLeftTopRightBottomDiagonal($im, $toi, $toj, $rgbaColor, $rgbaCount);
break;
default:
// code...
break;
}
return $im;
}
/**
* 获取渐变颜色值
* @Author lang
* @Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 上午12:15
* @param $im
* @param $rgbaColor
* @param $rgbaCount
* @param $length
* @param $i
* @return false|int
*/
public function getColor($im, $rgbaColor, $rgbaCount, $length, $i)
{
$colorRgb = $this->calcColorArea($rgbaColor, $rgbaCount, $length, $i);
$color = imagecolorallocate($im, $colorRgb[0], $colorRgb[1], $colorRgb[2]);
return $color;
}
/**
* 获取透明颜色值
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 9:37
* @param $im
* @param $colorRgb
* @param $alphas
* @return false|int
*/
public function getAlphasColor($im, $colorRgb, $alphas)
{
$color = imagecolorallocatealpha($im, $colorRgb[0], $colorRgb[1], $colorRgb[2], $alphas);
return $color;
}
/**
* 渐变处理方法
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 10:04
* @param $im resource 画布资源
* @param $toi double 宽或高
* @param $toj double 高或宽
* @param $rgbaColor array 渐变色值
* @param $rgbaCount int 渐变色数量
* @param int $radius int 圆角
* @param string $ii string x,y 变量取值替换
* @param string $jj string x,y 变量取值替换
* @return mixed|resource
*/
protected function linearGradient($im, $toi, $toj, $rgbaColor, $rgbaCount, $ii = 'i', $jj = 'j')
{
for ($i = $toi; $i >= 0; $i--) {
// 获取颜色
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $toi, $i);
// imagefilledrectangle($this->im, 0, $i, $w, 0, $color); // 填充颜色
// $color = ($colorRgb[0] << 16) + ($colorRgb[1] << 8) + $colorRgb[2]; // 获取颜色参数
for ($j = 0; $j < $toj; $j++) {
imagesetpixel($im, $$jj, $$ii, $color);
}
}
return $im;
}
/**
* 画圆角
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 9:55
* @param $im resource 画布资源
* @param $w double 宽
* @param $h double 高
* @param $radius int 圆角
* @return mixed|resource
*/
protected function setPixelRadius($im, $w, $h, $radius)
{
$newIm = $this->createIm($w, $h, [], true);;
$len = $w > $h ? $h / 2 : $w / 2;
list($leftTopRadius, $rightTopRadius, $leftBottomRadius, $rightBottomRadius) = $this->getRadiusType($radius, $len);
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$color = imagecolorat($im, $x, $y);
if (($x >= $leftTopRadius || $y >= $leftTopRadius)
&& (($x <= ($w - $rightTopRadius) || $y >= $rightTopRadius))
&& (($x >= $leftBottomRadius || $y <= ($h - $leftBottomRadius)))
&& (($x <= ($w - $rightBottomRadius)) || $y <= ($h - $rightBottomRadius))) {
//不在四角的范围内,直接画
imagesetpixel($newIm, $x, $y, $color);
} else {
// 上左
$y_x = $leftTopRadius;
$y_y = $leftTopRadius;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($leftTopRadius * $leftTopRadius))) {
imagesetpixel($newIm, $x, $y, $color);
}
// 上右
$y_x = $w - $rightTopRadius;
$y_y = $rightTopRadius;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($rightTopRadius * $rightTopRadius))) {
imagesetpixel($newIm, $x, $y, $color);
}
//下左
$y_x = $leftBottomRadius;
$y_y = $h - $leftBottomRadius;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($leftBottomRadius * $leftBottomRadius))) {
imagesetpixel($newIm, $x, $y, $color);
}
//下右
$y_x = $w - $rightBottomRadius;
$y_y = $h - $rightBottomRadius;
if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($rightBottomRadius * $rightBottomRadius))) {
imagesetpixel($newIm, $x, $y, $color);
}
}
}
}
return $newIm;
}
/**
* 渐变处理方法 对角 分两段循环
* @Author lang
* @Email: 732853989@qq.com
* Date: 2022/10/20
* Time: 上午12:13
* @param $im
* @param $toi
* @param $toj
* @param $rgbaColor
* @param $rgbaCount
* @param int $x
* @param int $y
* @return mixed
*/
protected function linearGradientLeftTopRightBottom($im, $toi, $toj, $rgbaColor, $rgbaCount, $x = 0, $y = 0)
{
$toLen = $toi >= $toj ? $toi : $toj;
$len = $toi + $toj;
$ii = $len - 1;
if ($x == 0 && $y == 0) {
// 从 0,0 开始
for ($i = 0; $i < $toLen + 1; $i++) {
//设$i为y轴坐标
$f = 0;
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $len, $ii--);
for ($j = 0; $j <= $i; $j++) {
if ($j <= $toi && ($i - $j) <= $toj) {
if (!$f) {
$x = $j;
$y = $i - $j;
$f = 1;
}
imagesetpixel($im, $j, $i - $j, $color);
}
}
}
//加入右半段
for ($i = $x + 1; $i <= $toi; $i++) {
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $len, $ii--);
for ($j = 0; $j <= $y; $j++) {
if (($i + $j) <= $toi && ($y - $j) <= $toj) {
imagesetpixel($im, $i + $j, $y - $j, $color);
}
}
}
} else {
// 从 0,y 开始
for ($i = 0; $i < $toLen + 1; $i++) {
//设$i为y轴坐标
$f = false;
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $len, $ii--);
for ($j = 0; $j <= $i; $j++) {
if ($j <= $toi && ($i - $j) <= $toj) {
if (!$f) {
$x = $j;
$y = $i - $j;
$f = true;
}
imagesetpixel($im, $j, $toj - ($i - $j), $color);
}
}
}
//加入后半段
for ($i = $x + 1; $i <= $toi; $i++) {
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $len, $ii--);
for ($j = 0; $j <= $y; $j++) {
if (($i + $j) <= $toi && ($y - $j) <= $toj) {
imagesetpixel($im, $i + $j, $j, $color);
}
}
}
}
return $im;
}
/**
* 根据对角线长度循环
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/11/23
* Time: 11:35
* @param $im
* @param $toi
* @param $toj
* @param $rgbaColor
* @param $rgbaCount
* @param int $x
* @param int $y
* @return mixed
*/
protected function linearGradientLeftTopRightBottomDiagonal($im, $toi, $toj, $rgbaColor, $rgbaCount, $x = 0, $y = 0)
{
$total = $toi + $toj + 1; // 对角线最大循环次数
$isRectangle = $toi != $toj; // 判断是否是长方形
// 获取中间位置数值
$centerNum = !$isRectangle ? $this->centerNumSquare($total) : $this->centerNumRectangle($toi, $toj);
$ii = $total; // 颜色计算递减数值
$toiTag = 'ii'; // 默认宽大于长
$tojTag = 'jj'; // 默认宽大于长
if ($toj > $toi) { // 长大于宽
$toiTag = 'jj';
$tojTag = 'ii';
}
if ($isRectangle) { // 长方形
for ($i = 0; $i < $total; $i++) {
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $total, $ii--);
$im = $this->getPointRectangle($im, $i, $centerNum, $total, $color, $toiTag, $tojTag, $x, $y);
}
} else {
// 正方形
for ($i = 0; $i < $total; $i++) {
$color = $this->getColor($im, $rgbaColor, $rgbaCount, $total, $ii--);
$im = $this->getPointSquare($im, $i, $centerNum, $color, $x, $y);
}
}
return $im;
}
/**
* 正方形获取中间位置数值
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/11/23
* Time: 10:38
* @param $num
* @return float|int
*/
protected function centerNumSquare($num)
{
return $num / 2;
}
/**
* 长方形获取中间位置数值
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/11/23
* Time: 10:39
* @param $x
* @param $y
* @return int
*/
protected function centerNumRectangle($x, $y)
{
return $x > $y ? $y + 1 : $x + 1;
}
/**
* 长方形通过对角线循环绘画
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/12/12
* Time: 9:51
* @param $im
* @param $num
* @param $centerNum
* @param $total
* @param $color
* @param $toiTag
* @param $tojTag
* @param int $x
* @param int $y
* @return mixed
*/
protected function getPointRectangle($im, $num, $centerNum, $total, $color, $toiTag, $tojTag, $x = 0, $y = 0)
{
$len = $total - $centerNum * 2; // 求取对角线上相交线坐标到边的最大宽度数量
$min = $centerNum; // 从第几次循环开始保持最大宽度
$max = $min + $len; // 到第几结束
if ($num >= $min && $num <= $max) {
$ii = $num - $centerNum + 1;
for ($jj = $centerNum - 1; $jj >= 0; $jj--) {
imagesetpixel($im, ceil($$toiTag), abs($y - floor($$tojTag)), $color);
$ii++;
}
} elseif ($num > $max) {
$num = $num - $centerNum;
$ii = $num + 1;
$jj = $max - $len - 1;
for ($i = $max; $i > $num; $i--) {
imagesetpixel($im, ceil($$toiTag), abs($y - floor($$tojTag)), $color);
$ii++;
$jj--;
}
} else {
for ($i = 0; $i <= $num; $i++) {
imagesetpixel($im, $i, abs($y - ($num - $i)), $color);
}
}
return $im;
}
/**
* 正方形通过对角线循环绘画
* Author: lang
* Email: 732853989@qq.com
* Date: 2022/12/12
* Time: 9:52
* @param $im
* @param $num
* @param $centerNum
* @param $color
* @param int $x
* @param int $y
* @return mixed
*/
protected function getPointSquare($im, $num, $centerNum, $color, $x = 0, $y = 0)
{
if ($num > $centerNum) {
$num = $num - $centerNum;
$ii = $num;
for ($i = $centerNum; $i >= $num; $i--) {
// $arr[] = [ceil($ii) , floor($i)];
imagesetpixel($im, ceil($ii), abs($y - floor($i)), $color);
$ii++;
}
} else {
for ($i = 0; $i <= $num; $i++) {
// $arr[] = [$i , $num-$i];
imagesetpixel($im, $i, abs($y - ($num - $i)), $color);
}
}
return $im;
}
/**
* 字体加粗
*/
protected function fontWeight($weight, $fontSize, $angle, $dst_x, $dst_y, $color, $font, $contents)
{
for ($i = 0; $i < $weight; $i++) {
list($really_dst_x, $really_dst_y) = $this->calcWeight($i, $weight, $fontSize, $dst_x, $dst_y);
imagettftext($this->im, $fontSize, $angle, $really_dst_x, $really_dst_y, $color, $font, $contents);
}
}
/** 设置背景透明 */
public function setImageAlpha($pic, $w, $h, $alphas) {
$mask = $this->createIm($w, $h, [], $alphas > 1);
for ($x = 0; $x <= $w; $x++) {
for ($y = 0; $y <= $h; $y++) {
$rgbColor = imagecolorat($pic, $x, $y);
$thisColor = imagecolorsforindex($pic, $rgbColor);
$color = $this->createColorAlpha($pic, [$thisColor['red'], $thisColor['green'], $thisColor['blue'], $alphas]);
imagesetpixel($mask, $x, $y, $color);
}
}
return $mask;
}
}