/** * adds css files to self::$css * * @param string $key * @param string $path path to included file * @param string $path path to included file within given module/component */ public static function addCssFile($key, $path, $srcPathsuffix = null) { $key = Basic::addLastSlash($key) . '||' . $srcPathsuffix; if (!isset(self::$css[$key])) { self::$css[$key] = array(); } if (!in_array($path, self::$css[$key])) { self::$css[$key][] = $path; } }
/** * save $img in $dirname named $filename * * @param Image * @param string destination to store image to * @param string file name * @param bool overwrite older file with given $filename ? * @return string filename as file was stored (potential dangerous chars replaced) * @throws NotImageException */ public static function save($img, $dirname, $filename = null, $overwriteOldFile = true) { if (!$img instanceof Image) { throw new NotImageException('Argument "$img" must be instance of Image, "' . get_class($img) . '" given.'); } $dirname = Basic::addLastSlash($dirname); Basic::mkdir($dirname); if (is_null($filename)) { $filename = $img->name; } $filename = self::handleFilename($filename); if (!$overwriteOldFile) { $filename = self::getUniqueFilename($dirname, null, $filename); } $dest = $dirname . $filename; // PNG does not save alpha channel (transparency) by default, needs to be turned on explicitly $img->saveAlpha(true); $img->save($dest, self::$quality); return $filename; }
/** * return unique filename having $suffix within $dirname * * @param string path within which filename must be unique * @param string suffix of filename * @param string filename * @return string */ public static function getUniqueFilename($dirname, $suffix = null, $filename = null) { if ($suffix === null && ($filename === null or strpos($filename, '.') === false)) { throw new ArgumentOutOfRangeException('At least one of \\$suffix or \\$filename must be set'); } if ($suffix === null) { $suffix = self::getSuffix($filename); } // if only base filename given [without suffix] append it if (!self::hasSuffix($filename, $suffix)) { $filename .= ".{$suffix}"; } $dirname = Basic::addLastSlash($dirname); while (is_null($filename) or file_exists($dirname . $filename)) { $filename = self::handleFilename(uniqid() . '.' . $suffix); } return $filename; }
/** * copy $path to webmodulePublicDir/$path * @param string * @param bool is copied file mandatory? */ protected function copy($path, $need = true) { $dest = Basic::addLastSlash(self::$webloaderDestPath) . $path; $src = Basic::addLastSlash(self::$webloaderSrcPath) . $path; if (!file_exists($dest)) { if (file_exists($src)) { Basic::copyr($src, $dest); } elseif ($need) { throw new ArgumentOutOfRangeException("Source path '{$src}' does NOT exist"); } } }
/** * vrati pole suborov s pozadovanymi priponami * * @param string $path * @param array $suffixes * @return array */ private function getFiles($path, $suffixes) { $path = Basic::addLastSlash($path, '\\'); $ret = array(); if (($dp = @opendir($path)) != false) { while (false !== ($item = readdir($dp))) { $newPath = $path . $item; if (is_dir($newPath) and !in_array($item, array('.', '..'))) { $ret = array_merge($ret, $this->getFiles($newPath, $suffixes)); } elseif (is_file($newPath) and is_readable($newPath) and preg_match('/^.+\\.(' . implode('|', $suffixes) . ')$/i', $item)) { // echo $newPath . '<br>'; array_push($ret, $newPath); } } closedir($dp); } elseif ($this->debug) { echo "directory '{$path}' not opened"; } return $ret; }
/** * 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; }