/** * Return the image data stream * @return Image data */ public function render() { if (is_null($this->image)) { throw new \BadMethodCallException(__METHOD__ . ' Image must be set either in constructor or with setImage()'); } return $this->image->data(); }
/** * @brief reads jpegPhoto and set is as avatar if available * @param $uid string ownCloud user name * @param $dn string the user's LDAP DN * @return void */ private function updateAvatar($uid, $dn) { $hasLoggedIn = \OCP\Config::getUserValue($uid, 'user_ldap', 'firstLoginAccomplished', 0); $lastChecked = \OCP\Config::getUserValue($uid, 'user_ldap', 'lastJpegPhotoLookup', 0); if ($hasLoggedIn !== '1' || time() - intval($lastChecked) < 86400) { //update only once a day return; } $jpegPhoto = $this->access->readAttribute($dn, 'jpegPhoto'); \OCP\Config::setUserValue($uid, 'user_ldap', 'lastJpegPhotoLookup', time()); if (!$jpegPhoto || !is_array($jpegPhoto) || !isset($jpegPhoto[0])) { //not set, nothing left to do; return; } $image = new \OCP\Image(); $image->loadFromBase64(base64_encode($jpegPhoto[0])); if (!$image->valid()) { \OCP\Util::writeLog('user_ldap', 'jpegPhoto data invalid for ' . $dn, \OCP\Util::ERROR); return; } //make sure it is a square and not bigger than 128x128 $size = min(array($image->width(), $image->height(), 128)); if (!$image->centerCrop($size)) { \OCP\Util::writeLog('user_ldap', 'croping image for avatar failed for ' . $dn, \OCP\Util::ERROR); return; } if (!\OC\Files\Filesystem::$loaded) { \OC_Util::setupFS($uid); } $avatarManager = \OC::$server->getAvatarManager(); $avatar = $avatarManager->getAvatar($uid); $avatar->set($image); }
/** * Get a photo from the oC and crops it with the suplied geometry. * @NoAdminRequired * @NoCSRFRequired */ public function cropPhoto() { $params = $this->request->urlParams; $x = isset($this->request->post['x']) && $this->request->post['x'] ? $this->request->post['x'] : 0; $y = isset($this->request->post['y']) && $this->request->post['y'] ? $this->request->post['y'] : 0; $w = isset($this->request->post['w']) && $this->request->post['w'] ? $this->request->post['w'] : -1; $h = isset($this->request->post['h']) && $this->request->post['h'] ? $this->request->post['h'] : -1; $tmpkey = $params['key']; $maxSize = isset($this->request->post['maxSize']) ? $this->request->post['maxSize'] : 200; $app = new App($this->api->getUserId()); $addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); $response = new JSONResponse(); if (!$contact) { return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); } $data = $this->server->getCache()->get($tmpkey); if (!$data) { return $response->bailOut(App::$l10n->t('Image has been removed from cache')); } $image = new \OCP\Image(); if (!$image->loadFromData($data)) { return $response->bailOut(App::$l10n->t('Error creating temporary image')); } $w = $w !== -1 ? $w : $image->width(); $h = $h !== -1 ? $h : $image->height(); if (!$image->crop($x, $y, $w, $h)) { return $response->bailOut(App::$l10n->t('Error cropping image')); } if ($image->width() < $maxSize || $image->height() < $maxSize) { if (!$image->resize(200)) { return $response->bailOut(App::$l10n->t('Error resizing image')); } } // For vCard 3.0 the type must be e.g. JPEG or PNG // For version 4.0 the full mimetype should be used. // https://tools.ietf.org/html/rfc2426#section-3.1.4 if (strval($contact->VERSION) === '4.0') { $type = $image->mimeType(); } else { $type = explode('/', $image->mimeType()); $type = strtoupper(array_pop($type)); } if (isset($contact->PHOTO)) { $property = $contact->PHOTO; if (!$property) { $this->server->getCache()->remove($tmpkey); return $response->bailOut(App::$l10n->t('Error getting PHOTO property.')); } $property->setValue(strval($image)); $property->parameters = array(); $property->parameters[] = new \Sabre\VObject\Parameter('ENCODING', 'b'); $property->parameters[] = new \Sabre\VObject\Parameter('TYPE', $image->mimeType()); $contact->PHOTO = $property; } else { $contact->add('PHOTO', strval($image), array('ENCODING' => 'b', 'TYPE' => $type)); // TODO: Fix this hack $contact->setSaved(false); } if (!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact.')); } $thumbnail = Properties::cacheThumbnail($params['backend'], $params['addressBookId'], $params['contactId'], $image); $response->setData(array('status' => 'success', 'data' => array('id' => $params['contactId'], 'thumbnail' => $thumbnail))); $this->server->getCache()->remove($tmpkey); return $response; }
public static function renderOutput($addrbookId, $grpId = 'all') { if ($grpId === 'all') { $contacts_alphabet = VCard::all($addrbookId, null, null, array(), true); } elseif ($grpId === 'fav') { $contacts_alphabet = VCard::allByFavourite(); } else { $contacts_alphabet = VCard::getCardsByGroups($addrbookId, $grpId, null, null, true); } $addressBookPerm = Addressbook::find($addrbookId); //Favourites $favorites = \OC::$server->getTagManager()->load(self::$appname)->getFavorites(); $aFavourites = array(); if (is_array($favorites)) { foreach ($favorites as $fav) { $aFavourites[$fav] = 1; } } $contacts = array(); $counterAlle = 0; $aLetter = []; if ($contacts_alphabet) { $oldLetter = ''; foreach ($contacts_alphabet as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); $details = VCard::structureContact($vcard); $imgBuild = ''; if ($vcard->PHOTO) { $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgBuild = 'data:' . $image->mimeType() . ';base64,' . $imgSrc; } $sLetter = strtoupper(mb_substr($contact['sortFullname'], 0, 1, "UTF-8")); if ($sLetter !== $oldLetter) { $aLetter[] = $sLetter; } //\OCP\Util::writeLog(self::$appname,'LETTER: '. $sLetter.':'.$contact['fullname'], \OCP\Util::DEBUG); $contacts[$sLetter][] = array('id' => $contact['id'], 'aid' => $contact['addressbookid'], 'letter' => $sLetter, 'photo' => $imgBuild, 'component' => $contact['component'], 'fullname' => $contact['fullname'], 'surename' => $contact['surename'], 'lastname' => $contact['lastname'], 'organization' => $contact['organization'], 'bcompany' => $contact['bcompany'], 'data' => $details); $oldLetter = $sLetter; $counterAlle++; } catch (Exception $e) { continue; } } $oldLetter = ''; $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $bFound = false; $ContactsOutput = ''; if (isset($contacts[$letterInfo])) { foreach ($contacts[$letterInfo] as $contactInfo) { $bFound = true; $CONTACTDATA = $contactInfo['data']; $prepareOutput = self::renderSingleCard($CONTACTDATA, $contactInfo, $addressBookPerm, $aFavourites); $ContactsOutput .= '<li class="contactsrow visible">' . $prepareOutput . '</li>'; } } if ($bFound === true) { $buildingOutput .= '<li class="letter" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>' . $ContactsOutput; } else { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } } $buildingOutput .= '<li><span class="noitem hidden">' . (string) self::$l10n->t('No Cards found!') . '</span></li>'; $buildingOutput .= '</ul>'; } else { $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } $buildingOutput .= '<li><span class="noitem">' . (string) self::$l10n->t('Add a new contact or import existing contacts from a file (VCF) per Drag & Drop.') . ' <i id="importAddrStart" title="' . (string) self::$l10n->t('Import addressbook per Drag & Drop') . '" class="toolTip ioc ioc-upload"></i></span></li>'; $buildingOutput .= '</ul>'; } return $buildingOutput; }
/** * Get the PHOTO or LOGO * * @return \OCP\Image|null */ public function getPhoto() { $image = new \OCP\Image(); if (isset($this->PHOTO)) { $photo = $this->PHOTO; } elseif (isset($this->LOGO)) { $photo = $this->LOGO; } else { return null; } $photovalue = $photo->getValue(); if ($photo instanceof \Sabre\VObject\Property\Uri && substr($photovalue, 0, 5) === 'data:') { $mimeType = substr($photovalue, 5, strpos($photovalue, ',') - 5); if (strpos($mimeType, ';')) { $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); } $photovalue = substr($photovalue, strpos($photovalue, ',') + 1); if ($image->loadFromBase64($photovalue)) { return $image; } } elseif ($image->loadFromData($photovalue)) { return $image; } return null; }
public static function renderOutput($addrbookId, $grpId = 'all') { if ($grpId === 'all') { $contacts_alphabet = VCard::all($addrbookId, null, null, array(), true); } elseif ($grpId === 'fav') { $contacts_alphabet = VCard::allByFavourite(); } else { $contacts_alphabet = VCard::getCardsByGroups($addrbookId, $grpId, null, null, true); } $addressBookPerm = Addressbook::find($addrbookId); //Favourites $favorites = \OC::$server->getTagManager()->load(self::$appname)->getFavorites(); $aFavourites = array(); if (is_array($favorites)) { foreach ($favorites as $fav) { $aFavourites[$fav] = 1; } } $contacts = array(); $counterAlle = 0; $aLetter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); if ($contacts_alphabet) { foreach ($contacts_alphabet as $contact) { try { $vcard = VObject\Reader::read($contact['carddata']); $details = VCard::structureContact($vcard); $imgBuild = ''; if ($vcard->PHOTO) { $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgBuild = 'data:' . $image->mimeType() . ';base64,' . $imgSrc; } $sLetter = strtoupper(substr($contact['fullname'], 0, 1)); //\OCP\Util::writeLog(self::$appname,'LETTER: '. $sLetter.':'.$contact['fullname'], \OCP\Util::DEBUG); $contacts[$sLetter][] = array('id' => $contact['id'], 'aid' => $contact['addressbookid'], 'letter' => $sLetter, 'photo' => $imgBuild, 'component' => $contact['component'], 'fullname' => $contact['fullname'], 'surename' => $contact['surename'], 'lastname' => $contact['lastname'], 'data' => $details); $counterAlle++; } catch (Exception $e) { continue; } } $oldLetter = ''; $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $bFound = false; $ContactsOutput = ''; if (isset($contacts[$letterInfo])) { foreach ($contacts[$letterInfo] as $contactInfo) { $bFound = true; $CONTACTDATA = $contactInfo['data']; $prepareOutput = self::renderSingleCard($CONTACTDATA, $contactInfo, $addressBookPerm, $aFavourites); $ContactsOutput .= '<li class="contactsrow">' . $prepareOutput . '</li>'; } } if ($bFound === true) { $buildingOutput .= '<li class="letter" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>' . $ContactsOutput; } else { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } } $buildingOutput .= '</ul>'; } else { $buildingOutput = '<ul>'; foreach ($aLetter as $letterInfo) { $buildingOutput .= '<li class="letter hidden" data-scroll="' . $letterInfo . '"><span>' . $letterInfo . '</span></li>'; } $buildingOutput .= '<li><span class="noitem">' . (string) self::$l10n->t('No Cards found!') . '</span></li>'; $buildingOutput .= '</ul>'; } return $buildingOutput; }
} session_write_close(); $ownerView = new \OC\Files\View('/' . $user . '/files'); $mime = $ownerView->getMimeType($img); list($mimePart, ) = explode('/', $mime); if ($mimePart === 'image') { $fileInfo = $ownerView->getFileInfo($img); if ($fileInfo['encrypted'] === true) { $local = $ownerView->toTmpFile($img); } else { $local = $ownerView->getLocalFile($img); } $rotate = false; if (is_callable('exif_read_data')) { //don't use OCP\Image here, using OCP\Image will always cause parsing the image file $exif = @exif_read_data($local, 'IFD0'); if (isset($exif['Orientation'])) { $rotate = $exif['Orientation'] > 1; } } OCP\Response::setContentDispositionHeader(basename($img), 'attachment'); if ($rotate) { $image = new OCP\Image($local); $image->fixOrientation(); $image->show(); } else { //use the original file if we dont need to rotate, saves having to re-encode the image header('Content-Type: ' . $mime); $ownerView->readfile($img); } }
/** * Generate thumbnail of an Image with GD * @param string $viewPath Source view path * @param string $srcImagePath Source image path relative to the ownCloud fakeroot * @param string $dstImagePath Destination image path * @return boolean TRUE image generated successfully, FALSE otherwise */ private function generateImageThumbnailGD($viewPath, $srcImagePath, $dstImagePath) { $view = new \OC\Files\View($viewPath); $imageLocalPath = $view->getLocalFile($srcImagePath); $image = new \OCP\Image(); $image->loadFromFile($imageLocalPath); if (!$image->valid()) { return FALSE; } //Non legge il path $image->fixOrientation(); $image->resize($this->width); $imageRsrc = $image->resource(); $height = $image->height(); $width = $image->width(); $widthOffset = intval(($this->width - $width) / 2); $heightOffset = intval(($this->height - $height) / 2); $thumbGDImage = imagecreatetruecolor($this->width, $this->height); // Fill with background color $bgColor = imagecolorallocate($thumbGDImage, $this->bgColor['red'], $this->bgColor['green'], $this->bgColor['blue']); imagefilledrectangle($thumbGDImage, 0, 0, $this->width, $this->height, $bgColor); imagecopyresampled($thumbGDImage, $imageRsrc, $widthOffset, $heightOffset, 0, 0, $width, $height, $width, $height); imagepng($thumbGDImage, $dstImagePath, 7); imagedestroy($thumbGDImage); return TRUE; }
public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } $image = new \OCP\Image(); if (!isset($vCard->PHOTO) || !$image->loadFromBase64((string) $vCard->PHOTO)) { return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }
/** * Get the PHOTO or LOGO * * @return \OCP\Image|null */ public function getPhoto() { $image = new \OCP\Image(); if (isset($this->PHOTO) && $image->loadFromData($this->PHOTO->getValue())) { return $image; } elseif (isset($this->LOGO) && $image->loadFromData($this->LOGO->getValue())) { return $image; } return null; }
/** * @NoAdminRequired */ public function showContact($id) { $id = $this->params('id'); $vcard = ContactsApp::getContactVCard($id); $oldaddressbookid = VCard::getAddressbookid($id); $addressBookPerm = Addressbook::find($oldaddressbookid); $editInfoCard = VCard::structureContact($vcard); $TELTYPE = ContactsApp::getTypesOfProperty('TEL'); $EMAILTYPE = ContactsApp::getTypesOfProperty('EMAIL'); $URLTYPE = ContactsApp::getTypesOfProperty('URL'); $ADRTYPE = ContactsApp::getTypesOfProperty('ADR'); $IMTYPE = ContactsApp::getIMOptions(); $aDefNArray = array('0' => 'lname', '1' => 'fname', '2' => 'anrede', '3' => 'title'); $aN = ''; if (isset($editInfoCard['N'][0]['value']) && count($editInfoCard['N'][0]['value']) > 0) { foreach ($editInfoCard['N'][0]['value'] as $key => $val) { if ($val != '') { $aN[$aDefNArray[$key]] = $val; } } } //X-ABSHOWAS; $bShowCompany = false; if (isset($editInfoCard['ORG'][0]['SHOWAS']) && $editInfoCard['ORG'][0]['SHOWAS'] == 'COMPANY') { $bShowCompany = true; } $aOrgDef = array('0' => 'firm', '1' => 'department'); $aOrg = array(); if (isset($editInfoCard['ORG'][0]['value']) && count($editInfoCard['ORG'][0]['value']) > 0) { foreach ($editInfoCard['ORG'][0]['value'] as $key => $val) { if ($val != '') { $aOrg[$aOrgDef[$key]] = $val; } } } $sBday = ''; if (isset($editInfoCard['BDAY'][0]['value']) && !empty($editInfoCard['BDAY'][0]['value'])) { $sBday = $editInfoCard['BDAY'][0]['value']; $date = new \DateTime($sBday); $sBday = $date->format('d. M Y'); } $sNotice = ''; if (isset($editInfoCard['NOTE'][0]['value']) && !empty($editInfoCard['NOTE'][0]['value'])) { $sNotice = stripcslashes($editInfoCard['NOTE'][0]['value']); $sNotice = str_replace("\n", '<br />', $sNotice); } $sNickname = ''; if (isset($editInfoCard['NICKNAME'][0]['value']) && !empty($editInfoCard['NICKNAME'][0]['value'])) { $sNickname = $editInfoCard['NICKNAME'][0]['value']; } $sPosition = ''; if (isset($editInfoCard['TITLE'][0]['value']) && !empty($editInfoCard['TITLE'][0]['value'])) { $sPosition = $editInfoCard['TITLE'][0]['value']; } $aAddr = ''; if (array_key_exists('ADR', $editInfoCard)) { $aAddr = $this->getAddressInfo($editInfoCard['ADR'], $ADRTYPE); } $aTel = ''; if (array_key_exists('TEL', $editInfoCard)) { $aTel = $this->getPhoneInfo($editInfoCard['TEL'], $TELTYPE); } $aEmail = ''; if (array_key_exists('EMAIL', $editInfoCard)) { $aEmail = $this->getEmailInfo($editInfoCard['EMAIL'], $EMAILTYPE); } $aUrl = ''; if (array_key_exists('URL', $editInfoCard)) { $aUrl = $this->getUrlInfo($editInfoCard['URL'], $URLTYPE); } $aImpp = ''; if (array_key_exists('IMPP', $editInfoCard)) { $aImpp = $this->getImppInfo($editInfoCard['IMPP'], $IMTYPE); } $aCloud = ''; if (array_key_exists('CLOUD', $editInfoCard)) { $aCloud = $this->getCloudInfo($editInfoCard['CLOUD'], $ADRTYPE); } $bPhoto = 0; $imgSrc = ''; $imgMimeType = ''; $tmpkey = 'editphoto'; $thumb = '<div id="noimage" class="ioc ioc-user"></div>'; if (isset($vcard->PHOTO)) { $bPhoto = 1; $thumb = ''; $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgMimeType = $image->mimeType(); \OC::$server->getCache()->remove($tmpkey); \OC::$server->getCache()->set($tmpkey, $image->data(), 600); } $catOutput = ''; if (isset($editInfoCard['CATEGORIES'][0]['value']) && count($editInfoCard['CATEGORIES'][0]['value']) > 0) { foreach ($editInfoCard['CATEGORIES'] as $key => $catInfo) { if ($key == 'value') { $aCatInfo = explode(',', $catInfo['value']); foreach ($aCatInfo as $key => $val) { $backgroundColor = ContactsApp::genColorCodeFromText(trim($val), 80); $color = ContactsApp::generateTextColor($backgroundColor); $catOutput .= '<span class="colorgroup toolTip" data-category="' . $val . '" style="background-color:' . $backgroundColor . ';color:' . $color . ';" title="' . $val . '">' . mb_substr($val, 0, 1, "UTF-8") . '</span> '; } } } } $addressBookName = $addressBookPerm['displayname']; $maxUploadFilesize = \OCP\Util::maxUploadFilesize('/'); $params = ['id' => $id, 'tmpkey' => $tmpkey, 'oldaddressbookid' => $oldaddressbookid, 'addressbooksPerm' => $addressBookPerm, 'isPhoto' => $bPhoto, 'thumbnail' => $thumb, 'categories' => $catOutput, 'addressbookname' => $addressBookName, 'bShowCompany' => $bShowCompany, 'imgsrc' => $imgSrc, 'imgMimeType' => $imgMimeType, 'anrede' => isset($aN['title']) ? $aN['title'] : '', 'fname' => isset($aN['fname']) ? $aN['fname'] : '', 'lname' => isset($aN['lname']) ? $aN['lname'] : '', 'firm' => isset($aOrg['firm']) ? $aOrg['firm'] : '', 'department' => isset($aOrg['department']) ? $aOrg['department'] : '', 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), 'aTel' => isset($aTel) ? $aTel : '', 'aEmail' => isset($aEmail) ? $aEmail : '', 'aAddr' => isset($aAddr) ? $aAddr : '', 'aUrl' => isset($aUrl) ? $aUrl : '', 'aImpp' => isset($aImpp) ? $aImpp : '', 'aCloud' => isset($aCloud) ? $aCloud : '', 'sBday' => isset($sBday) ? $sBday : '', 'nickname' => isset($sNickname) ? $sNickname : '', 'position' => isset($sPosition) ? $sPosition : '', 'sNotice' => isset($sNotice) ? $sNotice : '']; $response = new TemplateResponse($this->appName, 'contact.show', $params, ''); return $response; }
public static function cacheThumbnail($id, \OC_Image $image = null, $remove = false, $update = false) { $key = self::THUMBNAIL_PREFIX . $id; if (\OC::$server->getCache()->hasKey($key) && $image === null && $remove === false && $update === false) { return \OC::$server->getCache()->get($key); } if ($remove) { \OC::$server->getCache()->remove($key); if (!$update) { return false; } } if (is_null($image)) { $vcard = self::getContactVCard($id); // invalid vcard if (is_null($vcard)) { \OCP\Util::writeLog(self::$appname, __METHOD__ . ' The VCard for ID ' . $id . ' is not RFC compatible', \OCP\Util::ERROR); return false; } $image = new \OCP\Image(); if (!isset($vcard->PHOTO)) { return false; } if (!$image->loadFromBase64((string) $vcard->PHOTO)) { return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog(self::$appname, 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog(self::$appname, 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id, \OCP\Util::ERROR); return false; } // Cache for around a month \OC::$server->getCache()->set($key, $image->data(), 3000000); \OCP\Util::writeLog(self::$appname, 'Caching ' . $id, \OCP\Util::DEBUG); return \OC::$server->getCache()->get($key); }
/** * @NoAdminRequired */ public function showContact() { $id = $this->params('id'); $vcard = ContactsApp::getContactVCard($id); $oldaddressbookid = VCard::getAddressbookid($id); $addressBookPerm = Addressbook::find($oldaddressbookid); $editInfoCard = VCard::structureContact($vcard); $TELTYPE = ContactsApp::getTypesOfProperty('TEL'); $EMAILTYPE = ContactsApp::getTypesOfProperty('EMAIL'); $URLTYPE = ContactsApp::getTypesOfProperty('URL'); $ADRTYPE = ContactsApp::getTypesOfProperty('ADR'); $aDefNArray = array('0' => 'lname', '1' => 'fname', '2' => 'anrede', '3' => 'title'); $aN = ''; if (isset($editInfoCard['N'][0]['value']) && count($editInfoCard['N'][0]['value']) > 0) { foreach ($editInfoCard['N'][0]['value'] as $key => $val) { if ($val != '') { $aN[$aDefNArray[$key]] = $val; } } } $aOrgDef = array('0' => 'firm', '1' => 'department'); $aOrg = array(); if (isset($editInfoCard['ORG'][0]['value']) && count($editInfoCard['ORG'][0]['value']) > 0) { foreach ($editInfoCard['ORG'][0]['value'] as $key => $val) { if ($val != '') { $aOrg[$aOrgDef[$key]] = $val; } } } $sBday = ''; if (isset($editInfoCard['BDAY'][0]['value']) && !empty($editInfoCard['BDAY'][0]['value'])) { $sBday = $editInfoCard['BDAY'][0]['value']; $date = new \DateTime($sBday); $sBday = $date->format('d. M Y'); } $sNotice = ''; if (isset($editInfoCard['NOTE'][0]['value']) && !empty($editInfoCard['NOTE'][0]['value'])) { $sNotice = $editInfoCard['NOTE'][0]['value']; } $sNickname = ''; if (isset($editInfoCard['NICKNAME'][0]['value']) && !empty($editInfoCard['NICKNAME'][0]['value'])) { $sNickname = $editInfoCard['NICKNAME'][0]['value']; } $sPosition = ''; if (isset($editInfoCard['TITLE'][0]['value']) && !empty($editInfoCard['TITLE'][0]['value'])) { $sPosition = $editInfoCard['TITLE'][0]['value']; } $aAddr = ''; if (array_key_exists('ADR', $editInfoCard)) { $aAddr = $this->getAddressInfo($editInfoCard['ADR'], $ADRTYPE); } $aTel = ''; if (array_key_exists('TEL', $editInfoCard)) { $aTel = $this->getPhoneInfo($editInfoCard['TEL'], $TELTYPE); } $aEmail = ''; if (array_key_exists('EMAIL', $editInfoCard)) { $aEmail = $this->getEmailInfo($editInfoCard['EMAIL'], $EMAILTYPE); } $aUrl = ''; if (array_key_exists('URL', $editInfoCard)) { $aUrl = $this->getUrlInfo($editInfoCard['URL'], $URLTYPE); } $bPhoto = 0; $imgSrc = ''; $imgMimeType = ''; $thumb = '<div id="noimage" class="ioc ioc-user"></div>'; if (isset($vcard->PHOTO)) { $bPhoto = 1; $thumb = ''; $image = new \OCP\Image(); $image->loadFromData((string) $vcard->PHOTO); $imgSrc = $image->__toString(); $imgMimeType = $image->mimeType(); \OC::$server->getCache()->set('show-contacts-foto-' . $id, $image->data(), 600); } $maxUploadFilesize = \OCP\Util::maxUploadFilesize('/'); $params = ['id' => $id, 'tmpkey' => 'show-contacts-foto-' . $id, 'oldaddressbookid' => $oldaddressbookid, 'addressbooksPerm' => $addressBookPerm, 'isPhoto' => $bPhoto, 'thumbnail' => $thumb, 'imgsrc' => $imgSrc, 'imgMimeType' => $imgMimeType, 'anrede' => isset($aN['title']) ? $aN['title'] : '', 'fname' => isset($aN['fname']) ? $aN['fname'] : '', 'lname' => isset($aN['lname']) ? $aN['lname'] : '', 'firm' => isset($aOrg['firm']) ? $aOrg['firm'] : '', 'department' => isset($aOrg['department']) ? $aOrg['department'] : '', 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), 'aTel' => isset($aTel) ? $aTel : '', 'aEmail' => isset($aEmail) ? $aEmail : '', 'aAddr' => isset($aAddr) ? $aAddr : '', 'aUrl' => isset($aUrl) ? $aUrl : '', 'sBday' => isset($sBday) ? $sBday : '', 'nickname' => isset($sNickname) ? $sNickname : '', 'position' => isset($sPosition) ? $sPosition : '', 'sNotice' => isset($sNotice) ? $sNotice : '']; $response = new TemplateResponse($this->appName, 'contact.show', $params, ''); return $response; }
/** * Get the PHOTO or LOGO * * @return \OCP\Image|null */ public function getPhoto() { $image = new \OCP\Image(); if (isset($this->PHOTO) && $image->loadFromBase64((string) $this->PHOTO)) { return $image; } elseif (isset($this->LOGO) && $image->loadFromBase64((string) $this->LOGO)) { return $image; } }
/** * @NoAdminRequired */ public function uploadPhoto() { //$type = $this->request->getHeader('Content-Type'); $id = $this->params('id'); $file = $this->request->getUploadedFile('imagefile'); $error = $file['error']; if ($error !== UPLOAD_ERR_OK) { $errors = array(0 => $this->l10n->t("There is no error, the file uploaded with success"), 1 => $this->l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini") . ini_get('upload_max_filesize'), 2 => $this->l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"), 3 => $this->l10n->t("The uploaded file was only partially uploaded"), 4 => $this->l10n->t("No file was uploaded"), 6 => $this->l10n->t("Missing a temporary folder")); \OCP\Util::writeLog($this->appName, 'Uploaderror: ' . $errors[$error], \OCP\Util::DEBUG); } if (file_exists($file['tmp_name'])) { $tmpkey = 'editphoto'; $size = getimagesize($file['tmp_name'], $info); //$exif = @exif_read_data($file['tmp_name']); $image = new \OCP\Image(); if ($image->loadFromFile($file['tmp_name'])) { if (!$image->fixOrientation()) { // No fatal error so we don't bail out. \OCP\Util::writeLog($this->appName, 'Couldn\'t save correct image orientation: ' . $tmpkey, \OCP\Util::DEBUG); } \OC::$server->getCache()->remove($tmpkey); \OC::$server->getCache()->remove($tmpkey . 'ratio'); $originalWidth = $image->width(); if (\OC::$server->getCache()->set($tmpkey, $image->data(), 600)) { if ($image->width() > 400 || $image->height() > 400) { $image->resize(400); // Prettier resizing than with browser and saves bandwidth. } $ratio = $originalWidth / $image->width(); if (\OC::$server->getCache()->set($tmpkey . 'ratio', $ratio, 600)) { $imgString = $image->__toString(); $resultData = array('mime' => $file['type'], 'size' => $file['size'], 'name' => $file['name'], 'id' => $id, 'tmp' => $tmpkey, 'imgdata' => $imgString); $response = new JSONResponse(); $response->setData($resultData); return $response; } } } } }
public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false) { $key = self::THUMBNAIL_PREFIX . $this->combinedKey(); //\OC_Cache::remove($key); if (\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) { return \OC_Cache::get($key); } if ($remove) { \OC_Cache::remove($key); if (!$update) { return false; } } if (is_null($image)) { $this->retrieve(); $image = new \OCP\Image(); if (!isset($this->PHOTO) && !isset($this->LOGO)) { return false; } if (!$image->loadFromBase64((string) $this->PHOTO)) { if (!$image->loadFromBase64((string) $this->LOGO)) { return false; } } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month \OC_Cache::set($key, strval($image), 3000000); \OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG); return \OC_Cache::get($key); }
public static function cacheThumbnail($backendName, $addressBookId, $contactId, \OCP\Image $image = null, $vCard = null, $options = array()) { $cache = \OC::$server->getCache(); $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; //$cache->remove($key); $haskey = $cache->hasKey($key); if (!array_key_exists('remove', $options) && !array_key_exists('update', $options)) { if ($cache->hasKey($key) && $image === null) { return $cache->get($key); } } else { if (isset($options['remove']) && $options['remove'] === false && (isset($options['update']) && $options['update'] === false)) { return $cache->get($key); } } if (isset($options['remove']) && $options['remove']) { $cache->remove($key); if (!isset($options['update']) || !$options['update']) { return false; } } if (is_null($image)) { if (is_null($vCard)) { $app = new App(); $vCard = $app->getContact($backendName, $addressBookId, $contactId); } if (!isset($vCard->PHOTO)) { return false; } $image = new \OCP\Image(); $photostring = (string) $vCard->PHOTO; if ($vCard->PHOTO instanceof \Sabre\VObject\Property\Uri && substr($photostring, 0, 5) === 'data:') { $mimeType = substr($photostring, 5, strpos($photostring, ',') - 5); if (strpos($mimeType, ';')) { $mimeType = substr($mimeType, 0, strpos($mimeType, ';')); } $photostring = substr($photostring, strpos($photostring, ',') + 1); } if (!$image->loadFromBase64($photostring)) { #\OCP\Util::writeLog('contacts', __METHOD__.', photo: ' . print_r($photostring, true), \OCP\Util::DEBUG); return false; } } if (!$image->centerCrop()) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } if (!$image->resize(self::THUMBNAIL_SIZE)) { \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR); return false; } // Cache as base64 for around a month $cache->set($key, strval($image), 3000000); return $cache->get($key); }