在Yii2中创建拇指
我已使用此链接来insatll yii2 imagine: 当它成功运行时,然后在在Yii2中创建拇指,yii2,thumbnails,yii-extensions,yii2-advanced-app,Yii2,Thumbnails,Yii Extensions,Yii2 Advanced App,我已使用此链接来insatll yii2 imagine: 当它成功运行时,然后在/vendor/yiisoft/yii2/Imagine/文件夹中有一个文件夹vendor,然后在该文件夹中还有两个文件夹名为Imagine和composer。在想象一下文件夹中,又有一个同名文件夹,然后在该文件夹中有许多文件夹和文件。我正在为此附加图像。现在,我如何使用缩略图功能制作图像的拇指。 composer.json文件 { "name": "yiisoft/yii2-imagine",
/vendor/yiisoft/yii2/Imagine/
文件夹中有一个文件夹vendor
,然后在该文件夹中还有两个文件夹名为Imagine
和composer
。在想象一下
文件夹中,又有一个同名文件夹,然后在该文件夹中有许多文件夹和文件。我正在为此附加图像。现在,我如何使用缩略图功能制作图像的拇指。 composer.json文件
{
"name": "yiisoft/yii2-imagine",
"description": "The Imagine integration for the Yii framework",
"keywords": ["yii2", "imagine", "image", "helper"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2-imagine/issues",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2-imagine"
},
"authors": [
{
"name": "Antonio Ramirez",
"email": "amigo.cobos@gmail.com"
}
],
"require": {
"yiisoft/yii2-imagine": "*",
"imagine/imagine": "0.5.*"
},
"autoload": {
"psr-4": {
"yii\\imagine\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
<?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; // upload the image in folder
use yii\Imagine\Image;
use vendor\ExportXLS; // for export data in excel file
class EmployeeController extends Controller
{
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/emp/'.$imageName.'.'.$model->file->extension);
$model->avatar = $imageName.'.'.$model->file->extension;
$originalFile = Yii::$app->basePath.'/uploads/emp/'.$imageName.'.'.$model->file->extension;
$thumbFile = Yii::$app->basePath.'/uploads/emp/thumb/'.$imageName.'.'.$model->file->extension;
$saveThumb = Image::thumbnail($originalFile, 200, 200)->save($thumbFile, ['quality' => 80]);
}
if($model->save()){
$this->redirect(\Yii::$app->urlManager->createUrl('employee'));
}
}
else {
return $this->render('create', [
'model' => $model
]);
}
}
}
?>
控制器文件
{
"name": "yiisoft/yii2-imagine",
"description": "The Imagine integration for the Yii framework",
"keywords": ["yii2", "imagine", "image", "helper"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/yii2-imagine/issues",
"forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2-imagine"
},
"authors": [
{
"name": "Antonio Ramirez",
"email": "amigo.cobos@gmail.com"
}
],
"require": {
"yiisoft/yii2-imagine": "*",
"imagine/imagine": "0.5.*"
},
"autoload": {
"psr-4": {
"yii\\imagine\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
<?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; // upload the image in folder
use yii\Imagine\Image;
use vendor\ExportXLS; // for export data in excel file
class EmployeeController extends Controller
{
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/emp/'.$imageName.'.'.$model->file->extension);
$model->avatar = $imageName.'.'.$model->file->extension;
$originalFile = Yii::$app->basePath.'/uploads/emp/'.$imageName.'.'.$model->file->extension;
$thumbFile = Yii::$app->basePath.'/uploads/emp/thumb/'.$imageName.'.'.$model->file->extension;
$saveThumb = Image::thumbnail($originalFile, 200, 200)->save($thumbFile, ['quality' => 80]);
}
if($model->save()){
$this->redirect(\Yii::$app->urlManager->createUrl('employee'));
}
}
else {
return $this->render('create', [
'model' => $model
]);
}
}
}
?>
我怎么做拇指?上传图像后。告诉我是否需要更多信息。1.请从 2.提取advanced\vendor\yiisoft\yii2中的文件,然后将“yii2 imagine master”重命名为“imagine” 3.打开advanced\vendor\yiisoft\yii2\classes.php并插入这两行
'yii\imagine\Image' => YII2_PATH . '/imagine/Image.php',
'yii\imagine\Image' => YII2_PATH . '/imagine/BaseImage.php',
在return[]中,与其他行一样
4.现在请转到前端或后端,打开一个视图或控制器,然后在顶部添加以下代码
use yii\imagine\Image;
现在添加了类,然后可以使用Image::如下所示
Image::thumbnail($filename, $width, $height);
或者你想用的其他方法
致以最良好的祝愿