0

I am trying to create & save images from data in an email from the base64 data of an actual image that was in the html body that appears inline such as:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAE==">

But i'm trying to create them sequentially, as there could be more than one image tag in the html body, where the variable $html_part is the html body of the email. I just need some assistance in coming to a solution on what i'm doing wrong.

    $img_tags = preg_match('/<img\s+(.*)>/', $html_part, $num_img_tags);
    $num_img_tags = count($num_img_tags);

    echo $html_part;
    for ($i = 1; $i <= $num_img_tags; $i++) {       
    preg_match('/<img\s+(.*)\s+src="data:image\/(.*);(.*),(.*)"\s+(.*)>/i', $html_part, $srcMatch);
{   
    foreach($srcMatch[4] as $im_data)
    {
        $ufname = "image0".$num_img_tags.".jpg";
        echo "<h1>$ufname</h1>";
            $im_data = base64_decode($im_data);
            $im = imagecreatefromstring($im_data);
            if ($im !== false) {
            header('Content-Type: image/jpeg');
            imagejpeg($im, $ufname);
            imagedestroy($im);
            }
            else {
            echo 'An error occurred.';
            }
    }

}
}
4

2 回答 2

1

Pretty sure you want to use a preg_match_all, not a preg_match

http://php.net/manual/en/function.preg-match-all.php

Also, solution using the above.

<?php

$html_part=<<<END
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAVFBMVEWcZjTcViTMuqT8/vzcYjTkhhTkljT87tz03sRkZmS8mnT03tT89vTsvoTk1sz86uTkekzkjmzkwpT01rTsmnzsplTUwqz89uy0jmzsrmTknkT0zqT3X4fRAAAAbklEQVR4XnXOVw6FIBBAUafQsZfX9r/PB8JoTPT+QE4o01AtMoS8HkALcH8BGmGIAvaXLw0wCqxKz0Q9w1LBfFSiJBzljVerlbYhlBO4dZHM/F3llybncbIC6N+70Q7OlUm7DdO+gKs9gyRwdgd/LOcGXHzLN5gAAAAASUVORK5CYII=">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAVFBMVEWcZjTcViTMuqT8/vzcYjTkhhTkljT87tz03sRkZmS8mnT03tT89vTsvoTk1sz86uTkekzkjmzkwpT01rTsmnzsplTUwqz89uy0jmzsrmTknkT0zqT3X4fRAAAAbklEQVR4XnXOVw6FIBBAUafQsZfX9r/PB8JoTPT+QE4o01AtMoS8HkALcH8BGmGIAvaXLw0wCqxKz0Q9w1LBfFSiJBzljVerlbYhlBO4dZHM/F3llybncbIC6N+70Q7OlUm7DdO+gKs9gyRwdgd/LOcGXHzLN5gAAAAASUVORK5CYII=">
END;

preg_match_all('/<img.*?src="data:image\/.*;.*,(.*)".*?>/i', $html_part, $img_tags, PREG_PATTERN_ORDER);

echo $html_part;
$img_num = 0;
foreach($img_tags[1] as $im_data)
{
    $ufname = "image0".$img_num.".jpg";
    echo "<h1>$ufname</h1>";
    $im_data = base64_decode($im_data);
    $im = imagecreatefromstring($im_data);
    if ($im !== false) {
        imagejpeg($im, $ufname);
        imagedestroy($im);
    }
    else {
        echo 'An error occurred.';
    }
    $img_num++;
}
于 2012-04-09T00:24:04.760 回答
1

Your code is impossible - you cannot do a header() call after you've performed ANY output. You can also not output multiple images in the same 'document'. You also cannot output html (the <h1> stuff), images (header('Content-type:...'), etc... all within the same document.

As well, parsing/processing HTML with regexes is dangerous. A single malformation of the source document and your regexes will happily feast on garbage and produce garbage for you. Do not use regexes on html... every time you do, Alan Turing kills a kitten. Use a DOM parser instead.

于 2012-04-09T00:39:30.470 回答