Пример #1
0
 /**
  * @NoAdminRequired
  */
 public function uploadPhoto()
 {
     //$type = $this->request->getHeader('Content-Type');
     $id = $this->params('id');
     $file = $this->request->getUploadedFile('imagefile');
     $error = $file['error'];
     if ($error !== UPLOAD_ERR_OK) {
         $errors = array(0 => $this->l10n->t("There is no error, the file uploaded with success"), 1 => $this->l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini") . ini_get('upload_max_filesize'), 2 => $this->l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"), 3 => $this->l10n->t("The uploaded file was only partially uploaded"), 4 => $this->l10n->t("No file was uploaded"), 6 => $this->l10n->t("Missing a temporary folder"));
         \OCP\Util::writeLog($this->appName, 'Uploaderror: ' . $errors[$error], \OCP\Util::DEBUG);
     }
     if (file_exists($file['tmp_name'])) {
         $tmpkey = 'editphoto';
         $size = getimagesize($file['tmp_name'], $info);
         //$exif = @exif_read_data($file['tmp_name']);
         $image = new \OCP\Image();
         if ($image->loadFromFile($file['tmp_name'])) {
             if (!$image->fixOrientation()) {
                 // No fatal error so we don't bail out.
                 \OCP\Util::writeLog($this->appName, 'Couldn\'t save correct image orientation: ' . $tmpkey, \OCP\Util::DEBUG);
             }
             \OC::$server->getCache()->remove($tmpkey);
             \OC::$server->getCache()->remove($tmpkey . 'ratio');
             $originalWidth = $image->width();
             if (\OC::$server->getCache()->set($tmpkey, $image->data(), 600)) {
                 if ($image->width() > 400 || $image->height() > 400) {
                     $image->resize(400);
                     // Prettier resizing than with browser and saves bandwidth.
                 }
                 $ratio = $originalWidth / $image->width();
                 if (\OC::$server->getCache()->set($tmpkey . 'ratio', $ratio, 600)) {
                     $imgString = $image->__toString();
                     $resultData = array('mime' => $file['type'], 'size' => $file['size'], 'name' => $file['name'], 'id' => $id, 'tmp' => $tmpkey, 'imgdata' => $imgString);
                     $response = new JSONResponse();
                     $response->setData($resultData);
                     return $response;
                 }
             }
         }
     }
 }
 /**
  * Saves the photo from ownCloud FS to oC cache
  * @return JSONResponse with data.tmp set to the key in the cache.
  *
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function cacheFileSystemPhoto()
 {
     $params = $this->request->urlParams;
     $response = new JSONResponse();
     if (!isset($this->request->get['path'])) {
         $response->bailOut(App::$l10n->t('No photo path was submitted.'));
     }
     $localpath = \OC\Files\Filesystem::getLocalFile($this->request->get['path']);
     $tmpkey = 'contact-photo-' . $params['contactId'];
     if (!file_exists($localpath)) {
         return $response->bailOut(App::$l10n->t('File doesn\'t exist:') . $localpath);
     }
     $image = new \OCP\Image();
     if (!$image) {
         return $response->bailOut(App::$l10n->t('Error loading image.'));
     }
     if (!$image->loadFromFile($localpath)) {
         return $response->bailOut(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.
         $response->debug('Couldn\'t save correct image orientation: ' . $localpath);
     }
     if (!$this->server->getCache()->set($tmpkey, $image->data(), 600)) {
         return $response->bailOut('Couldn\'t save temporary image: ' . $tmpkey);
     }
     return $response->setData(array('tmp' => $tmpkey, 'metadata' => array('contactId' => $params['contactId'], 'addressBookId' => $params['addressBookId'], 'backend' => $params['backend'])));
 }
Пример #3
0
 /**
  * Generate thumbnail of an Image with GD
  * @param string $viewPath Source view path
  * @param string $srcImagePath Source image path relative to the ownCloud fakeroot
  * @param string $dstImagePath Destination image path
  * @return boolean TRUE image generated successfully, FALSE otherwise
  */
 private function generateImageThumbnailGD($viewPath, $srcImagePath, $dstImagePath)
 {
     $view = new \OC\Files\View($viewPath);
     $imageLocalPath = $view->getLocalFile($srcImagePath);
     $image = new \OCP\Image();
     $image->loadFromFile($imageLocalPath);
     if (!$image->valid()) {
         return FALSE;
     }
     //Non legge il path
     $image->fixOrientation();
     $image->resize($this->width);
     $imageRsrc = $image->resource();
     $height = $image->height();
     $width = $image->width();
     $widthOffset = intval(($this->width - $width) / 2);
     $heightOffset = intval(($this->height - $height) / 2);
     $thumbGDImage = imagecreatetruecolor($this->width, $this->height);
     // Fill with background color
     $bgColor = imagecolorallocate($thumbGDImage, $this->bgColor['red'], $this->bgColor['green'], $this->bgColor['blue']);
     imagefilledrectangle($thumbGDImage, 0, 0, $this->width, $this->height, $bgColor);
     imagecopyresampled($thumbGDImage, $imageRsrc, $widthOffset, $heightOffset, 0, 0, $width, $height, $width, $height);
     imagepng($thumbGDImage, $dstImagePath, 7);
     imagedestroy($thumbGDImage);
     return TRUE;
 }