diff --git a/examples/example.php b/examples/example.php index 8adfaef..4455c90 100644 --- a/examples/example.php +++ b/examples/example.php @@ -29,6 +29,11 @@ $vcard->addAddress(null, null, 'street', 'worktown', null, 'workpostcode', 'Belgium'); $vcard->addURL('http://www.siesqo.be'); +$vcard->addPhoto(__DIR__ . '/landscape.jpeg', true); + +// return vcard as a string +//return $vcard->get(); + // return vcard as a download return $vcard->download(); diff --git a/examples/landscape.jpeg b/examples/landscape.jpeg new file mode 100644 index 0000000..a24998a Binary files /dev/null and b/examples/landscape.jpeg differ diff --git a/src/VCard.php b/src/VCard.php index 856a968..2a4254f 100644 --- a/src/VCard.php +++ b/src/VCard.php @@ -100,6 +100,30 @@ public function addJobtitle($jobtitle) $this->setProperty('TITLE', $jobtitle); } + /** + * Add a photo or logo (depending on property name) + * + * @return void + * @param string $property LOGO|PHOTO + * @param string $url image url or filename + * @param bool $encode to integrate / encode or not the file + */ + private function addMedia($property, $url, $encode = false) + { + if ($encode) { + $value = file_get_contents($url); + + // todo better MIME detection + $mime = mime_content_type($url); + $value = base64_encode($value); + $property .= ";ENCODING=b;TYPE=" . strtoupper(str_replace('image/', '', $mime)); + } else { + $value = $url; + } + + $this->setProperty($property, $value); + } + /** * Add name * @@ -153,6 +177,18 @@ public function addPhoneNumber($number, $type = '') $this->setProperty('TEL' . (($type != '') ? ';' . $type : ''), $number); } + /** + * Add Photo + * + * @return void + * @param string $url image url or filename + * @param bool $encode to integrate / encode or not the file + */ + public function addPhoto($url, $encode = false) + { + $this->addMedia('PHOTO', $url, $encode); + } + /** * Add URL *