gpt4 book ai didi

yii2 - 在 Yii2 中从上传的图像创建缩略图

转载 作者:行者123 更新时间:2023-12-04 01:20:20 25 4
gpt4 key购买 nike

如何从上传的图像创建缩略图?

我在我的 Controller 中尝试了这个:

<?php
namespace backend\controllers;

use Yii;
use app\models\Employee;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\imagine\Image;

public function actionCreate() {
$model = new Employee();
$model->added_date_time = date('Y-m-d H:i:s');
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstance($model,'avatar');

if (!empty($model->file)) {
$imageName = Yii::$app->security->generateRandomString();
$model->file->saveAs('uploads/' . $imageName . '.' . $model->file->extension);
$model->avatar = $imageName . '.' . $model->file->extension;
$file = 'uploads/' . $imageName . '.' . $model->file->extension;
Image::thumbnail($file, 200, 200)->save('uploads/thumb/', ['quality' => 80]);
}
if ($model->save()) {
$this->redirect(\Yii::$app->urlManager->createUrl('employee'));
}
} else {
return $this->render('create', ['model' => $model]);
}
}

而且它不起作用。图像已上传,但未创建缩略图。

有人可以帮忙吗?

最佳答案

设置原始缩略图图像的正确路径,然后save结果:

$imgPath = Yii::$app->basePath . '/uploads/'; // as an example
$imgName = Yii::$app->security->generateRandomString();
$fileExt = '.' . $model->file->extension;

$originFile = $imgPath . $imgName . $fileExt;
$thumbnFile = $imgPath . $imgName . '-thumb' . $fileExt;

// Generate a thumbnail image
Image::thumbnail($originFile, 200, 200)->save($thumbnFile, ['quality' => 80]);

关于yii2 - 在 Yii2 中从上传的图像创建缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com