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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?