$ dom-> saveHTML()切断了html代码

I'm transfering some htmls to other server. What I do is, I take original html, I do some parsing to get rid of head and stuff.

libxml_use_internal_errors(true);
$full_dom->loadHTML($html);
libxml_use_internal_errors(false);

$full_dom = $full_dom->getElementsByTagName('body')->item(0);
 foreach($full_dom->childNodes as $child_node) {
    $body->appendChild($body->importNode($child_node, true));
}

This code is not a problem. In $body now I have clean HTML, which is good.

I need to base46 encode images in original html, for each node, that has node img, I get the file, that original src is pointing to

$picture = file_get_contents($item_path);

and I base64 encode it

$picture_data = base64_encode($picture);

If I dump the $picture_data, I get correct base64 encoded string. I then overwrite node with this src attribute

$node->setAttribute("src", "data:image/jpeg;base64,".$picture_data);

And if I var_dump $node->getAttribute("src"), this node's src attribute is correctly replaced with looooong string. Which is good.

Here comes the problem:

$html = $body->saveHTML();

var_dumping $html shows, that last base64 encoded picture's string is cut off at the same position every time. This last picture is over 1MB in size..

This is, for example, my html that doesnt work:

<body>
    <img src="/content_storage/storage/389/Jellyfish.jpg" alt="" width="133" height="100" /><br /><br /><br />Link do<a href="http://www.subjectx.net"> zunanje vsebine</a><br /><br /><img src="/content_storage/storage/656/WallpaperFusion-lac-de-savine-3840x1200-W.jpg" alt="" width="3840" height="1200" />
</body>

BUT! if I add another element at the end like this:

<body>
    <img src="/content_storage/storage/389/Jellyfish.jpg" alt="" width="133" height="100" /><br /><br /><br />Link do<a href="http://www.subjectx.net"> zunanje vsebine</a><br /><br /><img src="/content_storage/storage/656/WallpaperFusion-lac-de-savine-3840x1200-W.jpg" alt="" width="3840" height="1200" /><a href="#">aaa</a>
</body>

it starts working as intended and base64 encoded picture's string is terminated with //Z, this html string is transfered to other server and pictures shows on that server. Also works, if I change the order of this two pictures, putting smaller one at the place of large one and large one at the start of body..

What am I missing?

EDIT1: I have tried to phpfiddle it, but with larger image base64_encoded, source code is too large (limit is 250k). I used same, smaller, picture twice and this error doesnt happen.

http://phpfiddle.org/main/code/x3gc-4rp7

So I guess it has something to do with picture size/length..

EDIT2: Adding chunk_split() around base64_encode() doesnt help either.

EDIT3: I have var_dumped whole node, not just "src" attribute and node itself is correctly formed, base64 string is whole.

I had the same issue and I found an alternative for saveHTML(). You can use this:

$full_dom->documentElement->c14n();

You'll get the full HTML back without it being cut off. However c14n() is going to canonicalize nodes to a string.