Advertisement
urutehe1

image2matrix

Apr 8th, 2019
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.06 KB | None | 0 0
  1. <?php
  2.  
  3. $raw = "";
  4. for ($i=0; $i < $_POST['length']; $i++) {
  5.     $raw = $raw . $_POST['str' . $i];
  6. }
  7.  
  8. $exploded = explode(',', $raw);
  9. $encoded = $exploded[1];
  10. $decoded = base64_decode($encoded);
  11.  
  12. $image = imagecreatefromstring($decoded);
  13. $width = imagesx($image);
  14. $height = imagesy($image);
  15.  
  16. $r = [[]];
  17. $g = [[]];
  18. $b = [[]];
  19.  
  20. for ($y=0; $y < $height; $y++) {
  21.     for ($x=0; $x < $width; $x++) {
  22.         $rgb = imagecolorat($image, $x, $y);
  23.         $r[$x][$y] = ($rgb >> 16) & 0xFF;
  24.         $g[$x][$y] = ($rgb >> 8) & 0xFF;
  25.         $b[$x][$y] = $rgb & 0xFF;
  26.     }
  27. }
  28.  
  29. $img = imagecreatetruecolor($width, $height);
  30. for ($y=0; $y < $height; $y++) {
  31.     for ($x=0; $x < $width; $x++) {
  32.         imagesetpixel($img, $x, $y, imagecolorallocate($img, $r[$x][$y], $g[$x][$y], $b[$x][$y]));
  33.     }
  34. }
  35.  
  36. ob_start();
  37.     imagejpeg($img);
  38.     $contents = ob_get_contents();
  39. ob_end_clean();
  40.  
  41. $base64 = "data:image/jpeg;base64," . base64_encode($contents);
  42.  
  43. $result = [
  44.     'r' => $r,
  45.     'g' => $g,
  46.     'b' => $b,
  47.     'width' => $width,
  48.     'height' => $height,
  49.     'base64' => $base64
  50. ];
  51. echo json_encode($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement