/** * save file to $dirname and return filename as stored on ftp * * @param string * @param HttpUploadedFile * @param string filename with suffix (usually id from DB) * @param bool overwrite older file with given $filename ? * @return string */ public static function saveFile($dirname, $file, $filename = NULL, $overwriteOldFile = true) { // return if no file given [may occur only when editing item otherwise it's controlled when submitting form] if (!$file instanceof HttpUploadedFile or empty($file->name)) { return null; } $dirname = Basic::AddLastSlash($dirname); Basic::mkdir($dirname); if (is_null($filename)) { $filename = $file->name; } $filename = self::handleFilename($filename); if (!$overwriteOldFile) { $filename = self::getUniqueFilename($dirname, null, $filename); } $dest = $dirname . $filename; $file->move($dest); return $filename; }
protected function setWebloaderPaths() { // sets webloader source path to file calling this one .. $backtrace = debug_backtrace(); self::$webloaderSrcPath = $callingFile = dirname($backtrace[0]['file']); // sets uri to webtemp control dir $webtempDir = Environment::getVariable('webtempDir'); $baseUri = Environment::getVariable('baseUri'); $webtempDirSlashed = str_replace('\\', '/', $webtempDir); // $webtempDirname = substr($webtempDirSlashed, strpos($webtempDirSlashed, $baseUri) + strlen($baseUri)); $webtempDirname = substr($webtempDirSlashed, strlen(WWW_DIR) + 1); $controlName = basename(self::$webloaderSrcPath); self::$webloaderDestPath = $webtempDir . '/' . $controlName; self::$webloaderDestUri = $baseUri . $webtempDirname . '/' . $controlName; // create automatically dir for scripts even if not used $dest = $webtempDir . '/' . $controlName; Basic::mkdir($dest); Basic::mkdir($dest . '/js'); Basic::mkdir($dest . '/css'); }
private static function renderFile(&$loader, $origTempUri, $origTempPath, $key, $files) { $controlId = basename(substr($key, 0, strpos($key, '||'))); // ak je to nejaky subor z modulu [vzdy ulozeny v zlozke 'web'], tak chceme zachovat meno podla 3.parametra if ($controlId == 'web') { $dirname = substr($key, strpos($key, '||') + 2); $pathSuffix = Basic::getBaseDirName($dirname); $srcPath = str_replace(array('||', $pathSuffix . '/'), array('', ''), $key); } else { $dirname = $controlId . '/' . substr($key, strpos($key, '||') + 2); $srcPath = str_replace('||', '', $key); } $dirname = String::trim($dirname, '/'); //todo: overit, ci to nebude robit adresare zase inde..zistit, cim to je, ze to na locale ide a na ostrom serveri nie // pouzit ked tak Environment::getVariable('webtempDir') Basic::mkdir($origTempPath . '/' . $dirname); $loader->sourcePath = $srcPath; $loader->tempUri = $origTempUri . '/' . $dirname; $loader->tempPath = $origTempPath . '/' . $dirname; $loader->render($files); }
/** * make thumbnail and resize big image storing them in $dirname (and $dirname/thumb) as $filename * * @param Image * @param string * @param int * @param int * @param int * @param int * @param bool makeThumbnail for big image or just resize proportionally? * @param string * @return void * @throws NotImageException */ public static function savePreviewWithThumb($img, $dirname, $thumb_w, $thumb_h, $big_w, $big_h, $useThumbForBig = false, $filename = 'main.jpg') { if (!$img instanceof Image) { throw new NotImageException('Argument "$img" must be instance of Image, "' . get_class($img) . '" given.'); } Basic::mkdir($dirname); Basic::mkdir($dirname . '/thumb'); $dest = $dirname . '/' . $filename; $dest_thumb = $dirname . '/thumb/' . $filename; // big one $img2 = clone $img; // resize only if needed if ($img2->getWidth() > $big_w || $img2->getHeight() > $big_h) { if ($useThumbForBig) { self::makeThumbnail($img2, $big_w, $big_h); } else { $img2->resize($big_w, $big_h)->sharpen(); } } // PNG does not save alpha channel (transparency) by default, needs to be turned on explicitly $img2->saveAlpha(true); $img2->save($dest, self::$quality); self::makeThumbnail($img, $thumb_w, $thumb_h); // PNG does not save alpha channel (transparency) by default, needs to be turned on explicitly $img->saveAlpha(true); $img->save($dest_thumb, self::$quality); }
/** * make thumbnail and resize images storing them in $dirname * * @param string * @param HttpUploadedFile array * @param int * @param int * @param int * @param int * @return bool have we uploaded sth? * @throws NotImageException */ public static function saveImages($dirname, $files, $thumb_w, $thumb_h, $big_w, $big_h) { if (count($files) == 0) { return false; } $dirname = Basic::addLastSlash($dirname); $dest_big = $dirname . 'big/'; $dest_thumb = $dirname . 'thumb/'; Basic::mkdir($dest_big); Basic::mkdir($dest_thumb); $uploaded = false; // move uploaded files .. errors are handled in MyAppForm and MyFileInput already when form is submitted foreach ($files as $file) { // ak nezvoli subor, tak skusime dalsi if (!$file instanceof HttpUploadedFile or $file->error === UPLOAD_ERR_NO_FILE) { continue; } $filename = $file->getName(); if (!$file->isImage()) { throw new NotImageException('Nahrať možete iba obrázky! Poslaný súbor: ' . $file->name); } $filename = self::handleFilename($filename); $img = $file->toImage(); $img2 = clone $img; self::makeThumbnail($img, $thumb_w, $thumb_h); // PNG does not save alpha channel (transparency) by default, needs to be turned on explicitly $img->saveAlpha(true); $img->save($dest_thumb . $filename, self::$quality); // big one - proportionally $img2->resize($big_w, $big_h); // PNG does not save alpha channel (transparency) by default, needs to be turned on explicitly $img2->saveAlpha(true); $img2->save($dest_big . $filename, self::$quality); // flag we uploaded sth. $uploaded = true; } return $uploaded; }