exit; } $sharedFile = \OC\Files\Filesystem::normalizePath($file); } if ($linkedItem['item_type'] === 'file') { $parent = $pathInfo['parent']; $path = $view->getPath($parent); $sharedFile = $pathInfo['name']; } $path = \OC\Files\Filesystem::normalizePath($path, false); if (substr($path, 0, 1) === '/') { $path = substr($path, 1); } if ($maxX === 0 || $maxY === 0) { \OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST); \OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG); exit; } $root = 'files/' . $path; try { $preview = new \OC\Preview($userId, $root); $preview->setFile($sharedFile); $preview->setMaxX($maxX); $preview->setMaxY($maxY); $preview->setScalingUp($scalingUp); $preview->setKeepAspect($keepAspect); $preview->showPreview(); } catch (\Exception $e) { \OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR); \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG); }
$root = \OC\Files\Filesystem::getPath($linkItem['file_source']) . '/'; $images = array_map(function ($image) use($root) { return $root . $image; }, $images); } else { $root = ''; OCP\JSON::checkLoggedIn(); $user = OCP\User::getUser(); } session_write_close(); $eventSource = new OC_EventSource(); foreach ($images as $image) { $height = 200 * $scale; if ($square) { $width = 200 * $scale; } else { $width = 400 * $scale; } $userView = new \OC\Files\View('/' . $user); $preview = new \OC\Preview($user, 'files', '/' . $image, $width, $height); $preview->setKeepAspect(!$square); $fileInfo = $userView->getFileInfo('/files/' . $image); // if the thumbnails is already cached, get it directly from the filesystem to avoid decoding and re-encoding the image $imageName = substr($image, strlen($root)); if ($path = $preview->isCached($fileInfo->getId())) { $eventSource->send('preview', array('image' => $imageName, 'preview' => base64_encode($userView->file_get_contents('/' . $path)))); } else { $eventSource->send('preview', array('image' => $imageName, 'preview' => (string) $preview->getPreview())); } } $eventSource->close();
public function testKeepAspectRatioCover() { $originalWidth = 1680; $originalHeight = 1050; $originalAspectRation = $originalWidth / $originalHeight; $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', 'testimage.jpg', 150, 150); $preview->setKeepAspect(true); $preview->setMode(\OC\Preview::MODE_COVER); $image = $preview->getPreview(); $aspectRatio = $image->width() / $image->height(); $this->assertEquals(round($originalAspectRation, 2), round($aspectRatio, 2)); $this->assertGreaterThanOrEqual(150, $image->width()); $this->assertGreaterThanOrEqual(150, $image->height()); }
/** * Initialises the preview * * @param int $width * @param int $height * * @return \OC\Preview */ private function createPreview($width, $height) { $preview = new \OC\Preview(self::TEST_PREVIEW_USER1, 'files/', $this->sampleFilename, $width, $height); $this->assertSame(true, $preview->isFileValid()); $preview->setKeepAspect($this->keepAspect); $preview->setScalingup($this->scalingUp); return $preview; }
$img = $_GET['file']; if (!empty($_GET['token'])) { $linkItem = \OCP\Share::getShareByToken($_GET['token']); if (!(is_array($linkItem) && isset($linkItem['uid_owner']))) { exit; } // seems to be a valid share $rootLinkItem = \OCP\Share::resolveReShare($linkItem); $user = $rootLinkItem['uid_owner']; // Setup filesystem OCP\JSON::checkUserExists($user); OC_Util::tearDownFS(); OC_Util::setupFS($user); $fullPath = \OC\Files\Filesystem::getPath($linkItem['file_source']); if ($fullPath === null) { exit; } $img = trim($fullPath . '/' . $img); } else { OCP\JSON::checkLoggedIn(); $user = OCP\User::getUser(); } session_write_close(); $square = isset($_GET['square']) ? (bool) $_GET['square'] : false; if ($square) { $preview = new \OC\Preview($user, 'files', '/' . $img, 200 * $scale, 200 * $scale); } else { $preview = new \OC\Preview($user, 'files', '/' . $img, 400 * $scale, 200 * $scale); $preview->setKeepAspect(true); } $preview->showPreview();
/** * return a preview of a file * * @param string $file The path to the file where you want a thumbnail from * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly * @param boolean $aspect Keep aspect * @return \OCP\IImage */ public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false, $aspect = false) { $preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp); $preview->setKeepAspect($aspect); return $preview->getPreview(); }