uploadimage.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. $data = $_POST["datavars"];
  3. $dataarray = explode("###", $data);
  4. $user = $dataarray[0];
  5. $avatar = $dataarray[1];
  6. $imagename = $_FILES["image"]["name"];
  7. $size = $_FILES["image"]["size"];
  8. $tempname = $_FILES["image"]["tmp_name"];
  9. $type = $_FILES["image"]["type"];
  10. $endtemp = explode(".", $_FILES["image"]["name"]);
  11. $ending = end($endtemp);
  12. // unique image filename
  13. $unique = md5($imagename . $tempname . time());
  14. $filename = $unique . "." . $ending;
  15. // thumbnail
  16. $thumbname = $unique . "t" . "." . $ending;
  17. // path
  18. $uploaddir = "../uploads";
  19. $uploaddirexists = false;
  20. if( !is_dir($uploaddir) ) // check if upload directory exists
  21. {
  22. if( mkdir($uploaddir, 0705, true) ) // create upload directory
  23. {
  24. $uploaddirexists = true;
  25. }
  26. }
  27. else
  28. {
  29. $uploaddirexists = true;
  30. }
  31. $path = "../uploads/" . $filename;
  32. $thumbpath = "../uploads/" . $thumbname;
  33. // upload
  34. if( strlen($user) > 0 && strlen($imagename) > 0 && $size > 0 && $uploaddirexists )
  35. {
  36. if( ($type == "image/gif") || ($type == "image/jpeg") || ($type == "image/jpg") || ($type == "image/png") )
  37. {
  38. if( $size < 5000000 )
  39. {
  40. if( $_FILES["image"]["error"] == 0 )
  41. {
  42. if( !file_exists($path) )
  43. {
  44. if( copy($tempname, $path) ) // upload image
  45. {
  46. // thumbnail
  47. $sizedata = getimagesize($tempname);
  48. if( $type == "image/gif" )
  49. {
  50. $imagetoupload = @imagecreatefromgif($tempname);
  51. }
  52. elseif( $type == "image/jpeg" || $type == "image/jpg" )
  53. {
  54. $imagetoupload = @imagecreatefromjpeg($tempname);
  55. }
  56. elseif( $type == "image/png" )
  57. {
  58. $imagetoupload = @imagecreatefrompng($tempname);
  59. }
  60. if( $imagetoupload ) // imagecreatefromX
  61. {
  62. $width = imagesx($imagetoupload);
  63. $height = imagesy($imagetoupload);
  64. $div = $width / $height;
  65. $newwidth = 150;
  66. $newheight = 150 / $div;
  67. $newimage = @imageCreateTrueColor($newwidth, $newheight);
  68. if( $newimage ) // imagecreatetruecolor
  69. {
  70. // upload thumbnail
  71. $imagecopy = @imagecopyresized($newimage, $imagetoupload, 0, 0, 0, 0,
  72. $newwidth, $newheight, $sizedata[0], $sizedata[1]);
  73. if( $imagecopy ) // imagecopyresized
  74. {
  75. if( $type == "image/gif" )
  76. {
  77. $img = @imagegif($newimage, $thumbpath);
  78. }
  79. elseif( $type == "image/jpeg" || $type1 == "image/jpg" )
  80. {
  81. $img = @imagejpeg($newimage, $thumbpath);
  82. }
  83. elseif( $type == "image/png" )
  84. {
  85. $img = @imagepng($newimage, $thumbpath);
  86. }
  87. if( $img ) // imageX
  88. {
  89. @imagedestroy($newimage);
  90. // db entry
  91. include("connect.php");
  92. $timestamp = time();
  93. $message = "specialcharimg" . $thumbname;
  94. if( !$db->exec("INSERT INTO chatpack_log (timestamp, user, avatar, message)
  95. VALUES ('$timestamp', '$user', '$avatar', '$message')") )
  96. {
  97. cleanup($path, $thumbpath, $filename); // clean up on error
  98. }
  99. $db->close();
  100. }
  101. }
  102. }
  103. }
  104. }
  105. else // error upload
  106. {
  107. cleanup($path, $thumbpath, $filename);
  108. }
  109. }
  110. else // error exists
  111. {
  112. cleanup($path, $thumbpath, $filename);
  113. }
  114. }
  115. }
  116. else // error size
  117. {
  118. cleanup($path, $thumbpath, $filename);
  119. }
  120. }
  121. else // error type
  122. {
  123. cleanup($path, $thumbpath, $filename);
  124. }
  125. }
  126. function cleanup($path, $thumbpath, $filename)
  127. {
  128. // delete image
  129. if( file_exists($path) )
  130. {
  131. unlink($path);
  132. }
  133. // delete thumbnail
  134. if( file_exists($thumbpath) )
  135. {
  136. unlink($thumbpath);
  137. }
  138. // delete db entry
  139. include("connect.php");
  140. $message = "specialcharimg" . $thumbname;
  141. $db->exec("DELETE FROM chatpack_log WHERE message='$message'");
  142. $db->close();
  143. }
  144. ?>