Advertisement
urutehe1

Grayscale

Apr 8th, 2019
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. $r = json_decode($_POST['r'], TRUE);
  4. $g = json_decode($_POST['g'], TRUE);
  5. $b = json_decode($_POST['b'], TRUE);
  6. $width = $_POST['width'];
  7. $height = $_POST['height'];
  8.  
  9. $abu = [[]];
  10.  
  11. for ($y=0; $y < $height; $y++) {
  12.     for ($x=0; $x < $width; $x++) {
  13.         $abu[$x][$y] = round(($r[$x][$y] + $g[$x][$y] + $b[$x][$y]) / (float) 3);
  14.     }
  15. }
  16.  
  17. $img = imagecreatetruecolor($width, $height);
  18. for ($y=0; $y < $height; $y++) {
  19.     for ($x=0; $x < $width; $x++) {
  20.         imagesetpixel($img, $x, $y, imagecolorallocate($img, $abu[$x][$y], $abu[$x][$y], $abu[$x][$y]));
  21.     }
  22. }
  23.  
  24. ob_start();
  25.     imagejpeg($img);
  26.     $contents = ob_get_contents();
  27. ob_end_clean();
  28.  
  29. $base64 = "data:image/jpeg;base64," . base64_encode($contents);
  30.  
  31. $result = [
  32.     'r' => $abu,
  33.     'g' => $abu,
  34.     'b' => $abu,
  35.     'width' => $width,
  36.     'height' => $height,
  37.     'base64' => $base64
  38. ];
  39. echo json_encode($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement