-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanCodeImage.php
executable file
·228 lines (184 loc) · 5.81 KB
/
CleanCodeImage.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
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
<?php
require_once 'CleanCodeFile.php';
class CleanCodeImage extends CleanCodeFile
{
private $width = 0;
private $height = 0;
private $maxWidth = 1600;
private $maxHeigh = 1600;
private $dst_x = 0;
private $dst_y = 0;
private $src_x = 0;
private $src_y = 0;
private $thumbs = array();
private $cropParameters = array();
function __construct($extensions = 'jpg|jpeg|gif|png')
{
parent::__construct($extensions);
}
public function setMaxWidth($pixels)
{
if($pixels) $this->maxWidth = $pixels;
}
public function setMaxHeight($pixels)
{
if($pixels) $this->maxHeigh = $pixels;
}
public function setMaxDim($pixels)
{
$this->setMaxWidth($pixels);
$this->setMaxHeight($pixels);
}
public function crop($width, $height, $x = 0, $y = 0)
{
if($width > 0 && $height > 0)
{
$this->cropParameters = array('x' => 0, 'y' => 0, 'width' => $width, 'height' => $height);
}
}
private function createThumb($folder, $maxWidth, $maxHeight, $cropWidth = 0, $cropHeight = 0, $cropX = 0, $cropY = 0)
{
$thumb = new self();
$thumb->setPath($this->getFolderPath());
$thumb->setMaxWidth($maxWidth);
$thumb->setMaxHeight($maxHeight);
$thumb->setFolder($folder);
return $thumb;
}
private function createCroppedThumb($folder, $maxWidth, $maxHeight, $cropWidth, $cropHeight, $cropX, $cropY)
{
$thumb = $this->createThumb($folder, $maxWidth, $maxHeight);
$thumb->crop($cropWidth, $cropHeight, $cropX, $cropY);
return $thumb;
}
public function addThumb($folder, $maxWidth, $maxHeight)
{
$this->thumbs[$folder] = $this->createThumb($folder, $maxWidth, $maxHeight);
}
public function addCroppedThumb($folder, $maxWidth, $maxHeight, $cropWidth, $cropHeight, $cropX = 0, $cropY = 0)
{
$this->thumbs[$folder] = $this->createCroppedThumb($folder, $maxWidth, $maxHeight, $cropWidth, $cropHeight, $cropX, $cropY);
}
public function sendThumbs()
{
$sends = 0;
foreach ($this->thumbs as $thumb)
{
$thumb->oldName = $this->oldName;
$thumb->setName($this->name);
$thumb->setTmpName($this->tmp_name);
$thumb->setSize($this->size);
$thumb->setType($this->type);
if($thumb->send()) $sends++;
}
return $sends == count($this->thumbs) && parent::send();
}
public function deleteThumbs()
{
$sends = 0;
foreach ($this->thumbs as $folder => $thumb)
{
$thumb->setName($this->name);
if($thumb->delete()) $sends++;
}
return $sends == count($this->thumbs);
}
private function setTransparency($newImage, $imageSource)
{
$transparencyIndex = imagecolortransparent($imageSource);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);
if($transparencyIndex >= 0)
{
$transparencyColor = imagecolorsforindex($imageSource, $transparencyIndex);
}
$transparencyIndex = imagecolorallocate($newImage, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
imagefill($newImage, 0, 0, $transparencyIndex);
imagecolortransparent($newImage, $transparencyIndex);
}
private function copyAndResampled($newImage, $imageSource, $width, $height)
{
imagecopyresampled($newImage, $imageSource, $this->dst_x, $this->dst_y, $this->src_x, $this->src_y, $width, $height, $this->width, $this->height);
}
private function copyAndResampledTransparency($newImage, $imageSource, $width, $height)
{
$this->setTransparency($newImage, $imageSource);
$this->copyAndResampled($newImage, $imageSource, $width, $height);
}
private function resize($imageSource, $transparency)
{
$width = $this->width;
$height = $this->height;
if ($width > $this->maxWidth)
{
$height = $height * ($this->maxWidth / $width);
$width = $this->maxWidth;
}
if ($height > $this->maxHeigh)
{
$width = $width * ($this->maxHeigh / $height);
$height = $this->maxHeigh;
}
$newImage = imagecreatetruecolor($width, $height);
$transparency? $this->copyAndResampledTransparency($newImage, $imageSource, $width, $height) : $this->copyAndResampled($newImage, $imageSource, $width, $height);
return $this->cropParameters? imagecrop($newImage, $this->cropParameters) : $newImage;
}
private function createJPEG()
{
return imagejpeg($this->resize(imagecreatefromjpeg($this->tmp_name), false), $this->getFullPath());
}
private function createPNG()
{
return imagepng($this->resize(imagecreatefrompng($this->tmp_name), true), $this->getFullPath());
}
private function createGIF()
{
return imagegif($this->resize(imagecreatefromgif($this->tmp_name), true), $this->getFullPath());
}
private function createBMP()
{
return imagebmp($this->resize(imagecreatefrombmp($this->tmp_name), false), $this->getFullPath());
}
private function create()
{
switch ($this->ext)
{
case 'jpg':
case 'jpeg':
return $this->createJPEG();
break;
case 'png':
return $this->createPNG();
break;
case 'gif':
return $this->createGIF();
break;
case 'bmp':
return $this->createBMP();
break;
default:
return false;
}
}
private function loadDimensions()
{
list($this->width, $this->height) = getimagesize($this->tmp_name);
}
private function analyseDimensions()
{
return ($this->width > $this->maxWidth || $this->height > $this->maxHeigh)? $this->create() : parent::upload();
}
protected function upload()
{
$this->loadDimensions();
return $this->analyseDimensions();
}
public function send()
{
return count($this->thumbs)? $this->sendThumbs() : parent::send();
}
public function delete()
{
return count($this->thumbs)? $this->deleteThumbs() : parent::delete();
}
}
?>