-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCutImage.php
More file actions
137 lines (128 loc) · 3.15 KB
/
CutImage.php
File metadata and controls
137 lines (128 loc) · 3.15 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
<?php
/**
* Created by PhpStorm.
* User: APG
* Date: 19.05.2020
* Time: 21:42
*/
/**
* Class CutImage
*/
class CutImage
{
/**
* @var string - source file
*/
private $file;
/**
* @var string file type
*/
private $type;
/**
* @var -file resource
*/
private $image;
/**
* @var array cutting template
* [
* [
* x1, // x
* y1, // y
* w, // width
* h, // height
* ],
* [
* x2,
* y2,
* w, // width
* h, // height
* ],
* ]
*/
private $params = [];
/**
* @param string $filename
* @param array $params
*/
public function __construct(string $filename, array $params)
{
$this->file = $filename;
$this->params = $params;
$this->getMimeType();
$this->getImageCreate();
}
/**
* @return bool
*/
public function getMimeType()
{
switch(mime_content_type($this->file))
{
case 'image/jpeg':
$this->type = "jpg";
break;
case 'image/png':
$this->type = "png";
break;
case 'image/gif':
$this->type = "gif";
break;
default:
return false;
}
}
/**
*
*/
public function getImageCreate()
{
switch($this->type) {
case 'jpg':
$this->image = @imagecreatefromjpeg($this->file);
break;
case 'png':
$this->image = @imagecreatefrompng($this->file);
break;
case 'gif':
$this->image = @imagecreatefromgif($this->file);
break;
default:
exit('Unknown file type');
}
}
public function save(string $name)
{
$arrSize = getimagesize($this->file);
# new size
$width = $arrSize[0] + 70;
$height = $arrSize[1];
# new file resource
$nfile = imagecreate($width, $height);
# background
$white = imagecolorallocatealpha($nfile, 255, 255, 255, 110);
imagefilledrectangle($nfile, 0, 0, $width, $height, $white);
# copy & resize
foreach($this->params as $param) {
imagecopyresampled($nfile, $this->image, $param[0], $param[1], $param[0]-10, $param[1], $param[2], $param[3], $param[2], $param[3]);
}
# save file
$this->imageByType($nfile, $name . '.' . $this->type);
return "<img src='" . $name . '.' . $this->type . "' alt='img'>";
}
public function imageByType($newFileResource, $fileName)
{
switch($this->type) {
case 'jpg':
imagejpeg($newFileResource, $fileName, 100);
break;
case 'png':
imagepng($newFileResource, $fileName, 0);
break;
case 'gif':
imagegif($newFileResource, $fileName);
break;
default:
exit('Unknown file type');
}
}
}