RotateTrait.php
1.9 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
<?php
/**
* Author: lang
* Email: 732853989@qq.com
* Date: 2023/3/29
* Time: 14:52
*/
namespace Kkokk\Poster\Captcha\Traits;
trait RotateTrait
{
public function draw()
{
$im_width = $this->configs['im_width'];
$im_height = $this->configs['im_height'];
$this->im = $this->PosterDriver->createIm($im_width, $im_height, [], true);
$this->drawImage($this->configs['src'], true); // 背景图
// 旋转角度
$angle = mt_rand(45, 315);
$this->im = imagerotate($this->im, $angle, 0);
$this->drawRotate();
return [
'angle' => $angle
];
}
protected function drawRotate()
{
$Width = imagesx($this->im);
$rotateBg = $this->im; // 旋转后的背景
$bgWidth = $this->configs['im_width'];
$bgHeight = $this->configs['im_height'];
$circle = $this->PosterDriver->createIm($bgWidth, $bgHeight, [255, 255, 255, 127], $alpha = false);
$this->im = $this->PosterDriver->createIm($bgWidth, $bgHeight, [255, 255, 255, 127], $alpha = true); // 最后输出的jpg
$surplusR = ($Width - $bgWidth) / 2;
$r = ($bgWidth / 2) - 2; //圆半径
for ($x = 0; $x < $bgWidth; $x++) {
for ($y = 0; $y < $bgHeight; $y++) {
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
$rgbColor = imagecolorat($rotateBg, $x + 2 + $surplusR, $y + 2 + $surplusR);
imagesetpixel($circle, $x + 2, $y + 2, $rgbColor);
}
}
}
imagecopyresampled($this->im, $circle, 0, 0, 2, 2, $bgWidth, $bgHeight, $bgWidth - 3, $bgHeight - 3);
$this->destroyImage($rotateBg);
$this->destroyImage($circle);
}
protected function getImBg()
{
return __DIR__ . '/../../style/rotate_bg/rotate0' . mt_rand(1, 5) . '.jpg';
}
}