forked from kingkool68/dummyimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleCrop.php
83 lines (70 loc) · 2.16 KB
/
CircleCrop.php
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
<?php
/**
* Created by PhpStorm.
* User: kushagra
* Date: 05/10/17
* Time: 11:17 PM
*/
namespace App;
class CircleCrop
{
private $src_img;
private $src_w;
private $src_h;
private $dst_img;
private $dst_w;
private $dst_h;
public function __construct($img, $dstWidth, $dstHeight)
{
$this->src_img = $img;
$this->src_w = imagesx($img);
$this->src_h = imagesy($img);
$this->dst_w = $dstWidth;
$this->dst_h = $dstHeight;
}
public function __destruct()
{
if (is_resource($this->dst_img))
{
imagedestroy($this->dst_img);
}
}
public function display()
{
header("Content-type: image/png");
imagepng($this->dst_img);
return $this;
}
public function reset()
{
if (is_resource(($this->dst_img)))
{
imagedestroy($this->dst_img);
}
$this->dst_img = imagecreatetruecolor($this->dst_w, $this->dst_h);
imagecopy($this->dst_img, $this->src_img, 0, 0, 0, 0, $this->dst_w, $this->dst_h);
return $this;
}
public function size($dstWidth, $dstHeight)
{
$this->dst_w = $dstWidth;
$this->dst_h = $dstHeight;
return $this->reset();
}
public function crop()
{
$this->reset();
$mask = imagecreatetruecolor($this->dst_w, $this->dst_h);
$maskTransparent = imagecolorallocate($mask, 255, 0, 255);
imagecolortransparent($mask, $maskTransparent);
imagefilledellipse($mask, $this->dst_w / 2, $this->dst_h / 2, $this->dst_w, $this->dst_h, $maskTransparent);
imagecopymerge($this->dst_img, $mask, 0, 0, 0, 0, $this->dst_w, $this->dst_h, 100);
$dstTransparent = imagecolorallocate($this->dst_img, 255, 0, 255);
imagefill($this->dst_img, 0, 0, $dstTransparent);
imagefill($this->dst_img, $this->dst_w - 1, 0, $dstTransparent);
imagefill($this->dst_img, 0, $this->dst_h - 1, $dstTransparent);
imagefill($this->dst_img, $this->dst_w - 1, $this->dst_h - 1, $dstTransparent);
imagecolortransparent($this->dst_img, $dstTransparent);
return $this;
}
}