/** * @param string $imageContent * @return \ImageMS\models\File\File */ public function addIfNotExist($imageContent) : File { list($imageWidth, $imageHeigth, $imageType) = getimagesizefromstring($imageContent); list($dirPath, $fileName) = $this->_getFilePath($imageContent); $basePath = (new Module('Image'))->params['file']['BASE_DIR']; $filePath = $dirPath . $fileName . image_type_to_extension($imageType); $row = File::find()->where(['file_path' => $filePath])->one(); if (!is_null($row)) { if (!is_file($basePath . $filePath)) { throw new ErrorException('File not exist! ' . $filePath); } return $row; } SystemFolder::create($basePath . $dirPath, 0777); file_put_contents($basePath . $filePath, $imageContent); $row = new File(); $row->setWidth($imageWidth)->setHeigth($imageHeigth)->setFilePath($filePath)->setType($imageType)->save(); return $row; }
/** * @return self */ private function _init() { $this->_objFileInformation = $this->_objRoundSaveThumb->getFileInformation()->one(); $this->_objFile = $this->_objFileInformation->getFile()->one(); $this->_objThumbsCategory = $this->_objRoundSaveThumb->getThumbsCategory()->one(); $this->_basePath = Module::getInstance()->params['file']['BASE_DIR']; $this->_originalFilePath = $this->_objFile->getFilePath(); $this->_fileExtention = image_type_to_extension($this->_objFile->getType()); $pattern = '#(.*\\/)' . Module::getInstance()->params['file']['FILE_ORIGINAL_NAME'] . '\\.#ui'; preg_match($pattern, $this->_originalFilePath, $arrSaveDir); $this->_thumbFileSaveDir = $arrSaveDir[1]; return $this; }
/** * @return \yii\db\ActiveQuery */ public function getFiles() { return $this->hasMany(File::className(), ['id' => 'file_id'])->viaTable('image_thumbs2file', ['thumbs_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getFile() { return $this->hasOne(File::className(), ['id' => 'file_id']); }