如何邮寄使用php创建的图像

时间:2015-03-04 12:30:41

标签: php email php-gd

我有以下代码,将名字和姓氏添加到预定义的图像上。

function create_image($fname, $lname)
{
 $image = imagecreatefromjpeg('sample.jpg');
 $color = imagecolorallocate($image, 255, 255, 255);
 $font_path = '../fonts/GSMT.TTF';
 $font_size = 18;
 $first_name = $name;
 $last_name = $lname;
 $name = $fname." ". $lname;

// Print Text On Image
imagettftext($image, $font_size, 0, 100, 160, $color, $font_path, $name);

header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}

我使用以下电子邮件脚本;

function mailer($email_adrress, $registration_id)
{
$mail = new PHPMailer;

$mail->isSMTP(); // set mailer to use SMTP
$mail->Host = 'mail.server.com'; // set mail server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = 'email@address.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->Port = 587; // TCP port to connect to
$mail->IsHTML(true);      
$mail->From = 'johndoe@gmail.com';
$mail->FromName = 'John Doe'; // from name     
$mail->addAddress($email_adrress); // recipient                            

$mail->Subject = 'emailer';
$mail->Body    = "Congratulations you have been Successfully registered.   

if(!$mail->send()) 
{
    die("error mail not sent);
} 
}

调用create_image函数时,图像会成功创建。但我的问题是不是将图像打印到浏览器上,而是如何邮寄它。 我应该暂时保存图像,直到发送邮件然后将其删除,还是有另一种方法?

2 个答案:

答案 0 :(得分:1)

我认为您应该使用输出缓冲并将图像编码为base64并在src标记

中使用它
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($image);
$contents = ob_get_contents();
ob_end_clean();

现在你需要base64内容

$base64 = base64_encode($contents);

现在您可以在消息HTML内容中使用它

<img src="data:image/jpeg;base64,$base64">

答案 1 :(得分:0)

是的,您需要保存,发送然后删除它,如您所说。直接用php来显示它是不可能的。但是,您可以使用iframe。但我认为第一个解决方案是最好的。