public function actionIndex() { echo "================================================================================\n\r"; echo "ATMOSPHERA console command: Batch resizing of uploaded photos\n\r"; echo "================================================================================\n\r"; $fileform = new PhotoFileForm(); //get instance of model $fileform->setImageSize(); //set sizes from params-local $path = UrlHelper::getImageDir(); //get dir for upload $criteia = new CDbCriteria(); $countTotal = Photo::model()->count($criteia); echo "total count of photo: " . $countTotal . "\n\r"; echo "start process... please wait\n\r"; $offset = 0; //DebugBreak(); $step = floor($countTotal / 10); $criteia->limit = $step; $criteia->offset = $offset; $countTotal = 0; while (($count = count($aPhotos = Photo::model()->findAll($criteia))) > 0) { foreach ($aPhotos as $oPhoto) { $fileform->createImages($path, $oPhoto->filename, $oPhoto->thumb_filename); } $offset += $step; $criteia = new CDbCriteria(); $criteia->limit = $step; $criteia->offset = $offset; $countTotal += $count; echo "."; } echo "\n\r"; echo "total count of processed files: " . $countTotal . "\n\r"; }
public function uploadPhoto() { $success = false; $request = array(); $model = new FileForm(); $file = CUploadedFile::getInstance($model, 'file'); if ($model->validate()) { //DebugBreak(); //check extension of uploaded file $types = PhotoFileForm::getImageExtensions(); $isImage = in_array($file->extensionName, $types); if ($isImage) { $model = new PhotoFileForm(); } unset($photoFileForm); $model->file = $file; $success = $model->saveFile($model->file, $this->uploadDir); //$success = $model->file->saveAs($path . DIRECTORY_SEPARATOR . $newname); if ($success) { $arrPhoto = Yii::app()->request->getParam('arrPhoto'); //get data array for photo $arrPhoto = json_decode($arrPhoto); $fileNum = $arrPhoto->fileNum; //Yii::app()->request->getParam('photoCount'); //get count of files from page $photoCount = $arrPhoto->photoCount; //Yii::app()->request->getParam('photoCount'); //get count of files from page $maxSortOrder = $arrPhoto->maxSortOrder; $uniqueId = Yii::app()->request->getParam('uniqueId'); //get unique value for portfolio (widget) from page $this->portfolioType = Yii::app()->request->getParam('portfolioType'); //get portfolio type if (empty($photoCount)) { $photoCount = 0; } if (empty($uniqueId)) { $uniqueId = uniqid(); } // create and initialize new Photo model //$model->processFile(array('is_top'=>($photoCount == 0), 'sort_order'=>$maxSortOrder + 1)); $photo = new Photo(); $photo->title = $model->originalName; //original name of file $photo->filename = $model->secureName; //new name $photo->thumb_filename = $model->secureTName; //temp: thumb_filename = filename $photo->mime_type = $model->file->type; $photo->sort_order = $maxSortOrder + 1; //sort order = current idx of uploaded file if ($this->portfolioType == 1) { $photo->is_top = $photoCount == 0; } else { $photo->is_top = 0; } $this->initViews(); $_view = $isImage ? $this->_viewItem : $this->_viewFileAny; //select view $itemHtml = $this->renderFile($_view, array('uniqueId' => $uniqueId, 'uploadUrl' => $this->uploadUrl, 'model' => $photo, 'idx' => $fileNum + 1, 'portfolioType' => $this->portfolioType, 'radiobuttonClass' => $this->radiobuttonClass), true); $request = array('filename' => $model->secureTName, 'itemHtml' => $itemHtml); } else { throw new Exception('File not uploaded!' . $model->file->error); } } $request = CMap::mergeArray(array('success' => $success), $request); echo CJSON::encode($request); }
public static function getImageExtensions() { $photoFileForm = new PhotoFileForm(); $rules = $photoFileForm->rules(); $types = isset($rules[0]['types']) ? explode(',', str_replace(' ', '', $rules[0]['types'])) : array(); return $types; }