在Yii2中创建拇指

时间:2015-07-04 12:42:01

标签: yii2 thumbnails yii-extensions yii2-advanced-app

我用这个链接来改变yii2-imagine: ThumbCreate

成功运行后,在/vendor/yiisoft/yii2/Imagine/文件夹中有一个文件夹vendor,然后在该文件夹中还有另外两个名称为imaginecomposer的文件夹。在imagine文件夹中,还有一个具有相同名称的文件夹,然后在该文件夹中有很多文件夹和文件。我正在附加enter image description here的图像。
现在,我如何使用缩略图功能来制作图像。

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
                ]);
        }
    }
}
?>

我怎样才能制作拇指?上传图片后告诉我们是否需要更多信息。

1 个答案:

答案 0 :(得分:2)

1.请从https://github.com/yiisoft/yii2-imagine

下载yii2-imagine

2.在advanced \ vendor \ yiisoft \ yii2中提取文件,然后将“yii2-imagine-master”重命名为“imag”

3.打开高级\ 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);

或您想要使用的其他方法。

最好的问候。