首页 > 开发 > PHP > 正文

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

2024-05-04 22:16:54
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表