public static function file(UploadedFile $fileInstance, $dir = '', $namePostfix = true) { $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance, $namePostfix); if (!$fileInstance->saveAs($fileName)) { throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.'); } return Upload::getLink($fileName); }
public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false) { $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance); $uploaded = $resizeWidth ? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop) : $fileInstance->saveAs($fileName); if (!$uploaded) { throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.'); } return Upload::getLink($fileName); }
static function createThumbnail($fileName, $width, $height = null, $crop = true) { $fileName = str_replace(Url::base(true), '', $fileName); $webRoot = Yii::getAlias('@webroot'); if (!strstr($fileName, $webRoot)) { $fileName = $webRoot . $fileName; } $thumbFolder = dirname($fileName) . DIRECTORY_SEPARATOR . ($width . ($height ? 'x' . $height : '')); $thumbFile = $thumbFolder . DIRECTORY_SEPARATOR . basename($fileName); if (!FileHelper::createDirectory($thumbFolder)) { throw new HttpException(500, 'Cannot create "' . $thumbFolder . '". Please check write permissions.'); } return self::copyResizedImage($fileName, $thumbFile, $width, $height, $crop) ? Upload::getLink($thumbFile) : false; }
public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false) { $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance); $uploaded = $resizeWidth ? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop) : $fileInstance->saveAs($fileName); if (!$uploaded) { throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.'); } if (@exif_imagetype($fileName) === IMAGETYPE_GIF) { $frameFileName = $fileName . '.frame.png'; $image = new Imagick($fileName); $image = $image->coalesceImages(); foreach ($image as $frame) { $frame->setImageFormat('png'); $frame->writeImage($frameFileName); return Upload::getLink($frameFileName); } } return Upload::getLink($fileName); }
public function actionEdit($id) { $model = File::findOne($id); if ($model === null) { $this->flash('error', Yii::t('easyii', 'Not found')); return $this->redirect(['/admin/' . $this->module->id]); } if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { if ($fileInstanse = UploadedFile::getInstance($model, 'file')) { $model->file = $fileInstanse; if ($model->validate(['file'])) { $model->file = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; $model->time = time(); } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); return $this->refresh(); } } else { $model->file = $model->oldAttributes['file']; } if ($model->save()) { $this->flash('success', Yii::t('easyii/file', 'File updated')); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); } return $this->refresh(); } } else { return $this->render('edit', ['model' => $model]); } }
public function actionEdit($id) { $model = File::findOne($id); $model->loadTranslations_custom(); $file_ru = $model->file_ru; $file_it = $model->file_it; $file_sp = $model->file_sp; if ($model === null) { $this->flash('error', Yii::t('easyii', 'Not found')); return $this->redirect(['/admin/' . $this->module->id]); } if ($model->load(Yii::$app->request->post())) { if (Yii::$app->request->isAjax) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return ActiveForm::validate($model); } else { /*if(($fileInstanse = UploadedFile::getInstance($model, 'file'))) { $model->file = $fileInstanse; if($model->validate(['file'])){ $model->file = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; $model->time = time(); } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); return $this->refresh(); } } else{ $model->file = $model->oldAttributes['file']; } if($model->save()){ $this->flash('success', Yii::t('easyii/file', 'File updated')); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); } return $this->refresh();*/ foreach (array_keys(Yii::$app->params['mlConfig']['languages']) as $lang) { $file_lang = $lang != Yii::$app->params['mlConfig']['default_language'] ? 'file_' . $lang : 'file'; if ($fileInstanse = UploadedFile::getInstance($model, $file_lang)) { $model->{$file_lang} = $fileInstanse; if ($model->validate($file_lang)) { $model->{$file_lang} = Upload::file($fileInstanse, 'files', false); $model->size = $fileInstanse->size; $model->time = time(); } else { $this->flash('error', Yii::t('easyii/file', 'File error. {0}', $model->formatErrors())); return $this->refresh(); } } else { $model->{$file_lang} = $file_lang == 'file' ? $model->oldAttributes['file'] : ${'file_' . $lang}; } } if ($model->save()) { $this->flash('success', Yii::t('easyii/cooperation', 'Cooperation updated')); } else { $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors())); } return $this->refresh(); } } else { return $this->render('edit', ['model' => $model]); } }