Example #1
0
 /**
  * @brief Loads an image from a local file.
  * @param $imageref The path to a local file.
  * @returns An image resource or false on error
  */
 public function loadFromFile($imagePath = false)
 {
     // exif_imagetype throws "read error!" if file is less than 12 byte
     if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
         // Debug output disabled because this method is tried before loadFromBase64?
         OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: ' . $imagePath, OC_Log::DEBUG);
         return false;
     }
     $iType = exif_imagetype($imagePath);
     switch ($iType) {
         case IMAGETYPE_GIF:
             if (imagetypes() & IMG_GIF) {
                 $this->resource = imagecreatefromgif($imagePath);
             } else {
                 OC_Log::write('core', 'OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, OC_Log::DEBUG);
             }
             break;
         case IMAGETYPE_JPEG:
             if (imagetypes() & IMG_JPG) {
                 $this->resource = imagecreatefromjpeg($imagePath);
             } else {
                 OC_Log::write('core', 'OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, OC_Log::DEBUG);
             }
             break;
         case IMAGETYPE_PNG:
             if (imagetypes() & IMG_PNG) {
                 $this->resource = imagecreatefrompng($imagePath);
             } else {
                 OC_Log::write('core', 'OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, OC_Log::DEBUG);
             }
             break;
         case IMAGETYPE_XBM:
             if (imagetypes() & IMG_XPM) {
                 $this->resource = imagecreatefromxbm($imagePath);
             } else {
                 OC_Log::write('core', 'OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, OC_Log::DEBUG);
             }
             break;
         case IMAGETYPE_WBMP:
             if (imagetypes() & IMG_WBMP) {
                 $this->resource = imagecreatefromwbmp($imagePath);
             } else {
                 OC_Log::write('core', 'OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, OC_Log::DEBUG);
             }
             break;
         case IMAGETYPE_BMP:
             $this->resource = $this->imagecreatefrombmp($imagePath);
             break;
             /*
             case IMAGETYPE_TIFF_II: // (intel byte order)
             	break;
             case IMAGETYPE_TIFF_MM: // (motorola byte order)
             	break;
             case IMAGETYPE_JPC:
             	break;
             case IMAGETYPE_JP2:
             	break;
             case IMAGETYPE_JPX:
             	break;
             case IMAGETYPE_JB2:
             	break;
             case IMAGETYPE_SWC:
             	break;
             case IMAGETYPE_IFF:
             	break;
             case IMAGETYPE_ICO:
             	break;
             case IMAGETYPE_SWF:
             	break;
             case IMAGETYPE_PSD:
             	break;
             */
         /*
         case IMAGETYPE_TIFF_II: // (intel byte order)
         	break;
         case IMAGETYPE_TIFF_MM: // (motorola byte order)
         	break;
         case IMAGETYPE_JPC:
         	break;
         case IMAGETYPE_JP2:
         	break;
         case IMAGETYPE_JPX:
         	break;
         case IMAGETYPE_JB2:
         	break;
         case IMAGETYPE_SWC:
         	break;
         case IMAGETYPE_IFF:
         	break;
         case IMAGETYPE_ICO:
         	break;
         case IMAGETYPE_SWF:
         	break;
         case IMAGETYPE_PSD:
         	break;
         */
         default:
             // this is mostly file created from encrypted file
             $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
             $iType = IMAGETYPE_PNG;
             OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG);
             break;
     }
     if ($this->valid()) {
         $this->imageType = $iType;
         $this->mimeType = image_type_to_mime_type($iType);
         $this->filePath = $imagePath;
     }
     return $this->resource;
 }
 /**
  * return path to file which reflects one visible in browser
  *
  * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem
  * @param string $path
  * @return string
  */
 public static function getLocalPath($path)
 {
     return \OC\Files\Filesystem::getLocalPath($path);
 }
Example #3
0
 /**
  * Loads an image from a local file.
  *
  * @param bool|string $imagePath The path to a local file.
  * @return bool|resource An image resource or false on error
  */
 public function loadFromFile($imagePath = false)
 {
     // exif_imagetype throws "read error!" if file is less than 12 byte
     if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
         return false;
     }
     $iType = exif_imagetype($imagePath);
     switch ($iType) {
         case IMAGETYPE_GIF:
             if (imagetypes() & IMG_GIF) {
                 $this->resource = imagecreatefromgif($imagePath);
                 // Preserve transparency
                 imagealphablending($this->resource, true);
                 imagesavealpha($this->resource, true);
             } else {
                 $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
             }
             break;
         case IMAGETYPE_JPEG:
             if (imagetypes() & IMG_JPG) {
                 $this->resource = imagecreatefromjpeg($imagePath);
             } else {
                 $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
             }
             break;
         case IMAGETYPE_PNG:
             if (imagetypes() & IMG_PNG) {
                 $this->resource = imagecreatefrompng($imagePath);
                 // Preserve transparency
                 imagealphablending($this->resource, true);
                 imagesavealpha($this->resource, true);
             } else {
                 $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
             }
             break;
         case IMAGETYPE_XBM:
             if (imagetypes() & IMG_XPM) {
                 $this->resource = imagecreatefromxbm($imagePath);
             } else {
                 $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
             }
             break;
         case IMAGETYPE_WBMP:
             if (imagetypes() & IMG_WBMP) {
                 $this->resource = imagecreatefromwbmp($imagePath);
             } else {
                 $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
             }
             break;
         case IMAGETYPE_BMP:
             $this->resource = $this->imagecreatefrombmp($imagePath);
             break;
             /*
             case IMAGETYPE_TIFF_II: // (intel byte order)
             	break;
             case IMAGETYPE_TIFF_MM: // (motorola byte order)
             	break;
             case IMAGETYPE_JPC:
             	break;
             case IMAGETYPE_JP2:
             	break;
             case IMAGETYPE_JPX:
             	break;
             case IMAGETYPE_JB2:
             	break;
             case IMAGETYPE_SWC:
             	break;
             case IMAGETYPE_IFF:
             	break;
             case IMAGETYPE_ICO:
             	break;
             case IMAGETYPE_SWF:
             	break;
             case IMAGETYPE_PSD:
             	break;
             */
         /*
         case IMAGETYPE_TIFF_II: // (intel byte order)
         	break;
         case IMAGETYPE_TIFF_MM: // (motorola byte order)
         	break;
         case IMAGETYPE_JPC:
         	break;
         case IMAGETYPE_JP2:
         	break;
         case IMAGETYPE_JPX:
         	break;
         case IMAGETYPE_JB2:
         	break;
         case IMAGETYPE_SWC:
         	break;
         case IMAGETYPE_IFF:
         	break;
         case IMAGETYPE_ICO:
         	break;
         case IMAGETYPE_SWF:
         	break;
         case IMAGETYPE_PSD:
         	break;
         */
         default:
             // this is mostly file created from encrypted file
             $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
             $iType = IMAGETYPE_PNG;
             $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
             break;
     }
     if ($this->valid()) {
         $this->imageType = $iType;
         $this->mimeType = image_type_to_mime_type($iType);
         $this->filePath = $imagePath;
     }
     return $this->resource;
 }
 /**
  * Compile Latex File
  *
  * @NoAdminRequired
  *
  * @param string $path
  * @param string $filename
  * @param string $compiler
  * @return DataResponse
  */
 public function doCompile($path, $filename, $compiler)
 {
     $success = 'success';
     $error = 'error';
     try {
         set_time_limit(0);
         //scanning can take ages
         // If they've set the compiler to something other than an allowable option....
         if (!($compiler === 'xelatex' || $compiler === 'pdflatex' || $compiler === 'latex')) {
             $compiler = 'latex';
         }
         // The real directory file
         $workdir = dirname(\OC\Files\Filesystem::getLocalFile(stripslashes($path) . $filename));
         $info = pathinfo($filename);
         $fileext = '.' . $info['extension'];
         $projectname = trim(basename($filename, $fileext));
         $pdffile = $projectname . '.pdf';
         $dvifile = $projectname . '.dvi';
         $psfile = $projectname . '.ps';
         $tocfile = $projectname . '.toc';
         $logfile = $projectname . '.log';
         $bibfile = $projectname;
         // Bibtex File is without extension
         // As we will write pdf/ps file(s) in the $path, we need to known if it's writable
         if (!\OC\Files\Filesystem::isCreatable(stripslashes($path))) {
             return new JSONResponse(array('data' => array('message' => 'As you don\'t have write permission in the owner directory, it\'s not possible to create output latex files.', 'output' => '')), Http::STATUS_BAD_REQUEST);
         }
         // Command to jump into directory
         $cd_command = "cd " . str_replace(' ', '\\ ', trim($workdir));
         // PDFLatex command preparation
         if ($compiler == 'xelatex' || $compiler == 'pdflatex') {
             $latex_command = $compiler . ' ' . $filename;
             $bibtex_command = 'bibtex ' . $bibfile;
         } else {
             $latex_command = "latex -output-directory={$outpath}  {$filename} ; cd {$outpath}; dvips  {$dvifile} ; ps2pdf {$psfile}";
         }
         $output = "<b>========BEGIN COMPILE========</b>\n{$psfile} \n";
         $output .= $cd_command . "\n";
         $output .= getcwd() . "\n";
         // First Compile
         $output .= shell_exec($cd_command . " && pwd");
         $return = shell_exec($cd_command . " && " . $latex_command);
         $output .= getcwd() . "\n";
         // For BibTeX
         if ($compiler == 'pdflatex') {
             // Second compile step with bibtex
             $return .= shell_exec($cd_command . " && " . $bibtex_command);
             // compile again after bibtex
             $return .= shell_exec($cd_command . " && " . $latex_command);
         }
         $logfile = $workdir . '/' . $logfile;
         $log = file_get_contents($logfile);
         while (preg_match('/Return to get cross-references right/', $log) || preg_match('/No file ' . $tocfile . '/', $log)) {
             $return .= shell_exec($cd_command . " && " . $this->latex_command);
             $log = file_get_contents($logfile);
         }
         // ! at begining of a line indicate an error!
         $errors = preg_grep("/^!/", explode("\n", $log));
         if (empty($errors) === false) {
             $log_array = explode("\n", $log);
             $error = "\n";
             foreach ($errors as $line => $msg) {
                 for ($i = $line; $i <= $line + 5; $i++) {
                     $error .= $log_array[$i] . "\n";
                 }
             }
             return new JSONResponse(array('data' => array('message' => $this->l->t('Compile failed with errors') . ' - <br/>', 'output' => nl2br($output . " % " . $latex_command . "\n" . $error)), 'status' => $error), Http::STATUS_OK);
         }
         // No PDF File !?
         if (!file_exists($workdir . '/' . $pdffile)) {
             return new JSONResponse(array('data' => array('message' => $this->l->t('Compile failed with errors') . ':<br/>', 'output' => nl2br($output . " % " . $latex_command . "\n" . file_get_contents($outpath . '/' . $logfile))), 'status' => $error), Http::STATUS_OK);
         }
         $output .= $return;
         $output .= "\n========END COMPILE==========\n";
         $oc_workdir = stripslashes(\OC\Files\Filesystem::getLocalPath($workdir));
         $target = \OCP\Files::buildNotExistingFileName($oc_workdir, $pdffile);
         $target = \OC\Files\Filesystem::normalizePath($target);
         $meta = \OC\Files\Filesystem::getFileInfo($target);
         if ($compiler === 'latex') {
             $target = \OCP\Files::buildNotExistingFileName($oc_workdir, $psfile);
             $target = \OC\Files\Filesystem::normalizePath($target);
             $meta = \OC\Files\Filesystem::getFileInfo($target);
         }
         return new JSONResponse(array('data' => array('output' => nl2br($output), 'path' => $path, 'pdffile' => $pdffile, 'psfile' => $psfile, 'logfile' => $logfile), 'status' => $success), Http::STATUS_OK);
     } catch (\Exception $e) {
         return new DataResponse(['message' => $e], Http::STATUS_BAD_REQUEST);
     }
 }