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 $image = \circulon\images\models\Image::find()->where(['model_name' => $modelName, 'item_id' => $itemId, 'url_alias' => $alias])->one(); if (!$image) { return $this->getPlaceholder(); } return $image; }
/** * returns model image * @return null|image */ public function run($id = false, $ref = false) { if (empty($id) || empty($ref)) { return null; } $modelClasses = []; foreach ($this->models as $aModel) { if (preg_match('@\\\\([\\w]+)$@', $aModel, $matches)) { $modelClasses[] = $matches[1]; } else { $modelClasses[] = $aModel; } } $exploded = explode('_', $ref); $size = isset($exploded[1]) ? $exploded[1] : false; $ref = isset($exploded[0]) ? $exploded[0] : $ref; // this SHOULD return a unique record $image = Image::find()->where(['item_id' => $id, 'url_alias' => $ref])->andWhere(['in', 'model_name', $modelClasses])->one(); if ($image) { header('Content-Type: image/png'); echo $image->getContent($size); } }
private function imageQuery() { $query = Image::find()->where(['item_id' => $this->owner->{$this->idAttribute}, 'model_name' => $this->getModule()->getShortClass($this->owner)])->orderBy(['is_main' => SORT_DESC]); return $query; }
/** * @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(['item_id' => $this->model->{$this->model->idAttribute}, 'model_name' => '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'))); }