Created
October 18, 2012 15:12
-
-
Save kane-thornwyrd/3912469 to your computer and use it in GitHub Desktop.
How to Inserting a photo into a vCard 2.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Public Domain | |
Original by Twen from #Drupalfr on IRC Freenode. | |
*/ | |
/* Inserting a photo into a vCard 2.1 */ | |
if ( ($contact->photo != '') && (file_exists($image_path)) ) { | |
// Identifies the file format | |
// > identify 'myfile' | |
// myfile JPEG 78x100 78x100+0+0 DirectClass 2.3kb | |
// > identify %m 'myfile' | |
// JPEG | |
exec('identify -format %m '.$image_path, $out); // $out will be an array | |
// Reads the file to encode it. | |
$enc = encode_photo($image_path); | |
// tricky bit: Builds one full line for the vcard: vcard entry + base64 encoded file. | |
// we need to split this unique line into 76 characters long. | |
// BUT except the first line, all other have to be <space>+75 characters ! | |
// Builds the lone line. | |
// note we insert 'HOTO' instead of PHOTO, because the following foreach loop | |
// will add a space even on the 1st entry of the array | |
$encArray = str_split('HOTO;TYPE='.$out[0] .';ENCODING=b:'.$enc, 75); | |
// Adds the extra space needed in for of each line/item | |
foreach ($encArray as $l => $line) { | |
$encArray[$l] = ' '.$encArray[$l]; | |
} | |
$encArray[0] = str_replace( ' HOTO', 'PHOTO', $encArray[0]); | |
// recopmpose the lone line | |
$field = implode("\r\n", $encArray); | |
// Adds the line to the class | |
$vCard->setPhoto( $field ); | |
} | |
function encode_photo($image_path) { | |
if (file_exists($image_path) && is_readable ($image_path)) { | |
$file = file_get_contents($image_path); | |
} | |
return base64_encode($file); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment