yii实现图片上传及缩略图生成的方法_php实例
程序员文章站
2022-05-19 21:23:23
...
本文实例讲述了利用yii框架来实现图片上传功能并在上传成功之后自动生成缩略图的方法,分享给大家供大家参考。具体实现方法如下:
图片显示页面:
('enctype'=>'multipart/form-data'))?>
Action文件:
复制代码 代码如下:
/**
* 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,
));
}
}
* 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:
复制代码 代码如下:
class Upload extends CFormModel {
public $file;
public function rules() {
return array(
array('file', 'file', 'types' => 'jpg, gif, png,zip'),
);
}
}
public $file;
public function rules() {
return array(
array('file', 'file', 'types' => 'jpg, gif, png,zip'),
);
}
}
图片显示页面:
自定义图片大小,缩略图自动生成
复制代码 代码如下:
/**
* list.php
* Created on: 2014-1-26 13:12:01 by Outsider
*/
?>
* list.php
* Created on: 2014-1-26 13:12:01 by Outsider
*/
?>
图片上传表单:
复制代码 代码如下:
File was uploaded. Check .
('enctype'=>'multipart/form-data'))?>
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
推荐阅读
-
php实现按指定大小等比缩放生成上传图片缩略图的方法
-
AJAX实现图片预览与上传及生成缩略图的方法
-
yii实现图片上传及缩略图生成的方法
-
PHP基于ffmpeg实现转换视频,截图及生成缩略图的方法
-
yii2整合百度编辑器umeditor及umeditor图片上传问题的解决办法_php实例
-
yii2整合百度编辑器umeditor及umeditor图片上传问题的解决办法_php实例
-
php实现按指定大小等比缩放生成上传图片缩略图的方法
-
yii使用activeFileField控件实现上传文件与图片的方法_PHP
-
使用PHP生成图片的缩略图的方法_php实例
-
yii使用activeFileField控件实现上传文件与图片的方法_PHP