我的图像尺寸很大,我想在将它们保存到数据库之前对其进行压缩。这是我的 Controller ,有没有办法在没有任何扩展的情况下做到这一点?
public function actionCreate()
{
$model = new Business;
if (isset($_POST['Business'])) {
$rnd = rand(0, 9999);
$model->attributes = $_POST['Business'];
$uploadedFile = CUploadedFile::getInstance($model, 'image');
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
if ($model->save()) {
$uploadedFile->saveAs(Yii::app()->basePath . '/../img/' . $fileName);
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
最佳答案
1) 您需要下载this扩展并像文档中一样在配置中连接它。
2) 上传原始文件后,您只需调用方法resize()
。这是作品。我查了一下。在你的情况下,它将是这样的:
public function actionCreate()
{
$model = new Business;
if (isset($_POST['Business'])) {
$rnd = rand(0, 9999);
$model->attributes = $_POST['Business'];
$uploadedFile = CUploadedFile::getInstance($model, 'image');
$fileName = "{$rnd}-{$uploadedFile}";
$model->image = $fileName;
if ($model->validate()) {
$path = Yii::app()->basePath . '/../img/' . $fileName;
$uploadedFile->saveAs($path);
$image = new EasyImage($path);
$thumbPath = Yii::app()->basePath . '/../thumb/' . $fileName;
$image->resize(100, 100);
$image->save($thumbPath);
$model->save();
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model,
));
}
关于php - Yii:有什么方法可以以压缩形式保存图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807051/