Create E-Mail signatures with PHP

Create E-Mail signatures with PHP

This afternoons mini project “Create E-Mail signatures with PHP” for a client who wants their staff to have custom signatures but which would involve each person having an image created for them or them creating one for themselves.

A quick bit of PHP will sort that!

Upload this PHP script to your webserver along with arial.ttf, times.ttf and image.png of your signature with blank areas for details, a sample can be found here.

A live demo can be found here

To re-arrange the text edit the lines as below where 35 is the size, 0 is the rotation, 285 is the X pos and 50 is the Y pos

imagettftext($image, 35, 0, 285, 50, $name_text_Color, $times_fontPath, $name_text);

To change the colour of the text edit the lines below, where 101 is Red, 135 is Green and 122 is Blue. I use HTML Color Picker to choose my colours

$name_text_Color = imagecolorallocate($image, 101, 135, 122);

<HTML>
<HEAD>
</HEAD>

<BODY>
<?php
	//get some blank variables
	$name_text = $role_text = $landline_text = $email_text = $mobile_text = $website_text = $address_line1_text = $address_line2_text = "";
	
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$name_text = $_POST['name_text'];
	$role_text = $_POST['role_text'];
	$landline_text = $_POST['landline_text'];
	$email_text = $_POST['email_text'];
	$mobile_text = $_POST['mobile_text'];
	$website_text = $_POST['website_text'];
	$address_line1_text = $_POST['address_line1_text'];
	$address_line2_text = $_POST['address_line2_text'];
	
	//Background Image - The Image To Write Text On
	$image = imagecreatefrompng('image.png');
	
	//Color of Text
	$name_text_Color = imagecolorallocate($image, 101, 135, 122);
	$role_text_Color = imagecolorallocate($image, 101, 135, 122);
	$landline_text_Color = imagecolorallocate($image, 70, 100, 88);
	$email_text_Color = imagecolorallocate($image, 70, 100, 88);
	$mobile_text_Color = imagecolorallocate($image, 70, 100, 88);
	$website_text_Color = imagecolorallocate($image, 70, 100, 88);
	$address_line1_text_Color = imagecolorallocate($image, 70, 100, 88);
	$address_line2_text_Color = imagecolorallocate($image, 70, 100, 88);
	
	//Full Font-File Path
	$times_fontPath = 'times.ttf';
	$arial_fontPath = 'arial.ttf';
	
	//Function That Write Text, Image, size, rotation, x , y
	imagettftext($image, 35, 0, 285, 50, $name_text_Color, $times_fontPath, $name_text);
	imagettftext($image, 15, 0, 285, 80, $role_text_Color, $times_fontPath, $role_text);
	imagettftext($image, 10, 0, 315, 115, $landline_text_Color, $arial_fontPath, $landline_text);
	imagettftext($image, 10, 0, 480, 115, $email_text_Color, $arial_fontPath, $email_text);
	imagettftext($image, 10, 0, 315, 165, $mobile_text_Color, $arial_fontPath, $mobile_text);
	imagettftext($image, 10, 0, 480, 165, $website_text_Color, $arial_fontPath, $website_text);
	imagettftext($image, 10, 0, 315, 210, $address_line1_text_Color, $arial_fontPath, $address_line1_text);
	imagettftext($image, 10, 0, 315, 225, $address_line2_text_Color, $arial_fontPath, $address_line2_text);
	
	//Send Image To Browser
	ob_start();
	imagepng($image);
	$image = ob_get_contents();
	ob_end_clean();
	echo '<img src="data:image/png;base64,'.base64_encode($image).'" />';
	
    }
?>
	<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" autocomplete="off">
        <input type=text name=name_text value="<?php echo $name_text;?>" placeholder="Name" title="Name"></br>
		<input type=text name=role_text value="<?php echo $role_text;?>" placeholder="Role" title="Role"></br>
		<input type=text name=landline_text value="<?php echo $landline_text;?>" placeholder="Landline" title="Landline"></br>
		<input type=text name=email_text value="<?php echo $email_text;?>" placeholder="EMail" title="EMail"></br>
		<input type=text name=mobile_text value="<?php echo $mobile_text;?>" placeholder="Mobile" title="Mobile"></br>
		<input type=text name=website_text value="<?php echo $website_text;?>" placeholder="Website" title="Website"></br>
		<input type=text name=address_line1_text value="<?php echo $address_line1_text;?>" placeholder="Address line 1" title="Address line 1"></br>
		<input type=text name=address_line2_text value="<?php echo $address_line2_text;?>" placeholder="Address line 2" title="Address line 2"></br>
        <input type=submit value="Create Signature">
</form>
		
</BODY>
</HTML>

If you found this article helpful, would you consider buying me a Coffee?

Leave a Reply

Your email address will not be published. Required fields are marked *