How to insert image to vcard using PHP

Using PHP you can generate VCF-files, which contain contact data, suitable for Outlook. This article briefly discusses the insertion of image (avatar) to this file.

// Insertion avatar to vcard
// $vCard - variable, containing the body of vcard
$vCard  = 'BEGIN:VCARD' . "\r\n";
$vCard .= 'VERSION:2.1' . "\r\n";

$image = "path_to_jpeg_image";
if (file_exists($image)) {
  $photo = base64_encode(file_get_contents($image));
  // we should split this large string into smaller 72-symbols strings, to correspond the standard
  $photo = wordwrap($photo, 72, "\r\n ", true);

  $vCard .= 'PHOTO;TYPE=JPEG;ENCODING=BASE64:' . "\r\n " . $photo . "\r\n\r\n"; // here we need two line feeds

$vCard .= 'END:VCARD' . "\r\n";

Generated in such a way VCF will contain a built-in image. That is, the file can be transferred from one computer to another without a fear that the image will not be displayed. Note, that in this example we have used JPEG, however the other image types can be used too.

Links: – vCard specification

Leave a Reply

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