Exemple #1
0
 /**
  * @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;
 }