/** * Sets a new profile image by given temp file * * @param CUploadedFile $file */ public function setNew($file) { $this->delete(); ImageConverter::TransformToJpeg($file->tempName, $this->getPath('_org')); ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), array('width' => 1134, 'mode' => 'max')); ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), array('width' => $this->width, 'height' => $this->height)); }
public function afterSave($insert, $changedAttributes) { // Set new uploaded file if ($this->uploadedFile !== null && $this->uploadedFile instanceof UploadedFile) { $newFilename = $this->getStoredFilePath(); if (is_uploaded_file($this->uploadedFile->tempName)) { move_uploaded_file($this->uploadedFile->tempName, $newFilename); @chmod($newFilename, 0744); } /** * For uploaded jpeg files convert them again - to handle special * exif attributes (e.g. orientation) */ if ($this->uploadedFile->type == 'image/jpeg') { ImageConverter::TransformToJpeg($newFilename, $newFilename); } } // Set file by given contents if ($this->newFileContent != null) { $newFilename = $this->getStoredFilePath(); file_put_contents($newFilename, $this->newFileContent); @chmod($newFilename, 0744); } return parent::afterSave($insert, $changedAttributes); }
/** * Create a cfile model and create and connect it with its basefile File model from a given data file, connect it with its parent folder. * TODO: This method has a lot in common with BrowseController/actionUpload, common logic needs to be extracted and reused * * @param Response $response * the response errors will be parsed to. * @param int $parentFolderId * the files pid. * @param string $folderPath * the path of the folder the file data lies in. * @param string $filename * the files name. */ protected function generateModelFromFile(&$response, $parentFolderId, $folderPath, $filename) { $filepath = $folderPath . DIRECTORY_SEPARATOR . $filename; // check if the file already exists in this dir $filesQuery = File::find()->joinWith('baseFile')->readable()->andWhere(['title' => File::sanitizeFilename($filename), 'parent_folder_id' => $parentFolderId]); $file = $filesQuery->one(); // if not, initialize new File if (empty($file)) { $file = new File(); $humhubFile = new \humhub\modules\file\models\File(); } else { // else replace the existing file $humhubFile = $file->baseFile; // logging file replacement $response['infomessages'][] = Yii::t('CfilesModule.base', '%title% was replaced by a newer version.', ['%title%' => $file->title]); $response['log'] = true; } // populate the file $humhubFile->mime_type = FileHelper::getMimeType($filepath); if ($humhubFile->mime_type == 'image/jpeg') { ImageConverter::TransformToJpeg($filepath, $filepath); } $humhubFile->size = filesize($filepath); $humhubFile->file_name = $filename; $humhubFile->newFileContent = stream_get_contents(fopen($filepath, 'r')); if ($humhubFile->save()) { $file->content->container = $this->contentContainer; $file->parent_folder_id = $parentFolderId; if ($file->save()) { $humhubFile->object_model = $file->className(); $humhubFile->object_id = $file->id; $humhubFile->save(); $this->files[] = array_merge($humhubFile->getInfoArray(), ['fileList' => $this->renderFileList()]); } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($file->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $file->title]) . $messages; $response['log'] = true; } } else { $count = 0; $messages = ""; // show multiple occurred errors foreach ($humhubFile->errors as $key => $message) { $messages .= ($count++ ? ' | ' : '') . $message[0]; } $response['errormessages'][] = Yii::t('CfilesModule.base', 'Could not save file %title%. ', ['%title%' => $humhubFile->filename]) . $messages; $response['log'] = true; } }
/** * Sets a new profile image by given temp file * * @param mixed $file CUploadedFile or file path */ public function setNew($file) { if ($file instanceof \yii\web\UploadedFile) { $file = $file->tempName; } $this->delete(); ImageConverter::TransformToJpeg($file, $this->getPath('_org')); ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), array('width' => 400, 'mode' => 'max')); ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), array('width' => $this->width, 'height' => $this->height)); }