/home/banprang/domains/plai.go.th/public_html/coremain/function_msg.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<?php
// fieldset
function fieldset_top($name_top){
    echo 
"<fieldset><legend align=left><font color='$datatm[color1]'>$name_top </font></legend>&nbsp;";
}
function 
fieldset_down(){
    echo 
"</fieldset>";
}

 
// ########### ไม่มีสิทธิ์ในหน้านี้ ############
function permission_fail(){
bar_header("คุณไมีมีสิทธิ์ในหน้านี้"); // Bar_Header
fieldset_top("คุณไมีมีสิทธิ์ในหน้านี้");
    print 
"<font color='ff0000'><center><b>การกระทำของคุณล่อแหลมต่อความปลอดภัยของระบบ</b></font>";
    print 
"<br><br><a href='index.php'><font color='ff0000'>คลิกที่นี่</font></a> เพื่อกลับสู่หน้าหลัก</center>";
$widthsl='190';
fieldset_down();
include(
"footer.php");
exit;
}
 
// ########### ไม่มีสิทธิ์ใน Module นี้ ##########
function fieldset_no_module(){
bar_header("หน่วยงานของท่านไม่มีสิทธิ์ในหน้านี้"); // Bar_Header
fieldset_top("หน่วยงานของท่านไม่มีสิทธิ์ในหน้านี้");
    print 
"<font color='ff0000'><center><b>ขออภัย หน่วยงานของท่านไม่มีสิทธิ์ในหน้านี้ </b></font>";
    print 
"<br><br><font color='000000'><center><b>หากท่านต้องการใช้ Module ส่วนนี้ กรุณาติดต่อ <br><br>opstech@windowslive.com : <a href='http://www.opstech.co.th' target=blank>http://www.opstech.co.th</b></font>";    
    print 
"<br><br><a href='index.php'><font color='ff0000'>คลิกที่นี่</font></a> เพื่อกลับสู่หน้าหลัก</center>";
$widthsl='180';    
fieldset_down();
include(
"footer.php");
exit;
}
// ข้อมูลได้ถูกเพิ่มแล้ว
function msg_insert_data(){
        echo 
"<center><br><br>ข้อมูลได้ถูกเพิ่มแล้ว<br><br><br></center>";
}    
// ข้อมูลได้ถูกแก้ไขแล้ว
function msg_update_data(){
        echo 
"<center><br><br>ข้อมูลได้ถูกแก้ไขแล้ว<br><br><br></center>";
}    
// ข้อมูลได้ถูกลบแล้ว
function msg_delete_data(){
        echo 
"<center><br><br>ข้อมูลได้ถูกลบแล้ว<br><br><br></center>";
}    
// Refresh หน้าจอ
function refresh_data($index_link,$time){
        if(
$time==""$time=2;
        echo
"<meta http-equiv='refresh' content='$time; url=$index_link'>";
}    

// อธิบายขนาดของรูปว่าเท่าไหร่ถึงจะดี
function detail_pic(){
    echo
"&nbsp; <img src='coremain/images/bu.gif' border='0' title='รูปควรมีขนาดประมาณ 400*300 Pixel' onmouseover=this.style.cursor='hand'>";
}    
// อธิบายชนิดของไฟล์ว่าเป็นอย่างไร
function detail_file(){
    echo
"&nbsp; <img src='coremain/images/bu.gif' border='0' title='ไฟล์ต้องไม่ใช่ .php หรือ .asp หรือ .exe' onmouseover=this.style.cursor='hand'>";
}    

// #### Create Text Editor ####
/*function text_editor($width,$height,$values,$msg){
echo"<tr height='40'>";
echo"       <td><div align='left'><img src='coremain/images/marker.gif'> $msg</div></td> ";
echo"     <td><div align='left'></div>";
                  $oFCKeditor = new FCKeditor('evar') ;
                  $oFCKeditor->BasePath    = 'coremain/editor/';
                  $oFCKeditor->Value        = $values;
                  $oFCKeditor->Width = $width;
                  $oFCKeditor->Height = $height;
                  $oFCKeditor->Create() ;
echo"    </td> ";
echo"</tr>";
}*/

//### File Upload ###
/*function file_upload($msg, $id, $desc, $fold, $form, $input, $maxFileCount = 0){
  echo "<tr height='40'>";
    echo "<td><div align='left'><img src='coremain/images/marker.gif'> $msg</div></td> ";
    echo "<td><div id='fileuploader'>เลือกไฟล์...</div><div id='txt_error'></div></td>";
  echo "</tr>";
  echo "<script>
          var $id = $('#fileuploader').uploadFile({
              autoSubmit:false,
              url: 'coremain/file_upload.php',
              fileName: 'fname',
              dragDropStr: '',
              uploadStr: 'เลือกไฟล์...',
              cancelStr: 'ยกเลิก',
              extErrorStr: 'ไม่อนุญาตให้อัพโหลด ! กรุณาตรวจสอบประเภทไฟล์ ',
              dragDropStr: '<span>$desc</span>',
              formData: {'path_dir': '$fold'},
              returnType: 'json',
              allowedTypes: 'jpg, png, gif, doc, docx, xls, xlsx, pdf',
              afterUploadAll: function(obj){
                var num = 0;
                $.each(obj.responses, function(i, v){
                  if(v != ''){
                    $('#$form').append('<input type=\"hidden\" name=\"$input'+ (i + 1) +'\" value=\"'+ v +'\" />');
                    ++num;
                  }
                });
                if(num == 0){
                  $('#txt_error').html('<span style=\'color: red;\'>$desc</span>');
                }
              },
              onSuccess: function(){
                alert('123');
                $('#$form').submit();
                  alert('456');
              }";
    if($maxFileCount > 0){
      echo ",maxFileCount: $maxFileCount
            ,maxFileCountErrorStr: 'ไม่สำเร็จ ! เนื่องจากสามารถอัพโหลดได้มากที่สุดคือ '";
    }
  echo "  });
        </script>";
}*/


//#### ปุ่มเพิ่มข้อมูล ####
function text_button_insert($col 2){
  echo 
"<tr height='40'>";
    echo 
"<td colspan='$col' align='center'>    ";
      echo 
'<button type="submit" name="submit" class="ui-button ui-widget ui-corner-all btn-edit" style="padding: 5px 10px;">เพิ่มข้อมูล</button>';
    echo 
"</td> ";
  echo 
"</tr>";
//    echo" <input type='submit' name='Submit' value='เพิ่มข้อมูล' onmouseover=this.style.cursor='hand'>";
}
function 
text_button_mail(){
    echo
" <input type='submit' name='Submit' value='ส่งอีเมล์' onmouseover=this.style.cursor='hand'>";
}

//#### ปุ่มแก้ไขข้อมูล ####
function text_button_update($col 2$upload_id ''){
    echo 
"<tr height='40'>";
    echo 
" <td colspan='$col' align='center'>    ";
      echo 
'<button type="submit" name="submit" class="ui-button ui-widget ui-corner-all btn-edit" style="padding: 5px 10px;">แก้ไขข้อมูล</button>';
    echo 
"    </td> ";
    echo 
"  </tr>";
}

// ################ ตรวจสอบวันที่ประกาศ กับ วันสิ้นสุดประกาศ ##
function check_date($date_post,$date_line){
    if(
$date_post>$date_line){
    echo 
"<br><br><center>สิ้นสุดวันประกาศ ไม่ถูกต้อง !<br><br>";
                    echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
                    include(
'footer.php');
                    exit;
    }
}

// มีไฟล์อยู่แล้วแต่เอาออก
function check_pic_out($tables,$fields,$id_del,$paths,$id_field){
            
$sql="select $fields from $tables where $id_field='$id_del'";
            
$result=mysql_query($sql);
            
$data=mysql_fetch_row($result);
            if(
$data[0]!="")        {            
                
unlink("$paths/$data[0]");    
            }
}

// ################ ตรวจสอบชนิดของไฟล์ ################
function check_pic($userfilename$numberfile) {
  
$arr explode('.'$userfilename);
  
$strings '.'.end($arr);#$strings = substr($userfilename, -4);
  
if ($strings != ".gif" && $strings != ".jpg" && $strings != ".GIF" && $strings != ".JPG" && $strings != ".png" && $strings != ".PNG") {
    echo
"<center><br>ไฟล์ที่ $numberfile ชนิดไฟล์ไม่ถูกต้อง <br>ไฟล์ต้องมีนามสกุลเป็น .gif หรือ .jpg หรือ .png เท่านั้น <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
    echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
//    include('footer.php');
    
exit;
  }
}

/*// ################## ชนิดของไฟล์เสี่ยงต่อความปลอดภัยของระบบ ###############
function check_file($userfilename){
  $arr = explode('.', $userfilename);
  $strings = '.'.end($arr);
  if($strings == '.php' || $strings == '.asp'){
    return "<center><br>ไม่อนุญาตให้ Upload ไฟล์ชนิดนี้ <br>เนื่องจากชนิดไฟล์เสี่ยงต่อความเสียหายของระบบ <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
    //exit;
  }else{
    return 'OK';
  }
//            $strings=substr($userfilename,-4);
//            if($strings==".php" or $strings==".asp"){
//                    echo"<center><br>ไม่อนุญาตให้ Upload ไฟล์ชนิดนี้ <br>เนื่องจากชนิดไฟล์เสี่ยงต่อความเสียหายของระบบ <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
//                    echo"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
//                    include('footer.php');
//                    exit;
//            }
}

// ############## สร้างชื่อไฟล์ใหม่
function create_filename($userfilename){
//        //$strings=substr($userfilename,-4);
//        $arr = explode('.', $userfilename);
//        $strings = '.'.end($arr);
        $strings = get_file_type($userfilename);
        $str  = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        $pic1 = substr(str_shuffle($str), 0, 12);
        $name1="$pic1$strings";
        return ($name1);
}    */
// ############## Upload File ขึ้น Server
/*function upload_file_to_server($dlink, $userfile) {
  $temp_name = $userfile['tmp_name'];
  if (is_uploaded_file($userfile['tmp_name'])) {
    if (!move_uploaded_file($temp_name, $dlink)) {
      
    }
  }
}*/

// ลบไฟล์เก่าทิ้งในกรณีที่จะ Upload ไฟล์ใหม่ทับ
function delete_filename($tables,$fields,$id_del,$paths,$id_filed){
            
$sql="select $fields from $tables where $id_filed='$id_del'";
            
$result=mysql_query($sql);
            
$data=mysql_fetch_row($result);
            if(
$data[0]!="" && file_exists("$paths/$data[0]"))        {            @unlink("$paths/$data[0]");        }
}    

// Check_Picture Width
function check_pic_width($width$name1) {
  if (
$width 160) {
    
unlink("$_SESSION[web_name]/mainfile/$name1");
    echo 
"<center><br><br>ไฟล์มีขนาดความกว้างเกิน 200 Pixel ซึ่งมากเกินไป<br><br></center>";
    echo
"<a href='javascript:history.back()'><center><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
    include(
'footer.php');
    exit;
  }
}

// Check_Picture Height
function check_pic_height($height$name1) {
  if (
$height 150) {
    
unlink("$_SESSION[web_name]/mainfile/$name1");
    echo 
"<center><br><br>ไฟล์มีขนาดความสูงเกิน 150 Pixel ซึ่งมากเกินไป<br><br></center>";
    echo
"<a href='javascript:history.back()'><center><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
    include(
'footer.php');
    exit;
  }
}

// Check_User_Permission
// ################ ตรวจสอบวันที่ประกาศ กับ วันสิ้นสุดประกาศ ##
function check_user_permission($user) {
  if (
$user != "admin") {
    echo 
"<br><br><center><font color='ff0000'><b>คุณไม่มีสิทธิ์ใช้งานหน้านี้</b></font><br><br>";
    include(
'footer.php');
    exit;
  }
}





################# KJ : for new version ###############
function debuga($array){
  if(
is_array($array)){
    echo 
'<hr/><pre align="left">';
    
print_r($array);
    echo 
'</pre><hr/>';
  }else{
    echo 
'<br/>====== This data is not Array ======<br/>';
  }
}

function 
setArrayToAdd($array) {
  if (
is_array($array)) {
    foreach (
$array as $k => $v) {
      
$_array[$v['name']] = (isset($v['value'])) ? trim($v['value']) : '';
    }
    return 
$_array;
  }
}

function 
text_editor($width$height$title$colspan '2'$values ''$name 'evar'){
  if(
$colspan 0){
    echo 
"<tr height='40'>";
      echo 
"<td colspan='$colspan'>
              <img src='coremain/images/marker.gif'> <b>
$title</b><br/>";
  }
    echo 
"   <textarea id='$name' name='$name'>$values</textarea>";
  if(
$colspan 0){
      echo 
"</td>";
    echo 
"</tr>";
  }
  echo 
"<script>
          CKEDITOR.config.width = '100%';
          CKEDITOR.config.height = 
$height;
          CKEDITOR.replace( '
$name', {
            filebrowserBrowseUrl: 'coremain/ckeditor/filemanager/dialog.php?type=2&editor=ckeditor&fldr=',
            filebrowserImageBrowseUrl: 'coremain/ckeditor/filemanager/dialog.php?type=1&editor=ckeditor&fldr=',
            extraPlugins: 'youtube,iframe'
          });
          CKEDITOR.config.allowedContent = true;
        </script>"
;
}

