dongliaojing0554 2015-03-04 12:30
浏览 19
已采纳

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

I have the following code that adds the first name and last name onto a predefined image.

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);
}

I use the following email script;

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);
} 
}

When the create_image function is called the image gets created successfully. But my question is instead of printing the image onto the browser how do i mail it. Should i temporarily save the image till the mail is sent and then delete it, or is there another approach?

  • 写回答

2条回答 默认 最新

  • doulingqiu4349 2015-03-04 12:48
    关注

    I think you should use output buffering and encode the image to base64 and use it in src tag

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

    Now you need base64 content

    $base64 = base64_encode($contents);
    

    now you can use it in your message HTML contents

    <img src="data:image/jpeg;base64,$base64">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决