Exemplo n.º 1
0
 /**
  * Returns the rendered image
  *
  * @return string the file
  */
 public function render()
 {
     if ($this->preview instanceof \OC_Image) {
         // Uses imagepng() to output the image
         return $this->preview->data();
     } else {
         return $this->preview;
     }
 }
Exemplo n.º 2
0
 public function testSetAvatar()
 {
     $oldFile = $this->getMock('\\OCP\\Files\\File');
     $this->folder->method('get')->will($this->returnValueMap([['avatar.jpg', $oldFile], ['avatar.png', $oldFile]]));
     $oldFile->expects($this->exactly(2))->method('delete');
     $newFile = $this->getMock('\\OCP\\Files\\File');
     $this->folder->expects($this->once())->method('newFile')->with('avatar.png')->willReturn($newFile);
     $image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
     $newFile->expects($this->once())->method('putContent')->with($image->data());
     $this->avatar->set($image->data());
 }
Exemplo n.º 3
0
 public function testAvatarApi()
 {
     $avatarManager = \OC::$server->getAvatarManager();
     $avatar = $avatarManager->getAvatar($this->user);
     $this->assertEquals(false, $avatar->get());
     $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
     $expected->resize(64);
     $avatar->set($expected->data());
     $this->assertEquals($expected->data(), $avatar->get()->data());
     $avatar->remove();
     $this->assertEquals(false, $avatar->get());
 }
 public function testRenderWithOcImageInstance()
 {
     $resource = file_get_contents(\OC::$SERVERROOT . '/tests/data/testimage.jpg');
     $preview = new \OC_Image($resource);
     $data = ['name' => 'testimage.jpg', 'mimetype' => 'image/jpeg', 'preview' => $preview];
     $imageResponse = new ImageResponse($data);
     $response = $imageResponse->render();
     $this->assertSame($preview->data(), $response);
 }
Exemplo n.º 5
0
 /**
  * @NoAdminRequired
  *
  * @return DataResponse|DataDisplayResponse
  */
 public function getTmpAvatar()
 {
     $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_NOT_FOUND);
     }
     $image = new \OC_Image($tmpAvatar);
     $resp = new DataDisplayResponse($image->data(), Http::STATUS_OK, ['Content-Type' => $image->mimeType()]);
     $resp->setETag(crc32($image->data()));
     $resp->cacheFor(0);
     $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
     return $resp;
 }
Exemplo n.º 6
0
 public function testSetAvatar()
 {
     $avatarFileJPG = $this->getMock('\\OCP\\Files\\File');
     $avatarFileJPG->method('getName')->willReturn('avatar.jpg');
     $avatarFileJPG->expects($this->once())->method('delete');
     $avatarFilePNG = $this->getMock('\\OCP\\Files\\File');
     $avatarFilePNG->method('getName')->willReturn('avatar.png');
     $avatarFilePNG->expects($this->once())->method('delete');
     $resizedAvatarFile = $this->getMock('\\OCP\\Files\\File');
     $resizedAvatarFile->method('getName')->willReturn('avatar.32.jpg');
     $resizedAvatarFile->expects($this->once())->method('delete');
     $nonAvatarFile = $this->getMock('\\OCP\\Files\\File');
     $nonAvatarFile->method('getName')->willReturn('avatarX');
     $nonAvatarFile->expects($this->never())->method('delete');
     $this->folder->method('search')->with('avatar')->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]);
     $newFile = $this->getMock('\\OCP\\Files\\File');
     $this->folder->expects($this->once())->method('newFile')->with('avatar.png')->willReturn($newFile);
     $image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
     $newFile->expects($this->once())->method('putContent')->with($image->data());
     $this->avatar->set($image->data());
 }
