-
Notifications
You must be signed in to change notification settings - Fork 3
/
system_functions.php
408 lines (327 loc) · 15 KB
/
system_functions.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
if(!function_exists("admin_only")){
function admin_only(){
global $is_admin, $gallery_url;
if(!$is_admin){
$_SESSION['session_return_url'] = $_SERVER['REQUEST_URI'];
header("Location: ".$gallery_url."/login?message=you need to login to access that page&message_type=error");
exit;
}
}
}
// a function that will truncate a string
// this function will truncate a word if needed
if (!function_exists("truncate_by_letters")) {
function truncate_by_letters($string,$chars,$append = '...') {
if(mb_strlen($string,"UTF-8") > $chars) {
$string = mb_substr($string, 0, $chars,"UTF-8");
if($trunc_at !== FALSE)
$string = mb_substr($string, 0, $chars,"UTF-8");
$string = $string.$append;
}
return $string;
}
}
if(!function_exists("size_convert")){
function size_convert($size){
$unit=array('B','KB','MB','GB','TB','PB');
$string_to_return = round($size/pow(1024,($i=floor(log($size,1024)))),2);
return number_format($string_to_return,2,".",' ').' '.$unit[$i];
}
}
// !!! attention, this function uses the function "normalize_characters" too !!!
// formats a string like "ads zxcv eew" to "ads-zxcv-eew";
if(!function_exists("string_to_url")){
function string_to_url($string){
//normalize it BEFORE replacing the unacepted chars
$string = normalize_characters($string);
//
$string = preg_replace("/[^A-Za-z0-9]/", "-", $string);
$string = preg_replace("/[-]+/", '-', $string);
$string = trim($string,"-");
$string = strtolower($string);
return $string;
}
}
// Converts "Sandra" to "Sandra" and "Bucuresti" to "Bucuresti"
if(!function_exists("normalize_characters")){
function normalize_characters($string){
// !!!
// !!! NOTE that SPECIAL chars like "ă" might convert themselfs into "a" in this file;
// in that case, you need to pick UTF-8 when saving this file with NOTEPAD
// OR in Dreamwever it seems to work ok when saving the file and picking "C (Canonical Decomposition, followed by ...)" under file name
$string = str_replace(array("ä", "ă", "î", "ş", "ţ", "â", "ő", "ö", "í", "Ă", "Î", "Ş", "Ţ", "Â", "Ő", "Ö", "ĺ", "Á", "á", "É", "é", "Í", "í", "Ñ", "ñ", "Ó", "ó", "Ú", "ú", "Ü", "ü", "¿", "¡", "è", "È", "Ā", "ß", "Ä"), array("a", "a", "i", "s", "t", "a", "o", "o", "i", "A", "I", "S", "T", "A", "O", "O", "I", "A", "a", "E", "e", "I", "i", "N", "n", "O", "o", "U", "u", "U", "u", "?", "i", "e", "E", "A", "ss", "A"), $string);
return $string;
}
}
// resizes an image to exactly given sizes by cropping the larger size
if(!function_exists("crop_image")){
function crop_image($original_file, $destination_file, $new_width = 120, $new_height = 90){
list($original_width,$original_height) = getimagesize($original_file);
// first test if difference between old/new width are larger than diff betwern old/new height
$width_difference = $original_width/$new_width;
$height_difference = $original_height/$new_height;
// resize width first if width difference < height difference or if differences are the same (old/new image have same proportions);
// resize width first if width difference is smaller than height difference
if($width_difference<=$height_difference){
$resize_first = "width";
}
// resize height first if height difference is smaller than width difference
if($height_difference<$width_difference){
$resize_first = "height";
}
if($resize_first == "width"){
exec("convert ".escapeshellarg($original_file)." -resize ".$new_width."x -flatten -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 ".escapeshellarg($destination_file));
} else {
exec("convert ".escapeshellarg($original_file)." -resize x".$new_height." -flatten -gravity center -crop ".$new_width."x".$new_height."+0+0 -quality 100 ".escapeshellarg($destination_file));
}
}
}
// crop_image("ferrari.jpg","ferrari_new.jpg",120,90);
// resize an image so that it doesn't pass given maximum values
if(!function_exists("resize_in_limits")){
function resize_in_limits($original_file, $destination_file, $max_width = 125, $max_height = 200){
list($original_width,$original_height) = getimagesize($original_file);
// prevent scalling up smaller images ? (get the smaller one...)
$max_width = min($max_width,$original_width);
$max_height = min($max_height,$original_height);
// first test if difference between old/new width are larger than diff betwern old/new height
$width_difference = $original_width/$max_width;
$height_difference = $original_height/$max_height;
// resize by width if width_difference is smaller than height_difference
if($width_difference>=$height_difference){
exec("convert ".escapeshellarg($original_file)." -resize ".$max_width."x -quality 100 -flatten ".escapeshellarg($destination_file));
}
// resize by height if width_difference is smaller than height_difference
if($width_difference<$height_difference){
exec("convert ".escapeshellarg($original_file)." -resize x".$max_height." -quality 100 -flatten ".escapeshellarg($destination_file));
}
}
}
// resize_in_limits("ferrari.jpg","ferrari_new.jpg",600,400);
// resize an image into a maximum new width/height
if(!function_exists("gd_resize_in_limits")){
function gd_resize_in_limits($original_file, $destination_file=NULL, $maximum_width = 550, $maximum_height = 413){
// get width and height of original image
list($original_width,$original_height) = getimagesize($original_file);
// do not scale image up ?!
$maximum_width = min($maximum_width, $original_width);
$maximum_height = min($maximum_height, $original_height);
// calculating new width x height
// decide if the width or the height is the one that needs to be scalled the most in order to fit in the box
$width_scale_difference = $original_width/$maximum_width;
$height_scale_difference = $original_height/$maximum_height;
$resize_by = "width";
if($height_scale_difference > $width_scale_difference){
$resize_by = "height";
}
// resize by width
if($resize_by == "width"){
$new_width = $maximum_width;
$percent = $original_height/$original_width;
$new_height = $new_width*$percent;
}
// resize by height
if($resize_by == "height"){
$new_height = $maximum_height;
$percent = $original_width/$original_height;
$new_width = $new_height*$percent;
}
if($original_height > $original_width){
$new_height = $maximum_height;
$percent = $original_height/$original_width;
$new_width = $new_height/$percent;
}
if($original_width==$original_height){
$new_width = $maximum_width;
$new_height = $maximum_height;
}
$new_width = round($new_width);
$new_height = round($new_height);
// if original image was smaller then do not scale it up.
if($original_width<$new_width and $original_height<$new_height){
$new_width = $original_width;
$new_height = $original_height;
}
$smaller_image = imagecreatetruecolor($new_width, $new_height);
// load the image
if(substr_count(strtolower($original_file), ".jpg") or substr_count(strtolower($original_file), ".jpeg")){
$original_image = imagecreatefromjpeg($original_file);
}
if(substr_count(strtolower($original_file), ".gif")){
$original_image = imagecreatefromgif($original_file);
}
if(substr_count(strtolower($original_file), ".png")){
$original_image = imagecreatefrompng($original_file);
}
imagecopyresampled($smaller_image,$original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
// enable interlace, seems to be have clearer lines (tested with a large thin "x"), file size is smaller too
// doesn't work on most servers
// imageinterlace($square_image, true);
// if no destination file was given then display a jpg or png
if(!$destination_file){
//imagepng($smaller_image,NULL,9);
imagejpeg($smaller_image,NULL,100);
}
// save the smaller image FILE if destination file given
if(substr_count(strtolower($destination_file), ".jpg")){
imagejpeg($smaller_image,$destination_file,100);
}
if(substr_count(strtolower($destination_file), ".gif")){
imagegif($smaller_image,$destination_file);
}
if(substr_count(strtolower($destination_file), ".png")){
imagepng($smaller_image,$destination_file,9);
}
imagedestroy($original_image);
imagedestroy($smaller_image);
}
}
// gd_resize_in_limits("ferrari.jpg",NULL,1300,1000);
// a function that crops an image to any given size and centers the content (can also make square thumbs like another function).
if(!function_exists("gd_crop_image")){
function gd_crop_image($original_file, $destination_file=NULL, $new_width = 50, $new_height = 50){
// get width and height of original image
list($original_width,$original_height) = getimagesize($original_file);
// get file extension
$path_parts = pathinfo($original_file);
$image_extension = $path_parts['extension'];
// load the original image
if($image_extension == "jpg" or $image_extension == "jpeg"){
$original_image = imagecreatefromjpeg($original_file);
}
if($image_extension == "gif"){
$original_image = imagecreatefromgif($original_file);
}
if($image_extension == "png"){
$original_image = imagecreatefrompng($original_file);
}
// - if original width or original height smaller than new ones then resize according to smaller one
// - detect original width - new width and original height - new height, resize according to smallest result.
// first test if difference between old/new width are larger than diff betwern old/new height
$width_difference = $original_width/$new_width;
$height_difference = $original_height/$new_height;
// resize width first if width difference < height difference or if differences are the same (old/new image have same proportions);
// resize width first if width difference is smaller than height difference
if($height_width < $height_difference){
$resize_first = "width";
}
// resize height first if height difference is smaller than width difference
if($height_difference<$width_difference){
$resize_first = "height";
}
// new width/height has same proportions as original width/height
if($height_difference==$width_difference){
$resize_first = "equal";
}
// create a new image that will hold the result
$cropped_image = imagecreatetruecolor($new_width, $new_height);
// enable interlace, seems to be have clearer lines (tested with a large thin "x")
// file size is smaller too, doesn't work on most servers
//imageinterlace($cropped_image, true);
// resize width first, start x and dest x will be 0;
if($resize_first == "width"){
// width is directly $new_width and height is temporary, before fited in new size
$temporary_height = round($original_height/$width_difference);
// get the proportions/difference between the temporary height and the needed height
$temporary_difference = ($temporary_height/$new_height);
if($temporary_difference>0){
$source_y = $original_height-($original_height/$temporary_difference);
} else {
$source_y=0;
}
$source_y = round($source_y/2);
imagecopyresampled($cropped_image,$original_image, 0, 0, 0, $source_y, $new_width, $temporary_height, $original_width, $original_height);
}
// resize height first, start x and dest x will be 0;
if($resize_first == "height"){
// height is directly $new_height and width is temporary, before fited in new size
$temporary_width = round($original_width/$height_difference);
// get the proportions/difference between the temporary width and the needed width
$temporary_difference = $temporary_width/$new_width;
//echo "$original_width-$original_width/$temporary_difference"; exit;
if($temporary_difference>0){
$source_x = $original_width-($original_width/$temporary_difference);
} else {
$source_x = 0;
}
$source_x = round($source_x/2);
imagecopyresampled($cropped_image,$original_image, 0, 0, $source_x, 0, $temporary_width, $new_height, $original_width, $original_height);
}
// meaning that original image has same proportions as new image
if($resize_first == "equal"){
imagecopyresampled($cropped_image,$original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
}
// output image in browser no destination file
if(!$destination_file){
//header("Content-type: image/png");
//imagepng($cropped_image,NULL,9);
header("Content-type: image/jpeg");
imagejpeg($cropped_image,NULL,100);
}
// get file extension of destination file
if(isset($destination_file) and $destination_file!=""){
$destination_path_parts = pathinfo($original_file);
$destination_image_extension = $path_parts['extension'];
}
if($destination_image_extension == "jpg" or $destination_image_extension == "jpeg"){
imagejpeg($cropped_image,$destination_file,100);
}
if($destination_image_extension == "gif"){
imagegif($cropped_image,$destination_file);
}
if($destination_image_extension == "png"){
imagepng($cropped_image,$destination_file,9);
}
imagedestroy($original_image);
imagedestroy($cropped_image);
}
}
// gd_crop_image("temp/ferrari.jpg",NULL,300,200);
// a function to remove all contents of a directory (including dir)
if (!function_exists("rmdir_r")) {
function rmdir_r($path){
if (is_dir($path) && !is_link($path)){
if ($dh = opendir($path)){
while (($sf = readdir($dh)) !== false){
if ($sf == '.' || $sf == '..'){
continue;
}
if(!rmdir_r($path.'/'.$sf)){
// throw new Exception($path.'/'.$sf.' could not be deleted.');
// this code ^ fails on some servers, trying print_r instead
print_r($path.'/'.$sf.' could not be deleted.');
}
}
closedir($dh);
}
return rmdir($path);
}
return unlink($path);
}
}
// removes special characters that are unsafe for file names
if(!function_exists("string_to_file_name")){
function string_to_file_name($string){
$string = strip_tags($string);
$string = trim($string);
$string = str_replace("#", "", $string);
$string = str_replace("&", "", $string);
$string = str_replace("+", "", $string);
$string = str_replace("/", "", $string);
$string = str_replace("\\", "", $string);
$string = str_replace("?", "", $string);
$string = str_replace(":", "", $string);
$string = str_replace("*", "", $string);
$string = str_replace("<", "", $string);
$string = str_replace(">", "", $string);
// Dreamweaver shows this car as reserved when renaming a folder, so don't allow it here eider
$string = str_replace("|", "", $string);
// windows doesn't accept " but accepts '
$string = str_replace('"', "", $string);
// trim it again after removed some characters that might have been on margins after a space
$string = trim($string);
return $string;
}
}
?>