问题描述
我试图在查看时显示每个教练的图像。训练员的图像取自表格并存储在数据库中。现在我想提取图像。请帮我。 添加代码以提高清晰度。我不确定我是否遵循了正确的代码。
<?= DetailView::widget([
'model' => $model,'attributes' => [
'name','email:email','phone','age','gender','experience','training_category','expectation','professional_certificate','special_clients','time_zones','certificates','document','id','timestamp','status',[
'attribute'=>'photo','value'=>yii::getAlias('@trainerimgurl') . '/' .$model->photo,// 'value'=>Yii::getAlias('@web').'/'.$model->photo,'format'=>['image',['width'=>'100','height'=>'100']]
]
],]) ?>
这是模型代码:
public function rules()
{
return [
[['name','email','photo','status'],'required'],[['phone'],'integer'],[['timestamp'],'safe'],[['photo'],'file'],[['name','string','max' => 20],[['certificates','document'],'max' => 255],];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'name' => 'Name','email' => 'Email','phone' => 'Phone','age' => 'Age','gender' => 'Gender','experience' => 'Experience','training_category' => 'Training Category','expectation' => 'Expectation','professional_certificate' => 'Professional Certificate','special_clients' => 'Special Clients','time_zones' => 'Time Zones','certificates' => 'Certificates','photo' => 'Photo','document' => 'Document',
控制器代码:
public function actionView($id)
{
return $this->render('view',[
'model' => $this->findModel($id),]);
}
/**
* Creates a new JoinUs model.
* If creation is successful,the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new JoinUs();
if ($model->load(Yii::$app->request->post()) ) {
$model->save();
$trainerId= $model->id;
$image= UploadedFile::getInstance($model,'photo');
$imgName='tra_' .$trainerId. '.' .$image->getExtension();
$image->saveAs(yii::getAlias('@trainerImgPath') . '/' .$imgName);
$model->photo = $imgName ;
$model->save();
return $this->redirect(['view','id' => $model->id]);
}
解决方法
你可以这样渲染:
[
'attribute'=>'photo','value'=> function($model){
return Html::img(yii::getAlias('@trainerImgUrl') . '/' .$model->photo,['alt' => 'my photo',....// other image attributes//]); //check if your image url is correct
}
'format'=>'raw'
]