/public/chocolates/upload.php

https://gitlab.com/vince.omega/mcb-nov-build · PHP · 111 lines · 46 code · 28 blank · 37 comment · 14 complexity · a45c93c4cf31c088e2dcbcff06434406 MD5 · raw file

  1. <?php
  2. //create the directory if doesn't exists (should have write permissons)
  3. if(!is_dir("./files")) mkdir("./files", 0755);
  4. //move the uploaded file
  5. $now = time();
  6. /*
  7. $myFile = "./files/testFile.txt";
  8. $fh = fopen($myFile, 'w') or die("can't open file");
  9. $stringData = "HERE IS YOUR PIXEL DATA\n";
  10. fwrite($fh, $stringData);
  11. $stringData = "-----------------------\n";
  12. fwrite($fh, $stringData);
  13. */
  14. list($width, $height) = getimagesize($_FILES['Filedata']['tmp_name']);
  15. $new_width = $width;
  16. $new_height = $height;
  17. // Resample
  18. $image_p = imagecreatetruecolor($new_width, $new_height);
  19. if(strpos($_FILES['Filedata']['name'], '.jpg') > 0)
  20. $image = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
  21. elseif(strpos($_FILES['Filedata']['name'], '.gif') > 0)
  22. $image = imagecreatefromgif($_FILES['Filedata']['tmp_name']);
  23. elseif(strpos($_FILES['Filedata']['name'], '.png') > 0)
  24. $image = imagecreatefrompng($_FILES['Filedata']['tmp_name']);
  25. $colors = array();
  26. //imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  27. if($image && imagefilter($image, IMG_FILTER_GRAYSCALE)) {
  28. //imagealphablending($image_p, true);
  29. $transparencyIndex = imagecolortransparent($image);
  30. if ($transparencyIndex >= 0) {
  31. } else {
  32. // Create an index for the color white
  33. $white = imagecolorallocate($image, 255, 255, 255);
  34. $black = imagecolorallocate($image, 0, 0, 0);
  35. /*
  36. while(count($colors) <= 50) {
  37. $randColor = rand(0,255);
  38. if(array_key_exists($randColor, $colors)) {
  39. continue;
  40. } else {
  41. array_push($colors, $randColor);
  42. }
  43. }*/
  44. //Set each pixel that is lighter than 200,200,200 to white
  45. for($x = 0; $x < $new_width; $x++) {
  46. for($y = 0; $y < $new_height; $y++) {
  47. $rgb = imagecolorat($image,$x,$y);
  48. $r = ($rgb >> 16) & 0xFF;
  49. $g = ($rgb >> 8) & 0xFF;
  50. $b = $rgb & 0xFF;
  51. if($r > 150 && $g > 150 && $b > 150)
  52. imagesetpixel($image, $x,$y, $white);
  53. else
  54. imagesetpixel($image, $x,$y, $black);
  55. $stringData = 'Red: '.$r.' -- Green: '.$g.' -- Blue: '.$b.' \n';
  56. //fwrite($fh, $stringData);
  57. }
  58. }
  59. // Make the background transparent
  60. // TRUE COLOR
  61. //$white = imagecolorallocatealpha($image_p, 255, 255, 255, 0);
  62. // RGB
  63. imagecolortransparent($image, $white);
  64. }
  65. //for($h = 1; $h < 55; $h++) {
  66. // $greyVal = 255 - $h;
  67. // $color = imagecolorallocate($image, $greyVal, $greyVal, $greyVal);
  68. // imagecolortransparent($image, $color);
  69. //}
  70. if(strrpos($_FILES['Filedata']['name'], ".jpeg"))
  71. $_FILES['Filedata']['name'] = substr($_FILES['Filedata']['name'], 0, -5).".png";
  72. else
  73. $_FILES['Filedata']['name'] = substr($_FILES['Filedata']['name'], 0, -4).".png";
  74. imagepng($image, "./files/".$_FILES['Filedata']['name'], 1);
  75. } else {
  76. $_FILES['Filedata']['name'] = substr($_FILES['Filedata']['name'], 0, -4).".png";
  77. move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/".$_FILES['Filedata']['name']);
  78. }
  79. //fclose($fh);
  80. chmod("./files/".$_FILES['Filedata']['name'], 0777);
  81. ?>