Answer the question
In order to leave comments, you need to log in
How to write functions that work with variable names as object property names?
The bottom line is this: there are 3 functions for working with pictures
public $drivePath = '@app/web/files/images/drivers/';
public $webPath = '/web/files/images/drivers/';
public function uploadImage()
{
if (!$this->image)
return false;
$path = Yii::getAlias($this->drivePath.$this->car_id.'/');
if (!is_dir($path)) mkdir($path, 0777, true);
$filename = Yii::$app->security->generateRandomString(9);
$this->image->saveAs($path . $filename . '.' . $this->image->extension);
$this->image = $filename . '.' . $this->image->extension;
return true;
}
public function getImage()
{
$path = Yii::getAlias($this->webPath.$this->car_id.'/'.$this->image);
return Html::img($path, [
'height' => '100px',
'width' => 'auto'
]);
}
public function deleteImage()
{
$path = Yii::getAlias($this->drivePath.$this->car_id.'/'.$this->oldAttributes['image']);
if (file_exists($path))
unlink($path);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question