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.';
}
}
}
}