Skip to content

Commit

Permalink
Merge pull request #5 from jeroendesloovere/FredoVelcro-patch-1
Browse files Browse the repository at this point in the history
Adding Picture to VCard
  • Loading branch information
jeroendesloovere committed Nov 25, 2014
2 parents 9d7c836 + 52e6ec2 commit cc999f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Binary file added examples/landscape.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions src/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit cc999f0

Please sign in to comment.