如何使用base64编码像素数组?

时间:2018-07-24 14:18:49

标签: php image base64 encode

在第二列($ bytesArray)中,我解码了ras变量的代码; 但是我无法将第一个数组编码为ras变体($ byteArray)。 如何编码第一个数组? (对不起,我的英语不好) 运行代码段无法正常工作

为此(5 * 7透明)http://i.hizliresim.com/aY1Zq7.png

成像

<?php
$image_info = getimagesize($_FILES["nfile"]["tmp_name"]);
$arr=array();
$p = 4;
$image_width = $image_info[0];
$image_height = $image_info[1];
if ($_FILES) {
    $post_img = $_FILES['nfile'];
    $tmp_img = $post_img['tmp_name'];
    $img = imagecreatefrompng($tmp_img);
    list($width, $height) = getimagesize($tmp_img);
    for ($i = 0; $i < $height; $i++) {
        $y = $i; // Get Y coords
        for ($j = 0; $j < $width; $j++) {
            $x = $j; // Get X coords
            $rgb = imagecolorat($img, $j, $i); // Get pixel color
            $rgba = imagecolorsforindex($img, $rgb);
            $ad[]=$rgba;
        }
    }
}
$h=0;
for ($t=0;$t<$image_width*$image_height;$t++){
    $xdix[$h]= $ad[$t]['red'];
    $xdix[$h+1]= $ad[$t]['green'];
    $xdix[$h+2]= $ad[$t]['blue'];
    $xdix[$h+3]= $ad[$t]['alpha'];
    $h=$h+4;
}
$byteArray = array();
$x=0;
foreach ($xdix as $byte) {
    if($byte==255) $byteArray[] =  0;
    elseif ($byte==0){
        if ($x==3){
            $byteArray[] = 255;$x=0;
        }
        else {$x++;
            $byteArray[] = 0;}
    }
    elseif ($byte==127) $byteArray[] =  0;
}
print_r($byteArray);
 



 
$ras="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAD/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAP8AAAD/AAAAAAAAAAAAAAAAAAAA/wAAAP8AAAAAAAAAAAAAAAAAAAD/AAAAAAAAAP8AAAD/AAAA/wAAAAA=";

$bytesArray = array();
foreach (str_split(base64_decode($ras)) as $byte) {
    $bytesArray[] = ord($byte);
}
print_r($bytesArray);


?>
<html>
<head></head>
<body>
<table class="table table-striped flex-row flex-nowrap">


    <form method="post" id="formx" action="upload.php" enctype="multipart/form-data">
        <tr class="printertr">

            <td >


                <input type='file' accept=".png" id="nfile" name="nfile" value="Browse" onchange="readURL(this);" />

            </td>
            <td>

                <img id="blah" src="#"  />
                <script>
                    function readURL(input) {
                        if (input.files && input.files[0]) {
                            var reader = new FileReader();

                            reader.onload = function (e) {
                                $('#blah')
                                    .attr('src', e.target.result)
                                    .width(50)
                                    .height(50);
                            };

                            reader.readAsDataURL(input.files[0]);
                        }
                    }
                </script>
            </td>
            <td>
                <button class="logopic2" type="submit">Go</button>
            </td>

        </tr>

    </form>
    </tbody>
</table>
</body>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
</html>

0 个答案:

没有答案