public function getImage($item, $dirtyAlias) { //Get params $params = $data = $this->parseImageAlias($dirtyAlias); $alias = $params['alias']; $size = $params['size']; $itemId = preg_replace('/[^0-9]+/', '', $item); $modelName = preg_replace('/[0-9]+/', '', $item); //Lets get image if (empty($this->className)) { $imageQuery = Image::find(); } else { $class = $this->className; $imageQuery = $class::find(); } $image = $imageQuery->where(['modelName' => $modelName, 'itemId' => $itemId, 'urlAlias' => $alias])->one(); if (!$image) { return $this->getPlaceHolder(); } return $image; }
/** * * removes concrete model's image * @param Image $img * @throws \Exception */ public function removeImage(Image $img) { $img->clearCache(); $storePath = $this->getModule()->getStorePath(); $fileToRemove = $storePath . DIRECTORY_SEPARATOR . $img->filePath; if (preg_match('@\\.@', $fileToRemove) and is_file($fileToRemove)) { unlink($fileToRemove); } $img->delete(); }
/** * @depends testAttachImage * @depends testGetImage */ public function testRemoveImage() { $this->model->attachImage(__DIR__ . '/data/testPicture.jpg'); $img = $this->model->getImage(); //Make cache copy $file = scandir(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1'))[2]; mkdir(vfsStream::url('root/Cache/ActiveRecordImages/')); mkdir(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1')); copy(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1') . '/' . $file, vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file); $this->assertTrue(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file)); $this->model->removeImage($img); //Check db record removed $imageRecord = Image::find()->where(['itemId' => $this->model->id, 'modelName' => 'ActiveRecordImage'])->one(); $this->assertTrue($imageRecord == NULL); //check files not exists $files = scandir(vfsStream::url('root/Store/ActiveRecordImages/ActiveRecordImage1')); $this->assertTrue(count($files) == 2); //Check cache file and folder $this->assertFalse(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1') . '/' . $file)); $this->assertFalse(file_exists(vfsStream::url('root/Cache/ActiveRecordImages/ActiveRecordImage1'))); }