Exemplo n.º 7
0
 /**
  * @depends testData
  */
 public function testToString()
 {
     $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png');
     $expected = base64_encode($img->data());
     $this->assertEquals($expected, (string) $img);
     $img = new \OC_Image(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.jpg'));
     $expected = base64_encode($img->data());
     $this->assertEquals($expected, (string) $img);
     $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.gif');
     $expected = base64_encode($img->data());
     $this->assertEquals($expected, (string) $img);
 }
Exemplo n.º 8
0
    bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
}
if (!isset($_GET['path'])) {
    bailOut(OC_Contacts_App::$l10n->t('No photo path was submitted.'));
}
$localpath = OC_Filesystem::getLocalFile($_GET['path']);
$tmpkey = 'contact-photo-' . $_GET['id'];
if (!file_exists($localpath)) {
    bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:') . $localpath);
}
$image = new OC_Image();
if (!$image) {
    bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
}
if (!$image->loadFromFile($localpath)) {
    bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
}
if ($image->width() > 400 || $image->height() > 400) {
    $image->resize(400);
    // Prettier resizing than with browser and saves bandwidth.
}
if (!$image->fixOrientation()) {
    // No fatal error so we don't bail out.
    OCP\Util::writeLog('contacts', 'ajax/oc_photo.php: Couldn\'t save correct image orientation: ' . $localpath, OCP\Util::DEBUG);
}
if (OC_Cache::set($tmpkey, $image->data(), 600)) {
    OCP\JSON::success(array('data' => array('id' => $_GET['id'], 'tmp' => $tmpkey)));
    exit;
} else {
    bailOut('Couldn\'t save temporary image: ' . $tmpkey);
}
Exemplo n.º 9
0
 public static function cacheThumbnail($id, \OC_Image $image = null)
 {
     if (\OC_Cache::hasKey(self::THUMBNAIL_PREFIX . $id)) {
         return \OC_Cache::get(self::THUMBNAIL_PREFIX . $id);
     }
     if (is_null($image)) {
         $vcard = self::getContactVCard($id);
         // invalid vcard
         if (is_null($vcard)) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ' The VCard for ID ' . $id . ' is not RFC compatible', \OCP\Util::ERROR);
             return false;
         }
         $image = new \OC_Image();
         if (!isset($vcard->PHOTO)) {
             return false;
         }
         if (!$image->loadFromBase64((string) $vcard->PHOTO)) {
             return false;
         }
     }
     if (!$image->centerCrop()) {
         \OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id, \OCP\Util::ERROR);
         return false;
     }
     if (!$image->resize(self::THUMBNAIL_SIZE)) {
         \OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id, \OCP\Util::ERROR);
         return false;
     }
     // Cache for around a month
     \OC_Cache::set(self::THUMBNAIL_PREFIX . $id, $image->data(), 3000000);
     \OCP\Util::writeLog('contacts', 'Caching ' . $id, \OCP\Util::DEBUG);
     return \OC_Cache::get(self::THUMBNAIL_PREFIX . $id);
 }
Exemplo n.º 10
0
 public function testSetAvatar()
 {
     $avatarFileJPG = $this->getMock('\\OCP\\Files\\File');
     $avatarFileJPG->method('getName')->willReturn('avatar.jpg');
     $avatarFileJPG->expects($this->once())->method('delete');
     $avatarFilePNG = $this->getMock('\\OCP\\Files\\File');
     $avatarFilePNG->method('getName')->willReturn('avatar.png');
     $avatarFilePNG->expects($this->once())->method('delete');
     $resizedAvatarFile = $this->getMock('\\OCP\\Files\\File');
     $resizedAvatarFile->method('getName')->willReturn('avatar.32.jpg');
     $resizedAvatarFile->expects($this->once())->method('delete');
     $nonAvatarFile = $this->getMock('\\OCP\\Files\\File');
     $nonAvatarFile->method('getName')->willReturn('avatarX');
     $nonAvatarFile->expects($this->never())->method('delete');
     $this->folder->method('getDirectoryListing')->willReturn([$avatarFileJPG, $avatarFilePNG, $resizedAvatarFile, $nonAvatarFile]);
     $newFile = $this->getMock('\\OCP\\Files\\File');
     $this->folder->expects($this->once())->method('newFile')->with('avatar.png')->willReturn($newFile);
     $image = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png');
     $newFile->expects($this->once())->method('putContent')->with($image->data());
     // One on remove and once on setting the new avatar
     $this->user->expects($this->exactly(2))->method('triggerChange');
     $this->avatar->set($image->data());
 }
Exemplo n.º 11
0
 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())));
     }
 }