Yii:有什么方法可以以压缩形式保存图像?
Yii: any way to save the images in compressed form?
我有很大的图片,我想在将它们保存到数据库之前压缩它们。这是我的控制器,有没有办法不用任何扩展就可以做到这一点?
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,
));
}
我有很大的图片,我想在将它们保存到数据库之前压缩它们。这是我的控制器,有没有办法不用任何扩展就可以做到这一点?
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,
));
}