public static function postAvatar($args) { \OC_JSON::checkLoggedIn(); \OC_JSON::callCheck(); $user = \OC_User::getUser(); if (isset($_POST['path'])) { $path = stripslashes($_POST['path']); $view = new \OC\Files\View('/' . $user . '/files'); $fileInfo = $view->getFileInfo($path); if ($fileInfo['encrypted'] === true) { $fileName = $view->toTmpFile($path); } else { $fileName = $view->getLocalFile($path); } } elseif (!empty($_FILES)) { $files = $_FILES['files']; if ($files['error'][0] === 0 && is_uploaded_file($files['tmp_name'][0]) && !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])) { \OC\Cache::set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); $view = new \OC\Files\View('/' . $user . '/cache'); $fileName = $view->getLocalFile('avatar_upload'); unlink($files['tmp_name'][0]); } } else { $l = new \OC_L10n('core'); \OC_JSON::error(array("data" => array("message" => $l->t("No image or file provided")))); return; } try { $image = new \OC_Image(); $image->loadFromFile($fileName); $image->fixOrientation(); if ($image->valid()) { \OC\Cache::set('tmpavatar', $image->data(), 7200); \OC_JSON::error(array("data" => "notsquare")); } else { $l = new \OC_L10n('core'); $mimeType = $image->mimeType(); if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { \OC_JSON::error(array("data" => array("message" => $l->t("Unknown filetype")))); } if (!$image->valid()) { \OC_JSON::error(array("data" => array("message" => $l->t("Invalid image")))); } } } catch (\Exception $e) { \OC_JSON::error(array("data" => array("message" => $e->getMessage()))); } }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $this->initCmd(); if (is_null($this->cmd)) { return false; } $absPath = $fileview->toTmpFile($path); $tmpDir = get_temp_dir(); $defaultParameters = ' --headless --nologo --nofirststartwizard --invisible --norestore -convert-to pdf -outdir '; $clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters); $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); $export = 'export HOME=/' . $tmpDir; shell_exec($export . "\n" . $exec); //create imagick object from pdf try { $pdf = new \imagick($absPath . '.pdf' . '[0]'); $pdf->setImageFormat('jpg'); } catch (\Exception $e) { unlink($absPath); unlink($absPath . '.pdf'); \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); return false; } $image = new \OC_Image(); $image->loadFromData($pdf); unlink($absPath); unlink($absPath . '.pdf'); return $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { // TODO: use proc_open() and stream the source file ? $absPath = \OC_Helper::tmpFile(); $tmpPath = \OC_Helper::tmpFile(); $handle = $fileview->fopen($path, 'rb'); // we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB. // in some cases 1MB was no enough to generate thumbnail $firstmb = stream_get_contents($handle, 5242880); file_put_contents($absPath, $firstmb); if (self::$avconvBinary) { $cmd = self::$avconvBinary . ' -an -y -ss 5' . ' -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) . ' > /dev/null 2>&1'; } else { $cmd = self::$ffmpegBinary . ' -y -ss 5' . ' -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1' . ' -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) . ' ' . escapeshellarg($tmpPath) . ' > /dev/null 2>&1'; } exec($cmd, $output, $returnCode); unlink($absPath); if ($returnCode === 0) { $image = new \OC_Image(); $image->loadFromFile($tmpPath); unlink($tmpPath); return $image->valid() ? $image : false; } return false; }
/** * @param string $path * @param int $maxX * @param int $maxY * @param boolean $scalingup * @param \OC\Files\View $fileview * @return bool|\OC_Image */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $content = $fileview->fopen($path, 'r'); $content = stream_get_contents($content); //don't create previews of empty text files if (trim($content) === '') { return false; } $lines = preg_split("/\r\n|\n|\r/", $content); $fontSize = 5; //5px $lineSize = ceil($fontSize * 1.25); $image = imagecreate($maxX, $maxY); imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); foreach ($lines as $index => $line) { $index = $index + 1; $x = (int) 1; $y = (int) ($index * $lineSize) - $fontSize; imagestring($image, 1, $x, $y, $line, $textColor); if ($index * $lineSize >= $maxY) { break; } } $image = new \OC_Image($image); return $image->valid() ? $image : false; }
public function getThumbnail($path) { $gallery_path = \OCP\Config::getSystemValue('datadirectory') . '/' . \OC_User::getUser() . '/gallery'; if (file_exists($gallery_path . $path)) { return new \OC_Image($gallery_path . $path); } if (!\OC_Filesystem::file_exists($path)) { \OC_Log::write(self::TAG, 'File ' . $path . ' don\'t exists', \OC_Log::WARN); return false; } $image = new \OC_Image(); $image->loadFromFile(\OC_Filesystem::getLocalFile($path)); if (!$image->valid()) { return false; } $image->fixOrientation(); $ret = $image->preciseResize(floor(150 * $image->width() / $image->height()), 150); if (!$ret) { \OC_Log::write(self::TAG, 'Couldn\'t resize image', \OC_Log::ERROR); unset($image); return false; } $image->save($gallery_path . '/' . $path); return $image; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { try { $svg = new Imagick(); $svg->setBackgroundColor(new \ImagickPixel('transparent')); $content = stream_get_contents($fileview->fopen($path, 'r')); if (substr($content, 0, 5) !== '<?xml') { $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content; } // Do not parse SVG files with references if (stripos($content, 'xlink:href') !== false) { return false; } $svg->readImageBlob($content); $svg->setImageFormat('png32'); } catch (\Exception $e) { \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); return false; } //new image object $image = new \OC_Image(); $image->loadFromData($svg); //check if image object is valid return $image->valid() ? $image : false; }
/** * sets the users avatar * @param \OC_Image|resource|string $data OC_Image, imagedata or path to set a new avatar * @throws Exception if the provided file is not a jpg or png image * @throws Exception if the provided image is not valid * @throws \OC\NotSquareException if the image is not square * @return void */ public function set($data) { if ($data instanceof OC_Image) { $img = $data; $data = $img->data(); } else { $img = new OC_Image($data); } $type = substr($img->mimeType(), -3); if ($type === 'peg') { $type = 'jpg'; } if ($type !== 'jpg' && $type !== 'png') { $l = \OC_L10N::get('lib'); throw new \Exception($l->t("Unknown filetype")); } if (!$img->valid()) { $l = \OC_L10N::get('lib'); throw new \Exception($l->t("Invalid image")); } if (!($img->height() === $img->width())) { throw new \OC\NotSquareException(); } $this->view->unlink('avatar.jpg'); $this->view->unlink('avatar.png'); $this->view->file_put_contents('avatar.' . $type, $data); }
/** * {@inheritDoc} */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { //get fileinfo $fileInfo = $fileview->getFileInfo($path); if (!$fileInfo) { return false; } $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); $size = $fileInfo->getSize(); if ($maxSizeForImages !== -1 && $size > $maxSizeForImages * 1024 * 1024) { return false; } $image = new \OC_Image(); $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); if ($useTempFile) { $fileName = $fileview->toTmpFile($path); } else { $fileName = $fileview->getLocalFile($path); } $image->loadFromFile($fileName); $image->fixOrientation(); if ($useTempFile) { unlink($fileName); } if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); return $image; } return false; }
public static function getThumbnail($image_name, $owner = null) { if (!$owner) { $owner = OCP\USER::getUser(); } $save_dir = OCP\Config::getSystemValue("datadirectory") . '/' . $owner . '/gallery/'; $save_dir .= dirname($image_name) . '/'; $image_path = $image_name; $thumb_file = $save_dir . basename($image_name); if (!is_dir($save_dir)) { mkdir($save_dir, 0777, true); } if (file_exists($thumb_file)) { $image = new OC_Image($thumb_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->centerCrop(200); $image->fixOrientation(); $image->save($thumb_file); } } if ($image->valid()) { return $image; } else { $image->destroy(); } return null; }
/** * {@inheritDoc} */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $this->initCmd(); if (is_null($this->cmd)) { return false; } $absPath = $fileview->toTmpFile($path); $tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); $defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to pdf --outdir '; $clParameters = \OCP\Config::getSystemValue('preview_office_cl_parameters', $defaultParameters); $exec = $this->cmd . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath); shell_exec($exec); //create imagick object from pdf $pdfPreview = null; try { list($dirname, , , $filename) = array_values(pathinfo($absPath)); $pdfPreview = $dirname . '/' . $filename . '.pdf'; $pdf = new \imagick($pdfPreview . '[0]'); $pdf->setImageFormat('jpg'); } catch (\Exception $e) { unlink($absPath); unlink($pdfPreview); \OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::ERROR); return false; } $image = new \OC_Image(); $image->loadFromData($pdf); unlink($absPath); unlink($pdfPreview); if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); return $image; } return false; }
/** * Generates a default image when the file has no cover * * @return bool|\OCP\IImage false if the default image is missing or invalid */ private function getNoCoverThumbnail() { $icon = \OC::$SERVERROOT . '/core/img/filetypes/audio.png'; if (!file_exists($icon)) { return false; } $image = new \OC_Image(); $image->loadFromFile($icon); return $image->valid() ? $image : false; }
public function testValid() { $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/testimage.png'); $this->assertTrue($img->valid()); $text = base64_encode("Lorem ipsum dolor sir amet …"); $img = new \OC_Image($text); $this->assertFalse($img->valid()); $img = new \OC_Image(null); $this->assertFalse($img->valid()); }
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 getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { //get fileinfo $fileInfo = $fileview->getFileInfo($path); if (!$fileInfo) { return false; } $image = new \OC_Image(); //check if file is encrypted if ($fileInfo['encrypted'] === true) { $image->loadFromData(stream_get_contents($fileview->fopen($path, 'r'))); } else { $image->loadFromFile($fileview->getLocalFile($path)); } return $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { //get fileinfo $fileInfo = $fileview->getFileInfo($path); if (!$fileInfo) { return false; } $image = new \OC_Image(); if ($fileInfo['encrypted'] === true) { $fileName = $fileview->toTmpFile($path); } else { $fileName = $fileview->getLocalFile($path); } $image->loadFromFile($fileName); return $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { //get fileinfo $fileInfo = $fileview->getFileInfo($path); if (!$fileInfo) { return false; } $absPath = $fileview->toTmpFile($path); $epub = new \OCA\Files_Opds\Epub($absPath); $cover = $epub->Cover(); if ($cover) { $image = new \OC_Image(); $image->loadFromData($cover['data']); } return $cover !== null && $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $tmpPath = $fileview->toTmpFile($path); //create imagick object from TIFF try { $tiff = new Imagick($tmpPath); $tiff->setImageFormat('png'); } catch (\Exception $e) { \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); return false; } unlink($tmpPath); //new image object $image = new \OC_Image($tiff); //check if image object is valid return $image->valid() ? $image : false; }
/** * @param int $maxX * @param int $maxY * @param string $absPath * @param int $second * @return bool|\OC_Image */ private function generateThumbNail($maxX, $maxY, $absPath, $second) { $tmpPath = \OC_Helper::tmpFile(); if (self::$avconvBinary) { $cmd = self::$avconvBinary . ' -an -y -ss ' . escapeshellarg($second) . ' -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1 -vsync 1 ' . escapeshellarg($tmpPath) . ' > /dev/null 2>&1'; } else { $cmd = self::$ffmpegBinary . ' -y -ss ' . escapeshellarg($second) . ' -i ' . escapeshellarg($absPath) . ' -f mjpeg -vframes 1' . ' -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) . ' ' . escapeshellarg($tmpPath) . ' > /dev/null 2>&1'; } exec($cmd, $output, $returnCode); if ($returnCode === 0) { $image = new \OC_Image(); $image->loadFromFile($tmpPath); unlink($tmpPath); return $image->valid() ? $image : false; } unlink($tmpPath); return false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $tmpPath = $fileview->toTmpFile($path); //create imagick object from bitmap or vector file try { // Layer 0 contains either the bitmap or // a flat representation of all vector layers $bp = new Imagick($tmpPath . '[0]'); $bp->setImageFormat('png'); } catch (\Exception $e) { \OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR); return false; } unlink($tmpPath); //new bitmap image object $image = new \OC_Image($bp); //check if image object is valid return $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { require_once 'PHPExcel/Classes/PHPExcel.php'; require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php'; $absPath = $fileview->toTmpFile($path); $tmpPath = \OC_Helper::tmpFile(); $rendererName = \PHPExcel_Settings::PDF_RENDERER_DOMPDF; $rendererLibraryPath = \OC::$THIRDPARTYROOT . '/3rdparty/dompdf'; \PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath); $phpexcel = new \PHPExcel($absPath); $excel = \PHPExcel_IOFactory::createWriter($phpexcel, 'PDF'); $excel->save($tmpPath); $pdf = new \imagick($tmpPath . '[0]'); $pdf->setImageFormat('jpg'); unlink($absPath); unlink($tmpPath); $image = new \OC_Image(); $image->loadFromData($pdf); return $image->valid() ? $image : false; }
/** * {@inheritDoc} */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $tmpPath = $fileview->toTmpFile($path); if (!$tmpPath) { return false; } // Creates \Imagick object from bitmap or vector file try { $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); } catch (\Exception $e) { \OCP\Util::writeLog('core', 'ImageMagick says: ' . $e->getmessage(), \OCP\Util::ERROR); return false; } unlink($tmpPath); //new bitmap image object $image = new \OC_Image(); $image->loadFromData($bp); //check if image object is valid return $image->valid() ? $image : false; }
public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { //get fileinfo $fileInfo = $fileview->getFileInfo($path); if (!$fileInfo) { return false; } $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); $size = $fileInfo->getSize(); if ($maxSizeForImages !== -1 && $size > $maxSizeForImages * 1024 * 1024) { return false; } $image = new \OC_Image(); if ($fileInfo['encrypted'] === true) { $fileName = $fileview->toTmpFile($path); } else { $fileName = $fileview->getLocalFile($path); } $image->loadFromFile($fileName); return $image->valid() ? $image : false; }
/** * {@inheritDoc} */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $content = $fileview->fopen($path, 'r'); $content = stream_get_contents($content, 3000); //don't create previews of empty text files if (trim($content) === '') { return false; } $lines = preg_split("/\r\n|\n|\r/", $content); $fontSize = $maxX ? (int) (5 / 36 * $maxX) : 5; //5px $lineSize = ceil($fontSize * 1.25); $image = imagecreate($maxX, $maxY); imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); $fontFile = __DIR__; $fontFile .= '/../../../core'; $fontFile .= '/fonts/OpenSans-Regular.ttf'; $canUseTTF = function_exists('imagettftext'); foreach ($lines as $index => $line) { $index = $index + 1; $x = (int) 1; $y = (int) ($index * $lineSize); if ($canUseTTF === true) { imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); } else { $y -= $fontSize; imagestring($image, 1, $x, $y, $line, $textColor); } if ($index * $lineSize >= $maxY) { break; } } $image = new \OC_Image($image); return $image->valid() ? $image : false; }
/** * return a preview of a file * @return \OC_Image */ public function getPreview() { if (!is_null($this->preview) && $this->preview->valid()) { return $this->preview; } $this->preview = null; $file = $this->getFile(); $maxX = $this->getMaxX(); $maxY = $this->getMaxY(); $scalingUp = $this->getScalingUp(); $fileInfo = $this->getFileInfo($file); if ($fileInfo === null || $fileInfo === false) { return new \OC_Image(); } $fileId = $fileInfo->getId(); $cached = $this->isCached($fileId); if ($cached) { $stream = $this->userView->fopen($cached, 'r'); $this->preview = null; if ($stream) { $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; $this->resizeAndCrop(); fclose($stream); } } if (is_null($this->preview)) { $preview = null; foreach (self::$providers as $supportedMimeType => $provider) { if (!preg_match($supportedMimeType, $this->mimeType)) { continue; } \OC_Log::write('core', 'Generating preview for "' . $file . '" with "' . get_class($provider) . '"', \OC_Log::DEBUG); /** @var $provider Provider */ $preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView); if (!$preview instanceof \OC_Image) { continue; } $this->preview = $preview; $this->resizeAndCrop(); $previewPath = $this->getPreviewPath($fileId); $cachePath = $this->buildCachePath($fileId); if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) { $this->userView->mkdir($this->getThumbnailsFolder() . '/'); } if ($this->userView->is_dir($previewPath) === false) { $this->userView->mkdir($previewPath); } $this->userView->file_put_contents($cachePath, $preview->data()); break; } } if (is_null($this->preview)) { $this->preview = new \OC_Image(); } return $this->preview; }
/** * @brief return a preview of a file * @return \OC_Image */ public function getPreview() { if (!is_null($this->preview) && $this->preview->valid()) { return $this->preview; } $this->preview = null; $file = $this->getFile(); $maxX = $this->getMaxX(); $maxY = $this->getMaxY(); $scalingUp = $this->getScalingUp(); $fileInfo = $this->fileView->getFileInfo($file); $fileId = $fileInfo['fileid']; $cached = $this->isCached(); if ($cached) { $image = new \OC_Image($this->userView->file_get_contents($cached, 'r')); $this->preview = $image->valid() ? $image : null; $this->resizeAndCrop(); } if (is_null($this->preview)) { $preview = null; foreach (self::$providers as $supportedMimetype => $provider) { if (!preg_match($supportedMimetype, $this->mimetype)) { continue; } \OC_Log::write('core', 'Generating preview for "' . $file . '" with "' . get_class($provider) . '"', \OC_Log::DEBUG); $preview = $provider->getThumbnail($file, $maxX, $maxY, $scalingUp, $this->fileView); if (!$preview instanceof \OC_Image) { continue; } $this->preview = $preview; $this->resizeAndCrop(); $previewPath = $this->getThumbnailsFolder() . '/' . $fileId . '/'; $cachePath = $previewPath . $maxX . '-' . $maxY . '.png'; if ($this->userView->is_dir($this->getThumbnailsFolder() . '/') === false) { $this->userView->mkdir($this->getThumbnailsFolder() . '/'); } if ($this->userView->is_dir($previewPath) === false) { $this->userView->mkdir($previewPath); } $this->userView->file_put_contents($cachePath, $preview->data()); break; } } if (is_null($this->preview)) { $this->preview = new \OC_Image(); } return $this->preview; }
/** * @NoAdminRequired * * @param string $path * @return DataResponse */ public function postAvatar($path) { $userId = $this->userSession->getUser()->getUID(); $files = $this->request->getUploadedFile('files'); if (isset($path)) { $path = stripslashes($path); $view = new \OC\Files\View('/' . $userId . '/files'); if ($view->filesize($path) > 20 * 1024 * 1024) { return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]], Http::STATUS_BAD_REQUEST); } $fileName = $view->getLocalFile($path); } elseif (!is_null($files)) { if ($files['error'][0] === 0 && is_uploaded_file($files['tmp_name'][0]) && !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])) { if ($files['size'][0] > 20 * 1024 * 1024) { return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]], Http::STATUS_BAD_REQUEST); } $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); $view = new \OC\Files\View('/' . $userId . '/cache'); $fileName = $view->getLocalFile('avatar_upload'); unlink($files['tmp_name'][0]); } else { return new DataResponse(['data' => ['message' => $this->l->t('Invalid file provided')]], Http::STATUS_BAD_REQUEST); } } else { //Add imgfile return new DataResponse(['data' => ['message' => $this->l->t('No image or file provided')]], Http::STATUS_BAD_REQUEST); } try { $image = new \OC_Image(); $image->loadFromFile($fileName); $image->fixOrientation(); if ($image->valid()) { $mimeType = $image->mimeType(); if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { return new DataResponse(['data' => ['message' => $this->l->t('Unknown filetype')]]); } $this->cache->set('tmpAvatar', $image->data(), 7200); return new DataResponse(['data' => 'notsquare']); } else { return new DataResponse(['data' => ['message' => $this->l->t('Invalid image')]]); } } catch (\Exception $e) { return new DataResponse(['data' => ['message' => $e->getMessage()]]); } }
/** * Retrieves the preview from the cache and resizes it if necessary * * @param int $fileId fileId of the original image * @param string $cached the path to the cached preview */ private function getCachedPreview($fileId, $cached) { $stream = $this->userView->fopen($cached, 'r'); $this->preview = null; if ($stream) { $image = new \OC_Image(); $image->loadFromFileHandle($stream); $this->preview = $image->valid() ? $image : null; if (!is_null($this->preview)) { // Size of the preview we calculated $maxX = $this->previewWidth; $maxY = $this->previewHeight; // Size of the preview we retrieved from the cache $previewX = (int) $this->preview->width(); $previewY = (int) $this->preview->height(); // We don't have an exact match if ($previewX !== $maxX || $previewY !== $maxY) { $this->resizeAndStore($fileId); } } fclose($stream); } }
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; }
/** * @NoAdminRequired * * @param string $path * @return DataResponse */ public function postAvatar($path) { $userId = $this->userSession->getUser()->getUID(); $files = $this->request->getUploadedFile('files'); $headers = []; if ($this->request->isUserAgent([\OC\AppFramework\Http\Request::USER_AGENT_IE_8])) { // due to upload iframe workaround, need to set content-type to text/plain $headers['Content-Type'] = 'text/plain'; } if (isset($path)) { $path = stripslashes($path); $node = $this->userFolder->get($path); if (!$node instanceof \OCP\Files\File) { return new DataResponse(['data' => ['message' => $this->l->t('Please select a file.')]], Http::STATUS_OK, $headers); } if ($node->getSize() > 20 * 1024 * 1024) { return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]], Http::STATUS_BAD_REQUEST, $headers); } $content = $node->getContent(); } elseif (!is_null($files)) { if ($files['error'][0] === 0 && is_uploaded_file($files['tmp_name'][0]) && !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])) { if ($files['size'][0] > 20 * 1024 * 1024) { return new DataResponse(['data' => ['message' => $this->l->t('File is too big')]], Http::STATUS_BAD_REQUEST, $headers); } $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); $content = $this->cache->get('avatar_upload'); unlink($files['tmp_name'][0]); } else { return new DataResponse(['data' => ['message' => $this->l->t('Invalid file provided')]], Http::STATUS_BAD_REQUEST, $headers); } } else { //Add imgfile return new DataResponse(['data' => ['message' => $this->l->t('No image or file provided')]], Http::STATUS_BAD_REQUEST, $headers); } try { $image = new \OC_Image(); $image->loadFromData($content); $image->fixOrientation(); if ($image->valid()) { $mimeType = $image->mimeType(); if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { return new DataResponse(['data' => ['message' => $this->l->t('Unknown filetype')]], Http::STATUS_OK, $headers); } $this->cache->set('tmpAvatar', $image->data(), 7200); return new DataResponse(['data' => 'notsquare'], Http::STATUS_OK, $headers); } else { return new DataResponse(['data' => ['message' => $this->l->t('Invalid image')]], Http::STATUS_OK, $headers); } } catch (\Exception $e) { $this->logger->logException($e, ['app' => 'core']); return new DataResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK, $headers); } }