欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

yii实现图片上传及缩略图生成的方法

程序员文章站 2022-07-11 23:52:23
本文实例讲述了利用yii框架来实现图片上传功能并在上传成功之后自动生成缩略图的方法,分享给大家供大家参考。具体实现方法如下: action文件: 复制代码 代码如下:&...

本文实例讲述了利用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()?>

希望本文所述对大家基于yii框架的php程序设计有所帮助。