Пример #1
0
 /**
  * @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);
     if ($data) {
         \OC::$server->getCache()->remove($tmpkey);
         $image = new \OCP\Image();
         if ($image->loadFromdata($data)) {
             $w = $w !== -1 ? $w : $image->width();
             $h = $h !== -1 ? $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;
                 }
             }
         }
     }
 }
 /**
  * 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;
 }