Example #1
0
 /**
  * @return bool
  * @throws \yii\db\Exception
  */
 public function savePhoto()
 {
     if ($this->file) {
         $this->deletePhoto();
         $ext = "." . end(explode(".", $this->file));
         if ($ext === ".") {
             $ext = '.jpg';
         }
         $path = Document::FROM_ADM_PATH . Document::FILES_PATH . $this->document_id . '/';
         if (!file_exists($path)) {
             mkdir($path, 0777, true);
         }
         $name = $this->document->alias . "-" . $this->id;
         $fullname = $path . $name . $ext;
         $this->file->saveAs($fullname);
         $this->file = $fullname;
         Image::thumbnail($fullname, 300, 200, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)->save($path . $name . '_thumb' . $ext, ['quality' => 100]);
         $this->value = Document::FILES_PATH . $this->document_id . '/' . $name . $ext;
         if (!$this->isNewRecord) {
             $db = Field::getDb();
             $db->createCommand()->update('field', ['value' => Document::FILES_PATH . $this->document_id . '/' . $name . $ext], ['id' => $this->id])->execute();
         } else {
             $this->save();
         }
     }
     return true;
 }