Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何邮寄使用php创建的图像_Php_Email_Php Gd - Fatal编程技术网

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

如何邮寄使用php创建的图像,php,email,php-gd,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 = $

下面的代码将名字和姓氏添加到预定义的图像中

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函数时,将成功创建图像。但我的问题是,与其将图像打印到浏览器上,不如如何邮寄它。
我应该暂时保存图像直到邮件发送,然后删除它,还是有其他方法

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

我认为应该使用输出缓冲,将图像编码为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">

你可以。大多数浏览器都可以解释这一点。此外,用户不必在其电子邮件/网络邮件客户端中单击查看图像。您使用了
imagejpeg($image)
,输出使用
数据:image/gif