发布于 2014-12-05 12:07:38 | 489 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的YII 2.0教程,程序狗速度看过来!

Yii高性能PHP框架

Yii Framework是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。Yii提供了今日Web 2.0应用开发所需要的几乎一切功能。Yii是最有效率的PHP框架之一。Yii是创始人薛强的心血结晶,于2008年1月1日开始开发。


本文向大家讲解的是一个yii下实现图片上传及缩略图生成的方法及示例代码,感兴趣的同学参考下。

Action文件:

 
<?php
/**
 * TestController.php
 * Created on: 2014-1-26 12:59:36 by Outsider
 */
class TestController extends CController {
 
    /**
     * 缩略图片生成
     * @ path 图片路径
     * @ width 图片宽度
     * @ height 图片高度
     */
    public function actionGetThumb($path, $w, $h) {
        $file_name = md5($path . $w . $h);
        if (file_exists('./temp/' . $file_name . '.jpg')) {
            header('location:/temp/' . $file_name . '.jpg');
            Yii::app()->end();
        }
        Yii::import("ext.EPhpThumb.EPhpThumb");
        $thumb = new EPhpThumb();
        $thumb->init();
        $thumb->create('.' . $path)
                ->adaptiveResize($w, $h)
                ->save('./temp/' . $file_name . '.jpg')
                ->show();
    }
 
    /*
     * 图片显示
     */
 
    public function actionList() {
        $attache = Attache::model();
        $list = $attache->findAll();
        $this->render('list', array('list' => $list));
        die;
    }
 
    /**
     * 文件上传
     */
    public function actionIndex() {
        $path = getcwd() . 'uploads';
        $dir = DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m');
        $dir = str_replace("", "/", $dir);
        $uploads_dir = str_replace("", "/", $path . $dir);
        if (!is_dir($uploads_dir) || !is_writeable($uploads_dir)) {
            mkdir($uploads_dir, 0777, TRUE);
            touch($uploads_dir . '/index.html');
        }
        $uploaded = false;
        $model = new Upload();
        if (isset($_POST['Upload'])) {
            $model->attributes = $_POST['Upload'];
            $file = CUploadedFile::getInstance($model, 'file');
            $newName = substr(md5($file->extensionName . round((microtime(true) * 1000))), 0, 17) . '.' . $file->extensionName; 
            $file_name = $uploads_dir . '/' . $newName;
            if ($model->validate()) {
                $attache = new Attache();
                $uploaded = $file->saveAs($file_name, TRUE);
                $attache->name = $file->getName();
                $attache->path = $dir . '/' . $newName;
                $attache->create_time = time();
                $attache->save();
            }
        }
 
        $this->render('index', array(
            'model' => $model,
            'uploaded' => $uploaded,
            'dir' => $uploads_dir,
        ));
    }
}

Upload.php:

 
<?php
class Upload extends CFormModel {
 
    public $file;
 
    public function rules() {
        return array(
            array('file', 'file', 'types' => 'jpg, gif, png,zip'),
        );
    }
}


图片显示页面:

自定义图片大小,缩略图自动生成

 
<?php
/**
 * list.php
 * Created on: 2014-1-26 13:12:01 by Outsider
 */
?>
<?php foreach ($list as $v): ?>
    <img src="<?php echo Yii::app()->createUrl('test/getThumb', array('path' => '/uploads' . $v['path'], 'w' => '150', 'h' => '150')) ?>">
<?php endforeach; ?>

图片上传表单:

 
<?php if($uploaded):?>
<p>File was uploaded. Check <?php echo $dir?>.</p>
<?php endif ?>
<?php echo CHtml::beginForm('','post',array
        ('enctype'=>'multipart/form-data'))?>
    <?php echo CHtml::error($model, 'file')?>
    <?php echo CHtml::activeFileField($model, 'file')?>
    <?php echo CHtml::submitButton('Upload')?>
<?php echo CHtml::endForm()?>


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务