yii实现图片上传及缩略图生成的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-05-02 20:57:43 浏览: 评论:0
这篇文章主要介绍了yii实现图片上传及缩略图生成的方法,详细分析了图片的上传及缩略图的生成原理与实现方法,是非常实用的技巧,需要的朋友可以参考下
本文实例讲述了利用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程序设计有所帮助。
Tags: yii图片上传 yii缩略图生成
- 上一篇:yii的CURD操作实例详解
- 下一篇:Yii配置文件用法详解
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)