//### File Upload ###
function file_upload($upload_id$fold$form$url$option = array()){
  
#$option = array('title', 'description', 'text_error', 'colspan', 'file_type', 'max_file', 'multiple', 'prefix', 'thumb', 'extraHTML (array)');
  
$msg = isset($option['title']) ? $option['title'] : 'ไฟล์ประกอบ';
  
$desc = isset($option['description']) ? $option['description'] : '';
  
$txtError = isset($option['text_error']) ? $option['text_error'] : 'ไม่อนุญาตให้อัพโหลด ! กรุณาตรวจสอบประเภทไฟล์ ';
  
$colspan = isset($option['colspan']) ? "colspan='$option[colspan]'" "";
  
$_file_type = isset($option['file_type']) ? $option['file_type'] : '';
  
$allowed_types $_file_type == 'image' 'jpg, png, gif' 'jpg, png, gif, doc, docx, xls, xlsx, pdf';
  
$maxFileCount = isset($option['max_file']) ? $option['max_file'] : 0;
  
$multiple = isset($option['multiple']) ? $option['multiple'] : 'true';
  
$prefix = isset($option['prefix']) ? ", 'prefix': '$option[prefix]'" '';
  
$thumb = isset($option['thumb']) && $option['thumb'] == true ", 'thumb': 'thumb'" '';
  
$extraHTML = isset($option['extraHTML']) ? 'fn' '';
  
  echo 
"<tr height='40' valign='top'>";
    echo 
"<td><div align='left'><img src='coremain/images/marker.gif'> <b>$msg</b></div></td> ";
    echo 
"<td $colspan><div id='fileuploader'>เลือก...</div><div id='txt_error'></div></td>";
  echo 
"</tr>";
  echo 
"<script>
          var 
$upload_id = $('#fileuploader').uploadFile({
              autoSubmit: false,
              url: 'coremain/file_upload.php',
              fileName: 'fname',
              dragDropStr: '',
              uploadStr: 'เลือกไฟล์...',
              cancelStr: 'ยกเลิก',
              extErrorStr: '
$txtError',
              dragDropStr: '<span>
$desc</span>',
              formData: {'path_dir': '
$fold$prefix $thumb},
              returnType: 'json',
              sequential: true,
              sequentialCount: 1,
              uploadQueueOrder: 'bottom',
              multiple: 
$multiple,
              allowedTypes: '
$allowed_types',
              afterUploadAll: function(obj){
                  var arrayData = $('#
$form').serializeArray();
                  var txtArea = $('#
$form').find('textarea');
                  if(txtArea.length > 0){
                    var row = {};
                    row.name = txtArea.attr('name');
                    row.value = CKEDITOR.instances[txtArea.attr('id')].getData();
                    arrayData.push(row);
                  }
                  $.ajax({
                      url: '
$url',
                      type: 'post',
                      data: {fileData: obj.responses, formData: arrayData},
                      success: function(result){
                        var chk = result.substr(0, 1);
                        if(chk == 0){
                          var txt = result.substr(0);
                          alert(txt);
                          return false;
                        }else{
                          window.location.href = result;
                        }
                      }
                  });
              }"
;
    if(
$_file_type == 'image'){
      echo 
',acceptFiles: "image/*"';
    }
    if(
$maxFileCount 0){
      echo 
",maxFileCount: $maxFileCount
            ,maxFileCountErrorStr: 'ไม่สำเร็จ ! เนื่องจากสามารถอัพโหลดได้มากที่สุดคือ '"
;
    }
    if(
$extraHTML == 'fn'){
      echo 
",extraHTML: function(){
                var html = '<div>';"
;
      foreach(
$option['extraHTML'] as $ele){
        if(
$ele['type'] == 'input'){
          
#array('type' => 'input', 'text' => 'คำอธิบายรูป', 'name' => 'explain', 'style' => 'width: 100%;')
          
echo "  html += '<b>$ele[text] : </b><input type=\"text\" name=\"$ele[name]\" style=\"$ele[style]\" /> <br/>';";
        }else if(
$ele['type'] == 'select'){
          
#array('type' => 'select', 'text' => 'ตำแหน่ง', 'name' => 'position', 'style' => 'width: 100%;', 'option' => array('1' => 'กลาง', '2' => 'ซ้าย', '3' => 'ขวา')
          
echo "  html += '<b>$ele[text] : </b><select name=\"$ele[name]\" style=\"$ele[style]\">';";
          foreach(
$ele['option'] as $value => $text){
            echo 
" html += '<option value=\"$value\">$text</option>';";
          }
          echo 
"  html += '</select>';";
        }
      }
      echo 
"    html += '</div>';
              return html;
            }"
;
    }
  echo 
"  });
        </script>"
;
}

//### ปุ่ม Upload เอกสาร/รูปภาพ ###
# file_upload.$upload_id = text_button_upload.$upload_id
function button_text($bt_text$name 'submit'$col 2$upload_id ''$txt_validate ''$allow_nofile_in_startUpload 'false') {
  if (
$col 0) {
    echo 
"<tr height='40'>";
    echo 
"<td colspan='$col' align='center'><br/>";
  }
  
$btType $upload_id != '' 'button' 'submit';
  echo 
'<button type="' $btType '" name="' $name '" class="ui-button ui-widget ui-corner-all ui-state-focus '.$upload_id.'" id="'.$upload_id.'" style="padding: 5px 10px;">' $bt_text '</button>';
  if (
$col 0) {
    echo 
"</td> ";
    echo 
"</tr>";
  }
  if (
$upload_id != '') {
    
$chkType explode('_'$upload_id);
    if(
$chkType[0] != 'btn'){
      echo 
"<script>
                $('.
$upload_id').click(function(){
                  
$upload_id.startUpload('$txt_validate', $allow_nofile_in_startUpload);
                });
            </script>"
;
    }
  }
}

// ############## Upload File ขึ้น Server
/*function upload_file_to_server($dlink, $file, $thumbPath = '', $width = '800', $height = '600') {
  $temp_name = $file['tmp_name'];
  if (is_uploaded_file($file['tmp_name'])) {
    if (!move_uploaded_file($temp_name, $dlink)) {
      
    }else{
      createResizedImage($dlink, $thumbPath, $width, $height);
    }
  }
}*/
function upload_file_to_server($_dlink$file$_thumbPath ''$width '800'$height '600') {
  
$arr explode('/'$_dlink);
  
$file_name end($arr);
  
$file_type get_file_type($file_name);
  
$filename str_replace($file_type''$file_name);
  
$imp_dlink implode('/'$arr);
  
$dlink str_replace($file_name''$imp_dlink);
  
$thumbPath $_thumbPath != '' str_replace($file_name''$_thumbPath) : '';
    
  include_once(
'module/class.upload.php');
  
$hup = new upload($file);
  
//chmod($output_dir, 0755);
  
if($hup->uploaded){
      
$hup->image_resize true;
      
$hup->image_y  $height;
      
$hup->image_x $width;
      
$hup->image_ratio true;            
      
$hup->jpeg_quality 95;
      
$hup->image_ratio_no_zoom_in true;
      
$hup->file_new_name_body $filename;
      
$hup->Process($dlink);
      if(
$hup->processed){

      }else{
          
//echo 'failed';
      
}
      if(
$thumbPath != ''){
          
$hup->image_resize true;
          
$hup->image_y  100;
          
$hup->image_x 150;
          
$hup->image_ratio true;            
          
$hup->jpeg_quality 95;
          
$hup->image_ratio_no_zoom_in true;            
          
$hup->file_new_name_body $filename;            
          
$hup->Process($thumbPath);
          if(
$hup->processed){

          }else{

          }
      }
  }
}

function 
createResizedImage($imagePath$thumbPath$newWidth$newHeight){ #, $outExt) {
  
$newPath $imagePath;
  if (
$newPath === '' || file_exists($imagePath) === false) {
    return 
null;
  }

  
$type = @exif_imagetype($imagePath);
  if (
in_array($type, [IMAGETYPE_JPEGIMAGETYPE_PNGIMAGETYPE_GIF]) === false) {//[IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_BMP, IMAGETYPE_WEBP]
    
return null;
  }

  list(
$width$height) = getimagesize($imagePath);

//  $outBool = in_array($outExt, ['jpg', 'jpeg', 'png', 'gif']);//, 'bmp', 'webp']);

  
switch (true) {
    case 
$type === IMAGETYPE_JPEG:
      
$image imagecreatefromjpeg($imagePath);
//      if ($outBool === false)
        
$outExt 'jpg';
      break;
    case 
$type === IMAGETYPE_PNG:
      
$image imagecreatefrompng($imagePath);
//      if ($outBool === false)
        
$outExt 'png';
      break;
    case 
$type === IMAGETYPE_GIF:
      
$image imagecreatefromgif($imagePath);
//      if ($outBool === false)
        
$outExt 'gif';
      break;
//    case $type === IMAGETYPE_BMP:
//      $image = imagecreatefrombmp($imagePath);
////      if ($outBool === false)
//        $outExt = 'bmp';
//      break;
//    case $type === IMAGETYPE_WEBP:
//      $image = imagecreatefromwebp($imagePath);
////      if ($outBool === false)
//        $outExt = 'webp';
  
}

  
### KJ : cal width height auto by one ###
//  echo '==='.$width.'::'.$newWidth.'=='.$height.'::'.$newHeight.'===';
  
if($width $newWidth || $height $newHeight){
    
$calHeight round($newWidth*$height/$width);
//    echo '++'.$calHeight.'::'.$newHeight.'++';
    
if($calHeight $newHeight && $newHeight 0){
      
$newWidth round($newHeight*$newWidth/$calHeight);
    }else{
      
$newHeight $calHeight;
    }
    
### ================ ###
    
$newImage imagecreatetruecolor($newWidth$newHeight);

    
//TRANSPARENT BACKGROUND
    
$color imagecolorallocatealpha($newImage000127); //fill transparent back
    
imagefill($newImage00$color);
    
imagesavealpha($newImagetrue);

    
//ROUTINE
    
imagecopyresampled($newImage$image0000$newWidth$newHeight$width$height);

    
// Rotate image on iOS
    
$exif = @exif_read_data($imagePath'IFD0');
    if (isset(
$exif['Orientation']) && isset($exif['Make']) && !empty($exif['Orientation']) && preg_match('/(apple|ios|iphone)/i'$exif['Make'])) {
      switch (
$exif['Orientation']) {
        case 
8:
          if (
$width $height)
            
$newImage imagerotate($newImage900);
          break;
        case 
3:
          
$newImage imagerotate($newImage1800);
          break;
        case 
6:
          
$newImage imagerotate($newImage, -900);
          break;
      }
    }

    switch (
true) {
      case 
in_array($outExt, ['jpg''jpeg']) === true$success imagejpeg($newImage$newPath);
        break;
      case 
$outExt === 'png'$success imagepng($newImage$newPath);
        break;
      case 
$outExt === 'gif'$success imagegif($newImage$newPath);
        break;
  
//    case $outExt === 'bmp': $success = imagebmp($newImage, $newPath);
  //      break;
  //    case $outExt === 'webp': $success = imagewebp($newImage, $newPath);
    
}

    if (
$success === false) {
      return 
null;
    }

    
#N#return $newPath;
  
}
    
  
### thumbnail ###
  
if($thumbPath != ''){
    
$thumbWidth 150;
    
$thumbHeight 100;
    if(
$width 150 || $height 100){
      
$calHeight round($thumbWidth*$height/$width);
      if(
$calHeight $thumbHeight){
        
$thumbWidth round($thumbHeight*$thumbWidth/$calHeight);
      }else{
        
$thumbHeight $calHeight;
      }
      
### ================ ###
      
$thumbImage imagecreatetruecolor($thumbWidth$thumbHeight);

      
//TRANSPARENT BACKGROUND
      
$color imagecolorallocatealpha($thumbImage000127); //fill transparent back
      
imagefill($thumbImage00$color);
      
imagesavealpha($thumbImagetrue);

      
//ROUTINE
      
imagecopyresampled($thumbImage$image0000$thumbWidth$thumbHeight$width$height);

      
// Rotate image on iOS
      
$exif = @exif_read_data($imagePath'IFD0');
      if (isset(
$exif['Orientation']) && isset($exif['Make']) && !empty($exif['Orientation']) && preg_match('/(apple|ios|iphone)/i'$exif['Make'])) {
        switch (
$exif['Orientation']) {
          case 
8:
            if (
$width $height)
              
$thumbImage imagerotate($thumbImage900);
            break;
          case 
3:
            
$thumbImage imagerotate($thumbImage1800);
            break;
          case 
6:
            
$thumbImage imagerotate($thumbImage, -900);
            break;
        }
      }

      switch (
true) {
        case 
in_array($outExt, ['jpg''jpeg']) === true$success imagejpeg($thumbImage$thumbPath);
          break;
        case 
$outExt === 'png'$success imagepng($thumbImage$thumbPath);
          break;
        case 
$outExt === 'gif'$success imagegif($thumbImage$thumbPath);
          break;
      }
      
      if (
$success === false) {
        return 
null;
      }
    }
  }
  
//  return null;
}

function 
upload_file_to_server2($dlink$file){
  
$temp_name $file['tmp_name'];
  if (
is_uploaded_file($file['tmp_name'])) {
    if (!
move_uploaded_file($temp_name$dlink)) {
      
    }
  }
}
/*function upload_file_to_server2($_dlink, $file, $_thumbPath = '', $width = '800', $height = '600') {
  $arr = explode('/', $_dlink);
  $file_name = end($arr);
  $file_type = get_file_type($file_name);
  $filename = str_replace($file_type, '', $file_name);
  $imp_dlink = implode('/', $arr);
  $dlink = str_replace($file_name, '', $imp_dlink);
  $thumbPath = $_thumbPath != '' ? str_replace($file_name, '', $_thumbPath) : '';
    
  //include_once('class.upload.php');
  $hup = new upload($file);
  //chmod($output_dir, 0755);
  if($hup->uploaded){
      $hup->image_resize = true;
      $hup->image_y  = $height;
      $hup->image_x = $width;
      $hup->image_ratio = true;            
      $hup->jpeg_quality = 95;
      $hup->image_ratio_no_zoom_in = true;
      $hup->file_new_name_body = $filename;
      $hup->Process($dlink);
      if($hup->processed){

      }else{
          //echo 'failed';
      }
      if($thumbPath != ''){
          $hup->image_resize = true;
          $hup->image_y  = 100;
          $hup->image_x = 150;
          $hup->image_ratio = true;            
          $hup->jpeg_quality = 95;
          $hup->image_ratio_no_zoom_in = true;            
          $hup->file_new_name_body = $filename;            
          $hup->Process($thumbPath);
          if($hup->processed){

          }else{

          }
      }
  }
}*/


function allow_image_type() {
  return array(
      
'.gif''.jpg''.jpeg''.png'
  
);
}
function 
allow_document_type() {
  return array(
      
'.pdf''.doc''.docx''.xls''.xlsx' //, '.ppt', '.pptx'
  
);
}

function 
get_file_type($filename) {
  
$_filename strtolower($filename);
  
$_arr explode('.'$_filename);
  
$filetype '.' end($_arr);

  return 
$filetype;
}

// ################## ชนิดของไฟล์เสี่ยงต่อความปลอดภัยของระบบ ###############
function check_file($userfilename$check_type ''){
  
$arr explode('.'$userfilename);
  
$strings strtolower('.'.end($arr));
  if(
$strings == '.php' || $strings == '.asp'){
    echo 
"<center><br>ไม่อนุญาตให้ Upload ไฟล์ชนิดนี้ <br>เนื่องจากชนิดไฟล์เสี่ยงต่อความเสียหายของระบบ <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
    exit;
  }else{
    if(
$check_type == 'image'){
      if(
in_array($stringsallow_image_type())){
        return 
'OK';
      }else{
//        return 'no_image';
        
echo"<center><br>ชนิดไฟล์ไม่ถูกต้อง <br>ไฟล์ต้องมีนามสกุลเป็น .gif หรือ .jpg หรือ .png เท่านั้น <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
        echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
        exit;
      }
    }else if(
$check_type == 'document'){
      if(
in_array($stringsallow_document_type())){
        return 
'OK';
      }else{
//        return 'no_document';
        
echo "<center><br>ชนิดไฟล์ไม่ถูกต้อง <br>ไฟล์ต้องมีนามสกุลเป็น .pdf, .doc, .docx, .xls, .xlsx เท่านั้น <br><br>";
        echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
        exit;
      }
    }else if(
$check_type == 'banner'){
      if(
in_array($stringsallow_image_type()) || $strings == '.swf'){
        return 
'OK';
      }else{
//        return 'no_image';
        
echo"<center><br>ชนิดไฟล์ไม่ถูกต้อง <br>ไฟล์ต้องมีนามสกุลเป็น .gif หรือ .jpg หรือ .png หรือ .swf เท่านั้น <br><br>";
        echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
        exit;
      }
//    }else if($check_type == ''){
    
}else{
      if(
$check_type != ''){
        if(
in_array($stringsallow_image_type())){
          return 
'OK';
        }else{
          echo
"<center><br>ไฟล์ที่ $check_type ชนิดไฟล์ไม่ถูกต้อง <br>ไฟล์ต้องมีนามสกุลเป็น .gif หรือ .jpg หรือ .png เท่านั้น <br>และขนาดไฟล์ต้องไม่เกิน 2 Mb.<br><br>";
          echo
"<a href='javascript:history.back()'><font color='ff0000'>[แก้ไขใหม่]</font></center></a><br><br>";
          exit;
        }
      }else{
        return 
'OK';
      }
    }
  }
}

// ############## สร้างชื่อไฟล์ใหม่
function create_filename($userfilename){
        
$strings get_file_type($userfilename);
        
$str  "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        
$pic1 substr(str_shuffle($str), 012);
        
$name1="$pic1$strings";
        return (
$name1);
}    

?>