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