/** * @NoAdminRequired */ public function saveCropPhotoContact() { $id = $this->params('id'); $tmpkey = $this->params('tmpkey'); $x = $this->params('x1', 0); $y = $this->params('y1', 0); $w = $this->params('w', -1); $h = $this->params('h', -1); $image = null; //\OCP\Util::writeLog($this->appName,'TMP:'.$tmpkey,\OCP\Util::DEBUG); $data = \OC::$server->getCache()->get($tmpkey); $ratio = \OC::$server->getCache()->get($tmpkey . 'ratio'); if ($data && $ratio) { \OC::$server->getCache()->remove($tmpkey); \OC::$server->getCache()->remove($tmpkey . 'ratio'); $x *= $ratio; $w *= $ratio; $y *= $ratio; $h *= $ratio; $image = new \OCP\Image(); if ($image->loadFromdata($data)) { $w = $w > 0 ? $w : $image->width(); $h = $h > 0 ? $h : $image->height(); if ($image->crop($x, $y, $w, $h)) { if ($image->width() <= 400 && $image->height() <= 400 || $image->resize(400)) { $vcard = ContactsApp::getContactVCard($id); if (!$vcard) { \OC::$server->getCache()->remove($tmpkey); } if (isset($vcard->PHOTO)) { $property = $vcard->select('PHOTO'); if (!$property) { \OC::$server->getCache()->remove($tmpkey); //bailOut(OCA\Kontakte\App::$l10n->t('Error getting PHOTO property.')); } unset($vcard->PHOTO); $vcard->add('PHOTO', base64_decode($image->__toString()), array('ENCODING' => 'b', 'TYPE' => $image->mimeType())); } else { $vcard->add('PHOTO', base64_decode($image->__toString()), array('ENCODING' => 'b', 'TYPE' => $image->mimeType())); } $now = new \DateTime(); $vcard->REV = $now->format(\DateTime::W3C); if (!VCard::edit($id, $vcard)) { //bailOut(OCA\Kontakte\App::$l10n->t('Error saving contact.')); } $imgString = $image->__toString(); $result = ['status' => 'success', 'data' => ['id' => $id, 'width' => $image->width(), 'height' => $image->height(), 'dataimg' => $imgString, 'mimetype' => $image->mimeType(), 'lastmodified' => ContactsApp::lastModified($vcard)->format('U')]]; \OC::$server->getCache()->remove($tmpkey); \OC::$server->getCache()->remove('show-contacts-foto-' . $id); \OC::$server->getCache()->set($tmpkey, $image->data(), 600); \OC::$server->getCache()->set('show-contacts-foto-' . $id, $image->data(), 600); $response = new JSONResponse(); $response->setData($result); return $response; } } } } }