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 testGetAvatarNoSizeMatch() { $this->folder->method('nodeExists')->will($this->returnValueMap([['avatar.png', true], ['avatar.32.png', false]])); $expected = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); $expected2 = new OC_Image(\OC::$SERVERROOT . '/tests/data/testavatar.png'); $expected2->resize(32); $file = $this->getMock('\\OCP\\Files\\File'); $file->method('getContent')->willReturn($expected->data()); $this->folder->method('get')->with('avatar.png')->willReturn($file); $newFile = $this->getMock('\\OCP\\Files\\File'); $newFile->expects($this->once())->method('putContent')->with($expected2->data()); $this->folder->expects($this->once())->method('newFile')->with('avatar.32.png')->willReturn($newFile); $this->assertEquals($expected2->data(), $this->avatar->get(32)->data()); }
/** * get the users avatar * @param int $size size in px of the avatar, avatars are square, defaults to 64 * @return boolean|\OC_Image containing the avatar or false if there's no image */ public function get($size = 64) { if ($this->view->file_exists('avatar.jpg')) { $ext = 'jpg'; } elseif ($this->view->file_exists('avatar.png')) { $ext = 'png'; } else { return false; } $avatar = new OC_Image(); $avatar->loadFromData($this->view->file_get_contents('avatar.' . $ext)); $avatar->resize($size); return $avatar; }
function handleGetAlbumThumbnail($token, $albumname) { $owner = OC_Gallery_Sharing::getTokenOwner($token); $file = OCP\Config::getSystemValue("datadirectory") . '/' . $owner . '/gallery/' . $albumname . '.png'; $image = new OC_Image($file); if ($image->valid()) { $image->centerCrop(); $image->resize(200); $image->fixOrientation(); OCP\Response::enableCaching(3600 * 24); // 24 hour $image->show(); } }
function handleGetAlbumThumbnail($token, $albumname) { $owner = OC_Gallery_Sharing::getTokenOwner($token); $view = OCP\Files::getStorage('gallery'); $file = $view->fopen($albumname . '.png', 'r'); $image = new OC_Image($file); if ($image->valid()) { $image->centerCrop(); $image->resize(200); $image->fixOrientation(); OCP\Response::enableCaching(3600 * 24); // 24 hour $image->show(); } }
public function testResize() { $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png'); $this->assertTrue($img->resize(32)); $this->assertEquals(32, $img->width()); $this->assertEquals(32, $img->height()); $img = new \OC_Image(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.jpg')); $this->assertTrue($img->resize(840)); $this->assertEquals(840, $img->width()); $this->assertEquals(525, $img->height()); $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT . '/tests/data/testimage.gif'))); $this->assertTrue($img->resize(100)); $this->assertEquals(100, $img->width()); $this->assertEquals(100, $img->height()); }
/** * ownCloud - Image generator for contacts. * * @author Thomas Tanghus * @copyright 2012 Thomas Tanghus <*****@*****.**> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ $tmpkey = $_GET['tmpkey']; $maxsize = isset($_GET['maxsize']) ? $_GET['maxsize'] : -1; header("Cache-Control: no-cache, no-store, must-revalidate"); OCP\Util::writeLog('contacts', 'tmpphoto.php: tmpkey: ' . $tmpkey, OCP\Util::DEBUG); $image = new OC_Image(); $image->loadFromData(OC_Cache::get($tmpkey)); if ($maxsize != -1) { $image->resize($maxsize); } $image();
public static function getViewImage($image_name, $owner = null) { if (!$owner) { $owner = OCP\USER::getUser(); } $view_file = OC_Filesystem::getLocalFile($image_name); if (!is_dir($save_dir)) { mkdir($save_dir, 0777, true); } if (file_exists($view_file)) { $image = new OC_Image($view_file); } else { $image_path = OC_Filesystem::getLocalFile($image_path); if (!file_exists($image_path)) { return null; } $image = new OC_Image($image_path); if ($image->valid()) { $image->resize(1200); $image->fixOrientation(); $image->save($view_file); } } if ($image->valid()) { return $image; } else { $image->destroy(); } return null; }
} if (!isset($_GET['path'])) { bailOut(OC_Contacts_App::$l10n->t('No photo path was submitted.')); } $localpath = OC_Filesystem::getLocalFile($_GET['path']); $tmpfname = tempnam(get_temp_dir(), "occOrig"); if (!file_exists($localpath)) { bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:') . $localpath); } file_put_contents($tmpfname, file_get_contents($localpath)); $image = new OC_Image(); if (!$image) { bailOut(OC_Contacts_App::$l10n->t('Error loading image.')); } if (!$image->loadFromFile($tmpfname)) { 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. debug('Couldn\'t save correct image orientation: ' . $tmpfname); } if ($image->save($tmpfname)) { OCP\JSON::success(array('data' => array('id' => $_GET['id'], 'tmp' => $tmpfname))); exit; } else { bailOut('Couldn\'t save temporary image: ' . $tmpfname); }
if (is_null($contact)) { OCP\Util::writeLog('contacts', 'photo.php. The VCard for ID ' . $id . ' is not RFC compatible', OCP\Util::ERROR); } else { OCP\Response::enableCaching($caching); OC_Contacts_App::setLastModifiedHeader($contact); // Photo :-) if ($image->loadFromBase64($contact->getAsString('PHOTO'))) { // OK OCP\Response::setETagHeader(md5($contact->getAsString('PHOTO'))); } else { // Logo :-/ if ($image->loadFromBase64($contact->getAsString('LOGO'))) { // OK OCP\Response::setETagHeader(md5($contact->getAsString('LOGO'))); } } if ($image->valid()) { $max_size = 200; if ($image->width() > $max_size || $image->height() > $max_size) { $image->resize($max_size); } } } if (!$image->valid()) { // Not found :-( getStandardImage(); //$image->loadFromFile('img/person_large.png'); } header('Content-Type: ' . $image->mimeType()); $image->show(); //echo OC_Contacts_App::$l10n->t('This card does not contain a photo.');
if (is_null($contact)) { OCP\Util::writeLog('contacts', 'thumbnail.php. The VCard for ID ' . $id . ' is not RFC compatible', OCP\Util::ERROR); getStandardImage(); exit; } OCP\Response::enableCaching($caching); OC_Contacts_App::setLastModifiedHeader($contact); $thumbnail_size = 23; // Find the photo from VCard. $image = new OC_Image(); $photo = $contact->getAsString('PHOTO'); if ($photo) { OCP\Response::setETagHeader(md5($photo)); if ($image->loadFromBase64($photo)) { if ($image->centerCrop()) { if ($image->resize($thumbnail_size)) { if ($image->show()) { // done exit; } else { OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t display thumbnail for ID ' . $id, OCP\Util::ERROR); } } else { OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id, OCP\Util::ERROR); } } else { OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id, OCP\Util::ERROR); } } else { OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t load image string for ID ' . $id, OCP\Util::ERROR); }
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); }
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'); $property->parameters[] = new Sabre\VObject\Parameter('TYPE', $image->mimeType());
public static function getViewImage($image_name, $owner = null) { if (!$owner) { $owner = OCP\USER::getUser(); } $save_dir = OCP\Config::getSystemValue("datadirectory") . '/' . $owner . '/gallery'; $save_dir .= dirname($image_name) . '/view/'; $image_path = $image_name; $view_file = $save_dir . basename($image_name); if (!is_dir($save_dir)) { mkdir($save_dir, 0777, true); } if (file_exists($view_file)) { $image = new OC_Image($view_file); } else { $image_path = OC_Filesystem::getLocalFile($image_path); if (!file_exists($image_path)) { return null; } $image = new OC_Image($image_path); if ($image->valid()) { $image->resize(1200); $image->fixOrientation(); $image->save($view_file); } } if ($image->valid()) { return $image; } else { $image->destroy(); } return null; }