|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace XoopsModules\Tdmdownloads\Common; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
You may not change or alter any portion of this comment or credits |
|
7
|
|
|
of supporting developers from this source code or any supporting source code |
|
8
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
9
|
|
|
|
|
10
|
|
|
This program is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @copyright module for xoops |
|
17
|
|
|
* @license GPL 2.0 or later |
|
18
|
|
|
* @package wggallery |
|
19
|
|
|
* @since 1.0 |
|
20
|
|
|
* @min_xoops 2.5.9 |
|
21
|
|
|
* @author Wedega - Email:<[email protected]> - Website:<https://wedega.com> |
|
22
|
|
|
*/ |
|
23
|
|
|
trait ImageResizer |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* resize image if size exceed given width/height |
|
27
|
|
|
* @param $sourcefile |
|
28
|
|
|
* @param string $endfile |
|
29
|
|
|
* @param int $max_width |
|
30
|
|
|
* @param int $max_height |
|
31
|
|
|
* @param $imageMimetype |
|
32
|
|
|
* @return string|bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function resizeImage($sourcefile, $endfile, $max_width, $max_height, $imageMimetype) |
|
35
|
|
|
{ |
|
36
|
|
|
// check file extension |
|
37
|
|
|
|
|
38
|
|
|
switch ($imageMimetype) { |
|
39
|
|
|
case 'image/png': |
|
40
|
|
|
$img = \imagecreatefrompng($sourcefile); |
|
41
|
|
|
break; |
|
42
|
|
|
case 'image/jpeg': |
|
43
|
|
|
$img = \imagecreatefromjpeg($sourcefile); |
|
44
|
|
|
break; |
|
45
|
|
|
case 'image/gif': |
|
46
|
|
|
$img = \imagecreatefromgif($sourcefile); |
|
47
|
|
|
break; |
|
48
|
|
|
default: |
|
49
|
|
|
return 'Unsupported format'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$width = \imagesx($img); |
|
53
|
|
|
|
|
54
|
|
|
$height = \imagesy($img); |
|
55
|
|
|
|
|
56
|
|
|
if ($width > $max_width || $height > $max_height) { |
|
57
|
|
|
// recalc image size based on max_width/max_height |
|
58
|
|
|
|
|
59
|
|
|
if ($width > $height) { |
|
60
|
|
|
if ($width < $max_width) { |
|
61
|
|
|
$new_width = $width; |
|
62
|
|
|
} else { |
|
63
|
|
|
$new_width = $max_width; |
|
64
|
|
|
|
|
65
|
|
|
$divisor = $width / $new_width; |
|
66
|
|
|
|
|
67
|
|
|
$new_height = \floor($height / $divisor); |
|
68
|
|
|
} |
|
69
|
|
|
} elseif ($height < $max_height) { |
|
70
|
|
|
$new_height = $height; |
|
71
|
|
|
} else { |
|
72
|
|
|
$new_height = $max_height; |
|
73
|
|
|
|
|
74
|
|
|
$divisor = $height / $new_height; |
|
75
|
|
|
|
|
76
|
|
|
$new_width = \floor($width / $divisor); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Create a new temporary image. |
|
80
|
|
|
|
|
81
|
|
|
$tmpimg = \imagecreatetruecolor($new_width, $new_height); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
imagealphablending($tmpimg, false); |
|
84
|
|
|
|
|
85
|
|
|
imagesavealpha($tmpimg, true); |
|
86
|
|
|
|
|
87
|
|
|
// Copy and resize old image into new image. |
|
88
|
|
|
|
|
89
|
|
|
\imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
|
90
|
|
|
|
|
91
|
|
|
\unlink($endfile); |
|
92
|
|
|
|
|
93
|
|
|
//compressing the file |
|
94
|
|
|
|
|
95
|
|
|
switch ($imageMimetype) { |
|
96
|
|
|
case 'image/png': |
|
97
|
|
|
\imagepng($tmpimg, $endfile, 0); |
|
98
|
|
|
break; |
|
99
|
|
|
case 'image/jpeg': |
|
100
|
|
|
\imagejpeg($tmpimg, $endfile, 100); |
|
101
|
|
|
break; |
|
102
|
|
|
case 'image/gif': |
|
103
|
|
|
\imagegif($tmpimg, $endfile); |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// release the memory |
|
108
|
|
|
|
|
109
|
|
|
\imagedestroy($tmpimg); |
|
110
|
|
|
} else { |
|
111
|
|
|
return 'copy'; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
\imagedestroy($img); |
|
115
|
|
|
|
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param $src_url |
|
121
|
|
|
* @param $src_mimetype |
|
122
|
|
|
* @param $dest_url |
|
123
|
|
|
* @param $dest_w |
|
124
|
|
|
* @param $dest_h |
|
125
|
|
|
* @param int $quality |
|
126
|
|
|
* @return bool|string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function resizeAndCrop($src_url, $src_mimetype, $dest_url, $dest_w, $dest_h, $quality = 90) |
|
129
|
|
|
{ |
|
130
|
|
|
// check file extension |
|
131
|
|
|
|
|
132
|
|
|
switch ($src_mimetype) { |
|
133
|
|
|
case 'image/png': |
|
134
|
|
|
$original = \imagecreatefrompng($src_url); |
|
135
|
|
|
break; |
|
136
|
|
|
case 'image/jpeg': |
|
137
|
|
|
$original = \imagecreatefromjpeg($src_url); |
|
138
|
|
|
break; |
|
139
|
|
|
case 'image/gif': |
|
140
|
|
|
$original = \imagecreatefromgif($src_url); |
|
141
|
|
|
break; |
|
142
|
|
|
default: |
|
143
|
|
|
return 'Unsupported format'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (!$original) { |
|
147
|
|
|
return false; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// GET ORIGINAL IMAGE DIMENSIONS |
|
151
|
|
|
|
|
152
|
|
|
[$original_w, $original_h] = \getimagesize($src_url); |
|
153
|
|
|
|
|
154
|
|
|
// RESIZE IMAGE AND PRESERVE PROPORTIONS |
|
155
|
|
|
|
|
156
|
|
|
$dest_w_resize = $dest_w; |
|
157
|
|
|
|
|
158
|
|
|
$dest_h_resize = $dest_h; |
|
159
|
|
|
|
|
160
|
|
|
if ($original_w > $original_h) { |
|
161
|
|
|
$dest_h_ratio = $dest_h / $original_h; |
|
162
|
|
|
|
|
163
|
|
|
$dest_w_resize = (int)\round($original_w * $dest_h_ratio); |
|
164
|
|
|
} else { |
|
165
|
|
|
$dest_w_ratio = $dest_w / $original_w; |
|
166
|
|
|
|
|
167
|
|
|
$dest_h_resize = (int)\round($original_h * $dest_w_ratio); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($dest_w_resize < $dest_w) { |
|
171
|
|
|
$dest_h_ratio = $dest_w / $dest_w_resize; |
|
172
|
|
|
|
|
173
|
|
|
$dest_h_resize = (int)\round($dest_h * $dest_h_ratio); |
|
174
|
|
|
|
|
175
|
|
|
$dest_w_resize = $dest_w; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
// CREATE THE PROPORTIONAL IMAGE RESOURCE |
|
179
|
|
|
|
|
180
|
|
|
$thumb = \imagecreatetruecolor($dest_w_resize, $dest_h_resize); |
|
181
|
|
|
|
|
182
|
|
|
if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $dest_w_resize, $dest_h_resize, $original_w, $original_h)) { |
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
|
187
|
|
|
|
|
188
|
|
|
$final = \imagecreatetruecolor($dest_w, $dest_h); |
|
189
|
|
|
|
|
190
|
|
|
$dest_w_offset = 0; |
|
191
|
|
|
|
|
192
|
|
|
$dest_h_offset = 0; |
|
193
|
|
|
|
|
194
|
|
|
if ($dest_w < $dest_w_resize) { |
|
195
|
|
|
$dest_w_offset = (int)\round(($dest_w_resize - $dest_w) / 2); |
|
196
|
|
|
} else { |
|
197
|
|
|
$dest_h_offset = (int)\round(($dest_h_resize - $dest_h) / 2); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (!\imagecopy($final, $thumb, 0, 0, $dest_w_offset, $dest_h_offset, $dest_w_resize, $dest_h_resize)) { |
|
201
|
|
|
return false; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
// STORE THE FINAL IMAGE - WILL OVERWRITE $dest_url |
|
205
|
|
|
|
|
206
|
|
|
if (!\imagejpeg($final, $dest_url, $quality)) { |
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param $src_url |
|
215
|
|
|
* @param $dest_url |
|
216
|
|
|
* @param $pos |
|
217
|
|
|
* @param $of |
|
218
|
|
|
*/ |
|
219
|
|
|
public function mergeImage($src_url, $dest_url, $pos, $of) |
|
220
|
|
|
{ |
|
221
|
|
|
$dest = \imagecreatefromjpeg($dest_url); |
|
222
|
|
|
|
|
223
|
|
|
$src = \imagecreatefromjpeg($src_url); |
|
224
|
|
|
|
|
225
|
|
|
// ImageCopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h ) |
|
226
|
|
|
|
|
227
|
|
|
// $src = imagecreatefromjpeg($src_url); |
|
228
|
|
|
|
|
229
|
|
|
if (4 == $of) { |
|
230
|
|
|
switch ($pos) { |
|
231
|
|
|
case 1: |
|
232
|
|
|
\imagecopy($dest, $src, 0, 0, 0, 0, 199, 149); //top left |
|
233
|
|
|
break; |
|
234
|
|
|
case 2: |
|
235
|
|
|
\imagecopy($dest, $src, 201, 0, 0, 0, 199, 149); //top right |
|
236
|
|
|
break; |
|
237
|
|
|
case 3: |
|
238
|
|
|
\imagecopy($dest, $src, 0, 151, 0, 0, 199, 149); //bottom left |
|
239
|
|
|
break; |
|
240
|
|
|
case 4: |
|
241
|
|
|
\imagecopy($dest, $src, 201, 151, 0, 0, 199, 149); //bottom right |
|
242
|
|
|
break; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
if (6 == $of) { |
|
247
|
|
|
switch ($pos) { |
|
248
|
|
|
case 1: |
|
249
|
|
|
\imagecopy($dest, $src, 0, 0, 0, 0, 133, 149); //top left |
|
250
|
|
|
break; |
|
251
|
|
|
case 2: |
|
252
|
|
|
\imagecopy($dest, $src, 134, 0, 0, 0, 133, 149); //top center |
|
253
|
|
|
break; |
|
254
|
|
|
case 3: |
|
255
|
|
|
\imagecopy($dest, $src, 268, 0, 0, 0, 133, 149); //top right |
|
256
|
|
|
break; |
|
257
|
|
|
case 4: |
|
258
|
|
|
\imagecopy($dest, $src, 0, 151, 0, 0, 133, 149); //bottom left |
|
259
|
|
|
break; |
|
260
|
|
|
case 5: |
|
261
|
|
|
\imagecopy($dest, $src, 134, 151, 0, 0, 133, 149); //bottom center |
|
262
|
|
|
break; |
|
263
|
|
|
case 6: |
|
264
|
|
|
\imagecopy($dest, $src, 268, 151, 0, 0, 133, 149); //bottom right |
|
265
|
|
|
break; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
\imagejpeg($dest, $dest_url); |
|
270
|
|
|
|
|
271
|
|
|
\imagedestroy($src); |
|
272
|
|
|
|
|
273
|
|
|
\imagedestroy($dest); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* resize image if size exceed given width/height |
|
278
|
|
|
* @param string $endfile |
|
279
|
|
|
* @param int $max_width |
|
280
|
|
|
* @param int $max_height |
|
281
|
|
|
* @return string|bool |
|
282
|
|
|
*/ |
|
283
|
|
|
/* private function resizeImageSave($endfile, $max_width, $max_height){ |
|
284
|
|
|
// check file extension |
|
285
|
|
|
switch ($this->imageMimetype) { |
|
286
|
|
|
case'image/png': |
|
287
|
|
|
$img = imagecreatefrompng($this->imagePath); |
|
288
|
|
|
|
|
289
|
|
|
break; |
|
290
|
|
|
case'image/jpeg': |
|
291
|
|
|
$img = imagecreatefromjpeg($this->imagePath); |
|
292
|
|
|
break; |
|
293
|
|
|
case'image/gif': |
|
294
|
|
|
$img = imagecreatefromgif($this->imagePath); |
|
295
|
|
|
break; |
|
296
|
|
|
default: |
|
297
|
|
|
return 'Unsupported format'; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
$width = imagesx($img); |
|
301
|
|
|
$height = imagesy($img); |
|
302
|
|
|
|
|
303
|
|
|
if ($width > $max_width || $height > $max_height) { |
|
304
|
|
|
// recalc image size based on max_width/max_height |
|
305
|
|
|
if ($width > $height) { |
|
306
|
|
|
if ($width < $max_width) { |
|
307
|
|
|
$new_width = $width; |
|
308
|
|
|
} else { |
|
309
|
|
|
$new_width = $max_width; |
|
310
|
|
|
$divisor = $width / $new_width; |
|
311
|
|
|
$new_height = floor($height / $divisor); |
|
312
|
|
|
} |
|
313
|
|
|
} else if($height < $max_height){ |
|
314
|
|
|
$new_height = $height; |
|
315
|
|
|
} else { |
|
316
|
|
|
$new_height = $max_height; |
|
317
|
|
|
$divisor = $height / $new_height; |
|
318
|
|
|
$new_width = floor($width / $divisor); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
// Create a new temporary image. |
|
322
|
|
|
$tmpimg = imagecreatetruecolor($new_width, $new_height); |
|
323
|
|
|
imagealphablending($tmpimg, false); |
|
324
|
|
|
imagesavealpha($tmpimg, true); |
|
325
|
|
|
|
|
326
|
|
|
// Copy and resize old image into new image. |
|
327
|
|
|
imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
|
328
|
|
|
|
|
329
|
|
|
//compressing the file |
|
330
|
|
|
switch ($this->imageMimetype) { |
|
331
|
|
|
case'image/png': |
|
332
|
|
|
imagepng($tmpimg, $endfile, 0); |
|
333
|
|
|
break; |
|
334
|
|
|
case'image/jpeg': |
|
335
|
|
|
imagejpeg($tmpimg, $endfile, 100); |
|
336
|
|
|
break; |
|
337
|
|
|
case'image/gif': |
|
338
|
|
|
imagegif($tmpimg, $endfile); |
|
339
|
|
|
break; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
// release the memory |
|
343
|
|
|
imagedestroy($tmpimg); |
|
344
|
|
|
} else { |
|
345
|
|
|
return 'copy'; |
|
346
|
|
|
} |
|
347
|
|
|
imagedestroy($img); |
|
348
|
|
|
return true; |
|
349
|
|
|
} */ |
|
350
|
|
|
} |
|
351
|
|
|
|