PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/env/html5_configurator/color.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 361 lines | 193 code | 92 blank | 76 comment | 45 complexity | 49b836e9e7a7fb9e3ae1e406e6fbe4b2 MD5 | raw file
  1. <?php
  2. foreach($_REQUEST as $key => $value){
  3. $$key = htmlspecialchars(trim(strip_tags($value)));
  4. }
  5. // echo "<pre>";
  6. // print_r($_POST);
  7. // echo "</pre>";
  8. // die();
  9. // $image = "/env/configurator/files/clipArts/Americana/amr12.png";
  10. // $red = 255; $green = 70; $blue = 68;
  11. $temp = explode("?", $image);
  12. $image = $temp[0];
  13. $query = $temp[1];
  14. // print_r($temp);
  15. // $query = rand(1000000, 9999999);
  16. $imgdir = "/var/www/mch".$image;
  17. $imgtype = exif_imagetype($imgdir);
  18. $date = date('Y_m_d_H_i_s');
  19. $ext = trim(pathinfo($imgdir, PATHINFO_EXTENSION));
  20. // echo $ext;
  21. // die();
  22. if($ext != 'png' &&
  23. $ext != 'jpeg' &&
  24. $ext != 'jpg' &&
  25. $ext != 'gif' &&
  26. $ext != 'eps' &&
  27. $ext != 'tiff' &&
  28. $ext != 'bmp'){
  29. $data = array(
  30. 'error' => 'This is the wrong type of file, Please upload a file that has the correct file types.'
  31. );
  32. //echo $ext;
  33. //echo json_encode($data);
  34. echo 'wrong file type';
  35. // sleep(3);
  36. // header("Location:$_SERVER[HTTP_REFERER]");
  37. die();
  38. }
  39. // echo $ext;
  40. function replace_extension($filename, $new_extension) {
  41. $info = pathinfo($filename);
  42. return $info['dirname']
  43. . DIRECTORY_SEPARATOR
  44. . $info['filename']
  45. . '.'
  46. . $new_extension;
  47. }
  48. // transparent($imgdir);
  49. // $blob = base64_decode(convert($tempimg_path, $red, $green, $blue));
  50. // $layout->readImageBlob($blob);
  51. // $layout->setImageFormat('png');
  52. if($ext === 'png' || $imgtype === 3){
  53. $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  54. imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  55. $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  56. $imgdir = $tempimg_path;
  57. imagedestroy($im);
  58. // echo $imgdir;
  59. $im = imagecreatefrompng($imgdir);
  60. // $w = 0; $h = 0; $image_path = array();
  61. // $path_info = getimagesize($imgdir);
  62. // $w = $path_info[0]; $h = $path_info[1];
  63. // $blank = imagecreatetruecolor($w, $h);
  64. // $bg = imagecolorallocate($blank, $red, $green, $blue);
  65. // imagefilledrectangle($blank, 0, 0, $w, $h, $bg);
  66. // imagecopymerge($im, $blank, 0, 0, 0, 0, $w, $h, 40);
  67. // imagecolortransparent($im, $bg);
  68. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  69. ob_start();
  70. imagepng($im);
  71. $changed_file = base64_encode(ob_get_contents());
  72. ob_end_clean();
  73. imagepng($im, $imgdir."?".$query);
  74. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  75. imagedestroy($im);
  76. // imagedestroy($blank);
  77. echo $base64;
  78. // echo "<img src='$base64'><br>";
  79. // echo $imgtype;
  80. // echo "<img src=".$base64.">";
  81. //imagefilter($bm, IMG_FILTER_COLORIZE, $red, $green, $blue);
  82. // imagepng($bm, $imgdir."?".$query);
  83. // $color = imagecolorallocate($bm, $red, $green, $blue);
  84. //imagedestroy($bm);
  85. // $imgdir = replace_extension($imgdir, 'png');
  86. // echo $imgdir;
  87. // imagefill($im, 0, 0, $color);
  88. // header('Content-Type: image/png');
  89. // imagepng($im);
  90. // imagepng($handle, $imgdir."?".$query);
  91. } else if($ext === 'jpeg' || $ext === 'jpg' || $imgtype === 2){
  92. $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  93. imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  94. $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  95. $imgdir = $tempimg_path;
  96. imagedestroy($im);
  97. $im = imagecreatefrompng($imgdir);
  98. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  99. ob_start();
  100. imagepng($im);
  101. $changed_file = base64_encode(ob_get_contents());
  102. ob_end_clean();
  103. imagepng($im, $imgdir."?".$query);
  104. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  105. imagedestroy($im);
  106. echo $base64;
  107. // echo $imgtype;
  108. } else if ($ext === 'gif' || $imgtype === 1){
  109. // $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  110. // imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  111. // $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  112. // $imgdir = $tempimg_path;
  113. // imagedestroy($im);
  114. $im = imagecreatefromgif($imgdir);
  115. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  116. ob_start();
  117. imagepng($im);
  118. $changed_file = base64_encode(ob_get_contents());
  119. ob_end_clean();
  120. imagepng($im, $imgdir."?".$query);
  121. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  122. imagedestroy($im);
  123. echo $base64;
  124. //echo "<img src='$base64'><br>";
  125. //echo $imgtype;
  126. } else if($ext === 'tiff' || $imgtype === 7 || $imgtype === 8){
  127. $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  128. imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  129. $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  130. $imgdir = $tempimg_path;
  131. imagedestroy($im);
  132. $im = new Imagick();
  133. $im->setResolution(100, 100);
  134. $im->readImage($imgdir);
  135. $im->setImageFormat("png");
  136. $extln = strlen($ext);
  137. $dir = substr($imgdir, 0, strlen($imgdir)-$extln);
  138. $imgdir = $dir."png";
  139. // echo $imgdir;
  140. // die();
  141. $im->writeImages($imgdir, true);
  142. $im->destroy();
  143. $im = imagecreatefrompng($imgdir);
  144. // $white = imagecolorallocate($bm, 255, 255, 255);
  145. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  146. // imagecolortransparent($bm, $white);
  147. ob_start();
  148. imagepng($im);
  149. $changed_file = base64_encode(ob_get_contents());
  150. ob_end_clean();
  151. imagepng($im, $imgdir."?".$query);
  152. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  153. imagedestroy($im);
  154. echo $base64;
  155. // echo $imgtype;
  156. }else if($ext === 'bmp' || $imgtype === 6){
  157. $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  158. imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  159. $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  160. $imgdir = $tempimg_path;
  161. imagedestroy($im);
  162. $im = new Imagick();
  163. $im->setResolution(100, 100);
  164. $im->readImage($imgdir);
  165. $im->setImageFormat("png");
  166. $extln = strlen($ext);
  167. $dir = substr($imgdir, 0, strlen($imgdir)-$extln);
  168. $imgdir = $dir."png";
  169. // echo $imgdir;
  170. // die();
  171. $im->writeImages($imgdir, true);
  172. $im->destroy();
  173. $im = imagecreatefrompng($imgdir);
  174. // $white = imagecolorallocate($bm, 255, 255, 255);
  175. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  176. // imagecolortransparent($bm, $white);
  177. ob_start();
  178. imagepng($im);
  179. $changed_file = base64_encode(ob_get_contents());
  180. ob_end_clean();
  181. imagepng($im, $imgdir."?".$query);
  182. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  183. imagedestroy($im);
  184. echo $base64;
  185. // echo $imgtype;
  186. }else if ($ext === 'eps'){
  187. $im = imagecreatefromstring(base64_decode(transparent($imgdir, $imgtype)));
  188. imagepng($im, "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png");
  189. $tempimg_path = "/var/www/mch/env/html5_configurator/img/design/$post[sessionid]-$date-test_layer.png";
  190. $imgdir = $tempimg_path;
  191. imagedestroy($im);
  192. $im = new Imagick();
  193. $im->setResolution(100, 100);
  194. $im->readImage($imgdir);
  195. $im->setImageFormat("png");
  196. $extln = strlen($ext);
  197. $dir = substr($imgdir, 0, strlen($imgdir)-$extln);
  198. $imgdir = $dir."png";
  199. // echo $imgdir;
  200. // die();
  201. $im->writeImages($imgdir."?".$query, true);
  202. $im->destroy();
  203. $im = imagecreatefrompng($imgdir);
  204. imagefilter($im, IMG_FILTER_COLORIZE, $red, $green, $blue);
  205. ob_start();
  206. imagepng($im);
  207. $changed_file = base64_encode(ob_get_contents());
  208. ob_end_clean();
  209. imagepng($im, $imgdir."?".$query);
  210. $base64 = 'data:image/png' . ';base64,' . $changed_file;
  211. imagedestroy($im);
  212. echo $base64;
  213. // echo $imgtype;
  214. }
  215. // echo "<img src=".substr($imgdir,12, strlen($imgdir)-1)."?".$query.">";
  216. // echo substr($imgdir,12, strlen($imgdir)-1)."?".$query;
  217. function transparent($imgdir, $imgtype){
  218. list($width, $height) = getimagesize($imgdir);
  219. $new_width = $width;
  220. $new_height = $height;
  221. // Resample
  222. $image_p = imagecreatetruecolor($new_width, $new_height);
  223. $ext = trim(pathinfo($imgdir, PATHINFO_EXTENSION));
  224. if($ext === 'png' || $imgtype === 3){
  225. $im = imagecreatefrompng($imgdir);
  226. } else if($ext === 'jpeg' || $ext === 'jpg' || $imgtype === 2){
  227. $im = imagecreatefromjpeg($imgdir);
  228. } else if ($ext === 'gif' || $imgtype === 1){
  229. $im = imagecreatefromgif($imgdir);
  230. }
  231. $colors = array();
  232. //imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  233. if($im && imagefilter($im, IMG_FILTER_GRAYSCALE)) {
  234. //imagealphablending($image_p, true);
  235. $transparencyIndex = imagecolortransparent($im);
  236. if ($transparencyIndex >= 0) {
  237. } else {
  238. // Create an index for the color white
  239. $white = imagecolorallocate($im, 255, 255, 255);
  240. $black = imagecolorallocate($im, 0, 0, 0);
  241. // $brown = imagecolorallocate($im, 126, 64, 17);
  242. //Set each pixel that is lighter than 200,200,200 to white
  243. for($x = 0; $x < $new_width; $x++) {
  244. for($y = 0; $y < $new_height; $y++) {
  245. $rgb = imagecolorat($im,$x,$y);
  246. $r = ($rgb >> 16) & 0xFF;
  247. $g = ($rgb >> 8) & 0xFF;
  248. $b = $rgb & 0xFF;
  249. if($r > 150 && $g > 150 && $b > 150)
  250. imagesetpixel($im, $x,$y, $white);
  251. else
  252. imagesetpixel($im, $x,$y, $black);
  253. $stringData = 'Red: '.$r.' -- Green: '.$g.' -- Blue: '.$b.' \n';
  254. }
  255. }
  256. // Make the background transparent
  257. // TRUE COLOR
  258. //$white = imagecolorallocatealpha($image_p, 255, 255, 255, 0);
  259. // RGB
  260. imagecolortransparent($im, $white);
  261. }}
  262. ob_start();
  263. imagepng($im);
  264. $changed_file = base64_encode(ob_get_contents());
  265. ob_end_clean();
  266. // $base64 = 'data:image/png' . ';base64,' . $changed_file;
  267. imagedestroy($im);
  268. return $changed_file;
  269. }