/** This method is called when all files are uploaded * Will remove any uploaded file which isn't a real picture type */ public function pictureOrRemoveIt() { $arrFiles = MyFiles::filesFromDir($this->path->tmp); foreach ($arrFiles as $file) { $file_info = getimagesize($this->path->tmp . $file); if (empty($file_info)) { MyFiles::deleteFile($this->path->tmp, $file); } } return; }
public function thumb() { Yii::import('fbgallery.drivers.Image'); $quality = $this->conf->quality; $sharpen = $this->conf->sharpen; $imgWidth = $this->conf->imgWidth; $thWidth = $this->conf->thWidth; //create an array with filenames of files uploaded in tmp folder $arrFiles = MyFiles::filesFromDir($this->path->tmp); //select the type of resizing switch ($this->conf->thumbStyle) { case 'square': self::square($arrFiles, $quality, $sharpen, $imgWidth, $thWidth); break; case 'landscape': self::landscape($arrFiles, $quality, $sharpen, $imgWidth, $thWidth); break; case 'portrait': self::portrait($arrFiles, $quality, $sharpen, $imgWidth, $thWidth); break; } //after successful create thumbnail, we go to add new pictures filename in database operations::addImagesToDB($arrFiles); }
public function moveAllFiles($from, $to) { $files = MyFiles::filesFromDir($from); foreach ($files as $file) { if (!rename($from . $file, $to . $file)) { throw new Exception(Yii::t('app', 'Error: Couldn\'t move files! Please check permissions.')); } } }
public function actionUploader() { $this->bgImage = "images/logo32.jpg"; $this->drawMenu = false; Yii::import('application.extensions.FBGallery.MyFiles'); Yii::import('application.extensions.FBGallery.Image'); Yii::import('application.extensions.FBGallery.Uploader'); Yii::import('application.extensions.FBGallery.GalleryConfig'); $fancyBoxConfig = self::config("fancybox"); $galleryConfig = self::config("gallery"); $uploaderConfig = self::config("uploader"); self::publishJQAssets(); self::publishAssets(); $siteUrlBase = Yii::app()->request->hostInfo . Yii::app()->baseUrl . '/'; $gUrl = $siteUrlBase . $galleryConfig['gFolder'] . '/' . $this->pid; $gPath = self::getAppDir() . $galleryConfig['gFolder'] . DIRECTORY_SEPARATOR . $this->pid; $originalPath = $gPath . DIRECTORY_SEPARATOR . $galleryConfig['originalDir'] . DIRECTORY_SEPARATOR; $imgsPath = $gPath . DIRECTORY_SEPARATOR . $galleryConfig['picturesDir'] . DIRECTORY_SEPARATOR; $thPath = $gPath . DIRECTORY_SEPARATOR . $galleryConfig['thumbsDir'] . DIRECTORY_SEPARATOR; $ovPath = $gPath . DIRECTORY_SEPARATOR . $galleryConfig['overviewsDir'] . DIRECTORY_SEPARATOR; $tmpPath = $gPath . DIRECTORY_SEPARATOR . $galleryConfig['tempDir'] . DIRECTORY_SEPARATOR; $assetUrl = Yii::app()->getAssetManager()->publish(self::getFBGalleryDir() . '/assets') . '/'; //if is a new gallery if (!file_exists($tmpPath)) { $dirs = array($originalPath, $imgsPath, $thPath, $ovPath, $tmpPath); foreach ($dirs as $dir) { MyFiles::NewFolder($dir); } } $myfile = new FItem(); if (isset($_POST['FItem'])) { $myfile->attributes = $_POST['FItem']; $myfile->image = CUploadedFile::getInstance($myfile, 'image'); //get image $valid = $myfile->validate(); if ($valid) { if (isset($myfile->image)) { $name = str_replace(' ', '_', $myfile->image); //replace spaces $name = str_replace(')', '', $name); //replace ) $name = str_replace('(', '', $name); //replace ( $name = Yii::app()->session['fbid'] . "_" . $name; if (file_exists($imgsPath . $name)) { //check if it already exists then rename it $name = $name . "_" . Date("His", Time()); } //get file name with timestamp $name = strtolower($name); $myfile->image->saveAs($tmpPath . $name); //saveAs to tmp } else { $this->render('error', array('message' => 'Soubor nebyl nalezen.')); Yii::app()->end(); } } else { $this->render('error', array('message' => 'Soubor je příliš velký. ->' . $myfile->image->getSize())); Yii::app()->end(); } } //check if file exists if (!file_exists($tmpPath . $name)) { $this->render('error', array('message' => 'Soubor nebyl uložen.')); Yii::app()->end(); } $img_orig = new Image($tmpPath . $name); $imgW = $img_orig->__get('width'); $imgH = $img_orig->__get('height'); if ($imgW < $galleryConfig['imgWidth'] && $imgH < $galleryConfig['imgHeight']) { $this->render('error', array('message' => 'Fotografie je příliš malá. Rozměry musí být nejméně ' . $galleryConfig['imgWidth'] . 'x' . $galleryConfig['imgHeight'] . ' bodů.')); Yii::app()->end(); } // if( $imgH<$galleryConfig['imgHeight'] ) // { // $this->render('error', array('message'=>'Fotografie je příliš malá. Rozměry musí být nejméně ' . $galleryConfig['imgWidth'] . 'x' . $galleryConfig['imgHeight'] . ' bodů.') ); // Yii::app()->end(); // } //Save image $path = $galleryConfig['gFolder'] . '/1/' . $galleryConfig['picturesDir'] . '/' . $name; $img = new FImage(); $img->name = $name; $img->path = $path; $img->fbid = Yii::app()->session['fbid']; $img->votes = 0; $img->save(); //$this->resizeAllNew(); $fromDir = $tmpPath; $toDir = $imgsPath; $arrFiles = MyFiles::filesFromDir($fromDir, "jpg"); $imgWidth = $galleryConfig['imgWidth']; $thWidth = $galleryConfig['thWidth']; $quality = $galleryConfig['quality']; $sharpen = $galleryConfig['sharpen']; $arrOfTarget = array('images' => array('toDir' => $imgsPath, 'width' => $imgWidth), 'thumbs' => array('toDir' => $thPath, 'width' => $thWidth), 'overviews' => array('toDir' => $ovPath, 'width' => $thWidth * 2)); foreach ($arrFiles as $file) { self::resizeImg($file, $galleryConfig['imgWidth'], $galleryConfig['imgHeight'], $fromDir, $imgsPath, $quality, $sharpen); //Save width+height $image = new Image($imgsPath . $file); $imgWidth = $image->__get('width'); $imgHeight = $image->__get('height'); $img = $this->loadModel($img->id); $img->width = $imgWidth; $img->height = $imgHeight; $img->save(); //create thumb self::resizeImg($file, $galleryConfig['thWidth'], $galleryConfig['thHeight'], $fromDir, $thPath, $quality, $sharpen); //double size overview when nouse is above thumb self::resizeImg($file, $galleryConfig['thWidth'] * 2, $galleryConfig['thHeight'] * 2, $fromDir, $ovPath, $quality, $sharpen); } if ($galleryConfig['keepOriginal']) { MyFiles::moveAllFiles($tmpPath, $originalPath); } MyFiles::emptyFolder($fromDir); if (isset($img)) { $imageId = $img->id; $n = Vote::model()->countBySql("SELECT COUNT(*) FROM votes WHERE imageId=" . $imageId); $isvoted = Vote::model()->countBySql("SELECT COUNT(*) FROM votes WHERE fbid='" . $_SESSION["fbid"] . "' AND imageId=" . $imageId); $this->render('update', array('img' => $img, 'votes' => $n, 'isvoted' => $isvoted)); } else { //$this->render('error', "Položka nebyla nalezena. "); $this->pageName = "upload"; $this->render('error', array('message' => 'Image/Uploader: img was not found in db.')); } }
/** This method is called when all files are uploaded * Will resize all pictures from temporary folder * If keepOriginal is set to true, after resize, all files are moved to folder where we keep originals * If keepOriginal is set to false, all pictures will be removed from temporary folder */ public function resizeAllNew() { $fromDir = $this->tmpPath; $toDir = $this->imgsPath; $arrFiles = MyFiles::filesFromDir($fromDir); $imgWidth = $this->galleryConfig['imgWidth']; $thWidth = $this->galleryConfig['thWidth']; $quality = $this->galleryConfig['quality']; $sharpen = $this->galleryConfig['sharpen']; $arrOfTarget = array('images' => array('toDir' => $this->imgsPath, 'width' => $imgWidth), 'thumbs' => array('toDir' => $this->thPath, 'width' => $thWidth)); foreach ($arrOfTarget as $type => $target) { foreach ($arrFiles as $file) { $this->resizeImg($file, $target['width'], $fromDir, $target['toDir'], $quality, $sharpen); } } $this->addImagesToDB($arrFiles); if ($this->galleryConfig['keepOriginal']) { MyFiles::moveAllFiles($this->tmpPath, $this->originalPath); } MyFiles::emptyFolder($fromDir); }