/** * @NoAdminRequired * * @param array $crop * @return DataResponse */ public function postCroppedAvatar($crop) { $userId = $this->userSession->getUser()->getUID(); if (is_null($crop)) { return new DataResponse(['data' => ['message' => $this->l->t("No crop data provided")]], Http::STATUS_BAD_REQUEST); } if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { return new DataResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], Http::STATUS_BAD_REQUEST); } $tmpAvatar = $this->cache->get('tmpAvatar'); if (is_null($tmpAvatar)) { return new DataResponse(['data' => ['message' => $this->l->t("No temporary profile picture available, try again")]], Http::STATUS_BAD_REQUEST); } $image = new \OC_Image($tmpAvatar); $image->crop($crop['x'], $crop['y'], round($crop['w']), round($crop['h'])); try { $avatar = $this->avatarManager->getAvatar($userId); $avatar->set($image); // Clean up $this->cache->remove('tmpAvatar'); return new DataResponse(['status' => 'success']); } catch (\OC\NotSquareException $e) { return new DataResponse(['data' => ['message' => $this->l->t('Crop is not square')]], Http::STATUS_BAD_REQUEST); } catch (\Exception $e) { return new DataResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_BAD_REQUEST); } }
$id = isset($_POST['id']) ? $_POST['id'] : ''; if ($tmpkey == '') { bailOut('Missing key to temporary file.'); } if ($id == '') { bailOut('Missing contact id.'); } OCP\Util::writeLog('contacts', 'savecrop.php: key: ' . $tmpkey, OCP\Util::DEBUG); $data = OC_Cache::get($tmpkey); if ($data) { $image = new OC_Image(); if ($image->loadFromdata($data)) { $w = $w != -1 ? $w : $image->width(); $h = $h != -1 ? $h : $image->height(); OCP\Util::writeLog('contacts', 'savecrop.php, x: ' . $x1 . ' y: ' . $y1 . ' w: ' . $w . ' h: ' . $h, OCP\Util::DEBUG); if ($image->crop($x1, $y1, $w, $h)) { if ($image->width() <= 200 && $image->height() <= 200 || $image->resize(200)) { $vcard = OCA\Contacts\App::getContactVCard($id); if (!$vcard) { OC_Cache::remove($tmpkey); bailOut(OCA\Contacts\App::$l10n->t('Error getting contact object.')); } if ($vcard->__isset('PHOTO')) { OCP\Util::writeLog('contacts', 'savecrop.php: PHOTO property exists.', OCP\Util::DEBUG); $property = $vcard->__get('PHOTO'); if (!$property) { OC_Cache::remove($tmpkey); bailOut(OCA\Contacts\App::$l10n->t('Error getting PHOTO property.')); } $property->setValue($image->__toString()); $property->parameters[] = new Sabre\VObject\Parameter('ENCODING', 'b');
public function testCrop() { $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png'); $this->assertTrue($img->crop(0, 0, 50, 20)); $this->assertEquals(50, $img->width()); $this->assertEquals(20, $img->height()); $img = new \OC_Image(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.jpg')); $this->assertTrue($img->crop(500, 700, 550, 300)); $this->assertEquals(550, $img->width()); $this->assertEquals(300, $img->height()); $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.gif'))); $this->assertTrue($img->crop(10, 10, 15, 15)); $this->assertEquals(15, $img->width()); $this->assertEquals(15, $img->height()); }
public static function postCroppedAvatar($args) { \OC_JSON::checkLoggedIn(); \OC_JSON::callCheck(); $user = \OC_User::getUser(); if (isset($_POST['crop'])) { $crop = $_POST['crop']; } else { $l = new \OC_L10n('core'); \OC_JSON::error(array("data" => array("message" => $l->t("No crop data provided")))); return; } $tmpavatar = \OC_Cache::get('tmpavatar'); if (is_null($tmpavatar)) { $l = new \OC_L10n('core'); \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again")))); return; } $image = new \OC_Image($tmpavatar); $image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']); try { $avatar = new \OC_Avatar($user); $avatar->set($image->data()); // Clean up \OC_Cache::remove('tmpavatar'); \OC_JSON::success(); } catch (\Exception $e) { \OC_JSON::error(array("data" => array("message" => $e->getMessage()))); } }